天天看点

Java 基础 面试题

1、static 修饰的属性,相较于实例变量,有哪些特别之处?

随着类的加载而加载;早于对象的创建,只要权限通过,可以通过。

对象static.属性的方式进行调用;存在于方法区的静态域。

2、final可以用来修饰哪些结构,分别表示什么意思?

1、1、final 可以用来修饰的结构:类、方法、变量

2、final :用来修饰方法:表明此方法不可以被重写

3、final:用来修饰一个类:此类不能被继承。

如:String类、System类、、StringBuffer类、

4、final:用来修饰常量

如:final int sex s = 10;

4.2 static final 用来修饰属性:全局常量。

3、手写饿汉式、懒汉式的代码。
懒汉式的代码

public class SingletonTest2 {

public static void main(String[] args) {

Order order1 = Order.getInstance();

Order order2 = Order.getInstance();

System.out.println(order1==order2);

order1.show(10);

}

}

class Order{

private Order(){

}

private static Order instance =null;

// 返回当前类的对象。

public static Order getInstance(){

if (instance ==null){

instance = new Order();

}

return instance;

}

public static void show (final int num){

System.out.println(num);

}

}

饿汉式的代码

public class SingletonTest1 {

public static void main(String[] args) {

BanK banK1 = BanK.getInstance();

BanK banK2 = BanK.getInstance();

System.out.println(banK1==banK2); // true

}

}

class BanK{

// 1、私有化类的构造器

private BanK(){

}

//2、创建私有化类的对象

private static BanK instance = new BanK();

// 3、提供公共的静态方法,返回类的对象

public static BanK getInstance(){

return instance;

}

}

4、说明流的三种分类:

流向:输入流、输出流

数据单位:字节流、字符流

流的角色:处理流、缓冲流

写出四个IO流中的抽象基类、四个文件流、 四个缓冲流

InputStream、 FileInputStream BUfferedinputStream

outputStream、 FileoutputStream BUfferedOutputStream

Read FileRead BufferedFileRead

Write FileWrite BufferedFileWrite

字节流和字符流的区别:

字节流处理非文本文件

字符流处理文本文件

字节流主要是操作byte类型数据,以byte数组为准,主要操作类就是OutputStream、InputStream

字符流主要是操作char类型数据,以char数组为准,主要操作类就是FileRead、FileWrite

char[] chf = new char[5];