1.Integer的面试题
Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1 == i2);
System.out.println(i1.equals(i2));
System.out.println("-----------");
Integer i3 = new Integer(128);
Integer i4 = new Integer(128);
System.out.println(i3 == i4);
System.out.println(i3.equals(i4));
System.out.println("-----------");
Integer i5 = 128;
Integer i6 = 128;
System.out.println(i5 == i6);//false 因为 超过了一个字节的范围 会new 一个Integer对象
System.out.println(i5.equals(i6));
System.out.println("-----------");
Integer i7 = 127;
Integer i8 = 127;
System.out.println(i7 == i8);//true 没有超过一个字节的范围 因为在方法区中存在一个 字节常量池 范围-128---127
System.out.println(i7.equals(i8));
2.StringBufffer面试题
class Demo {
public static void main (String[] args) {
StringBuffer buffer1 = new StringBuffer("abc");
StringBuffer buffer2 = new StringBuffer("abc");
String s1 = new String("abc");
String s2 = "abc";
System.out.println(s1 == s2);false
System.out.println(s1 = s2);abc
System.out.println(buffer1 == buffer2);false
System.out.println(buffer1.equals(buffer2) );false
}
}
3.2、请编写程序,把给定字符串中的数字排序
给定的字符串是: “91 27 -45 46 38 50”
最终输出结果是: [-45 27 38 46 50 91]
public class Demo2 {
public static void main(String[] args) {
String s= "91 27 -45 46 38 50";
String[] s1 = s.split(" ");
int[] a = new int[6];
for (int i = 0; i < s1.length; i++) {
a[i]=Integer.parseInt(s1[i]);
}
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
}
4.请编写程序,完成获取指定的日期 与今天相距多少天
public class BrithdayTest {
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入出生日期");
String brithday=sc.nextLine();
Date date = new Date();
long now1=date.getTime();
Date date1 = DateUtils.getdate(brithday, "yyyy.MM.dd");
long now2 = date1.getTime();
long now=(now1-now2)/1000/60/60/24;
System.out.println("出生天数为"+now);
}
}
class DateUtils {
private DateUtils(){};
public static Date getdate(String str,String formatstr) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatstr);
Date parse = simpleDateFormat.parse(str);
return parse;
}
}
5.List<? extends T>和List<? super T>之间有什么区别
class Animal {}
class Dog extends Animal{}
class Cat extends Animal{}
class GenericDemo {
public static void main(String[] args) {
Collection<Object> c = new ArrayList<Object>();
Collection<Animal> c1 = new ArrayList<Animal>();
Collection<Animal> c2 = new ArrayList<Dog>();//报错
Collection<Animal> c3 = new ArrayList<Cat>();//报错
//泛型如果明确了数据类型以后,那么要求左右两边的数据类型必须一致
Collection<? extends Animal> c4 = new ArrayList<Object>();//报错
Collection<? extends Animal> c5 = new ArrayList<Animal>();
Collection<? extends Animal> c6 = new ArrayList<Dog>();
Collection<? extends Animal> c7 = new ArrayList<Cat>();
// ? extends E : 向下限定 , ? 表示的是E或者E的子类
Collection<? super Animal> c8 = new ArrayList<Object>();
Collection<? super Animal> c9 = new ArrayList<Animal>();
Collection<? super Animal> c10 = new ArrayList<Dog>();//报错
Collection<? super Animal> c11 = new ArrayList<Cat>();//报错
//? super E: 向上限定 , ? 表示的是E或者E的父类
}
}
6.编写程序,判断文件中是否有指定的键如果有就修改值
需求:我有一个文本文件file.txt,我知道数据是键值对形式的,但是不知道内容是什么。
请写一个程序判断是否有"lisi"这样的键存在,如果有就改变其实为"100"
file.txt文件内容如下:
zhangsan = 90
lisi = 80
wangwu = 85
public class ProptiesTest {
public static void main(String[] args) throws IOException {
Properties prop = new Properties();
prop.load(new FileReader("user.txt"));
Set<String> keys = prop.stringPropertyNames();
for (String key : keys) {
if ("lisi".equals(key)) {
prop.setProperty(key, "100");
}
}
prop.store(new FileWriter("user.txt"), "haha");
}
}
7.通过UDP协议,完成多线程版本的聊天室程序
public class ReceiveRunnable implements Runnable {
private DatagramSocket ds;
public ReceiveRunnable(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {
//接收数据
while(true){
try{
//1: 创建空的数据报包
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
//2: 接收数据到数据报包
ds.receive(dp);
//3: 解析数据并显示
String ip = dp.getAddress().getHostAddress();
String dataStr = new String(dp.getData(), 0, dp.getLength());
System.out.println("IP地址是:"+ip+" ,数据为:"+dataStr);
} catch (IOException e){
e.printStackTrace();
}
}
//4: 释放资源
//ds.close();
}
}
public class SendRunnable implements Runnable {
private DatagramSocket ds;
public SendRunnable(DatagramSocket ds) {
this.ds = ds;
}
@Override
public void run() {
try{
//发送数据
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line= null;
while((line = br.readLine()) != null ){
//1: 准备数据
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length,InetAddress
.getByName("192.168.1.101"), 12345);
//2: 发送数据
ds.send(dp);
}
//释放资源
ds.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class CharRoom {
public static void main(String[] args) throws IOException {
// 发送端:
DatagramSocket sendSocket = new DatagramSocket();
//接收端:
DatagramSocket receiveSocket = new DatagramSocket(12345);
//发送端
SendRunnable send = new SendRunnable(sendSocket);
//接收端:
ReceiveRunnable receive = new ReceiveRunnable(receiveSocket);
//创建线程
Thread sendThread = new Thread(send);
Thread receiveThread = new Thread(receive);
//启动线程
sendThread.start();
receiveThread.start();
}
}
8.通过TCP协议,完成多线程版本客户端上传文件到服务器端,服务器端给出反馈
* 服务器端
*/
public class TCPServer {
public static void main(String[] args) throws IOException {
//创建服务器Socket对象
ServerSocket ss = new ServerSocket(3344);
while(true){
//等待客户端的连接
final Socket s = ss.accept();
//创建新的线程对象,来执行当前客户端的操作
new Thread(new Runnable() {
@Override
public void run() {
try{
//获取Socket的输入流对象
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//a: 打开文件
long time = System.currentTimeMillis();
String fileName = time + ".txt";
//把数据写到 文本文件中
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
//获取流中的数据
String line = null;
while ((line = br.readLine()) != null) {
//b: 写入数据
bw.write(line);
bw.newLine();
bw.flush();
}
//c: 关闭文件
bw.close();
//给客户端发送反馈信息
//a:获取客户端的输出流对象
BufferedWriter bwServer = new BufferedWriter(
new OutputStreamWriter(s.getOutputStream()));
//b:写反馈信息
bwServer.write("上传成功");
bwServer.newLine();
bwServer.flush();
//c:释放资源
//bwServer.close();
//释放资源
s.close();
} catch(IOException e){
e.printStackTrace();
}
}
}).start();
}
}
}
/*
* 客户端
*/
public class TCPClient {
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s = new Socket("192.168.1.101", 3344);
//读取文本文件中的数据
BufferedReader br = new BufferedReader(new FileReader("server.txt"));
String line = null;
BufferedWriter bw = null;
while ((line = br.readLine()) != null) {
//写数据到 Socket流中
bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bw.write(line);
bw.newLine();
bw.flush();
}
//告诉服务器端, 数据已写完
s.shutdownOutput();
//获取反馈信息
//a: 获取Socket输入流对象
BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
//b: 获取反馈信息
String result = brClient.readLine();
//c: 显示返回信息
System.out.println("反馈信息:" + result);
//释放资源
br.close();
s.close();
}
}