<分区>
据我所知,new
关键字用于实例化类。而且我认为我们需要调用类构造函数并在 this 关键字之后传递它的参数。 对?
好的,这是一个代码片段:
public class JavaApp{
public static void main(String[] args) {
String st1 = "Test";
String st2 = new String("Test");
byte[] b1 = {'a', 'b'};
byte[] b2 = new byte[]{'a', 'b'};
byte[] b3 = new byte[]('a','b'); //Error
byte b4 = 3;
byte b5 = new byte{3}; //Error
byte b6 = new byte(3); //Error
}
}
我的问题:
st1
和st2
有什么区别?- 为什么我们必须在
{}
中传递构造函数的参数,而不是在b2
声明行中传递()
? - 为什么我收到
b5
和b6
的错误?