package com.ilaoda.day0903; /** * 字符串的常见题1 * @author iLaoda * */ public class Test1 { public static void main(String[] args) { String s1 = "hello"; String s2 = "world"; System.out.println(s1 + s2 == "helloworld"); // false System.out.println("hello" + "world" == "helloworld"); // true } }
false true
会在栈中声明s1,并将"hello"存进常量池中,同时s1存"hello"在常量池中的地址 java
package com.ilaoda.day0903; /** * 字符串的常见题2 * @author iLaoda */ public class Test2 { public static void main(String[] args) { final String s1 = "hello"; final String s2 = "world"; System.out.println(s1 + s2 == "helloworld"); System.out.println("hello" + "world" == "helloworld"); } }
true true
String s1 = "abc"; String s2 = new String("abc"); //这句话建立了几个对象
String s1 = new String("abc"); //这句话建立了几个对象
String s1 = new String("abc"); // 建立两个对象 String s2 = new String("abc"); // 建立1个对象
如下代码没有执行,可是都没有报错。说明语法上没有问题。 this
package com.ilaoda.day0904; /** * 关于final修饰的成员变量和局部变量初始化赋值的几种形式 * @author iLaoda * */ public class Test3 { /** * final修饰的成员变量,能够用如下三种方式初始化赋值: * 1. 声明的时候直接赋值 * 2. 构造方法中赋值 * 3. 构造代码快赋值(其实也是构造方法赋值) */ //1. 声明的时候直接赋值 final int a1 = 1; final int a2; final int a3; //3. 构造代码快赋值(其实也是构造方法赋值) { a2 = 2; } public Test3(int a3) { this.a3 = a3; } // 2. 构造方法中赋值 Test3 test3 = new Test3(5); /** * final修饰的局部变量,能够用如下三种方式初始化赋值: */ public static void haha(int a) { //1. 能够在声明的时候初始化赋值 final int a1 = 1; final int a2; //2. 或者表达式给它赋值。 a2 = a1 + 5; final int a3 = a; } public static void main(String[] args) { //3. 在第一次使用的经过方法 haha(3); } }
package com.ilaoda.day0905; /** * @author iLaoda */ class C { C() { System.out.print("C"); } } class A { /** * * 对父类中的c成员进行初始化,调用了C类的无参构造 * 由于A类中有C类的成员,在Test建立 */ C c = new C(); A() { this("A"); System.out.print("A"); } A(String s) { System.out.print(s); } } class Test extends A { /** * 执行父类的带参构造前要先对父类中的对象进行初始化, */ Test() { super("B"); System.out.print("B"); } public static void main(String[] args) { new Test(); } }
CBB
package com.ilaoda.day0905; /** * 输出结果为多少 * @author iLaoda * */ public class Test2 { static { int x = 5; } static int x, y; // 0 0 public static void main(String args[]) { x--; // x=-1; myMethod(); System.out.println(x + y + ++x); // 从下面可知第一个x为1,y为1 // ++x为2 // 因此:结果为3 } public static void myMethod() { y = x++ + ++x; //真正计算时: y = -1 + 1 = 0 // 第一个 x 进行加运算时为:-1, x++后为:0 // 第二个 x 进行加运算时,这时x已经为0。因此 ++x后,x为1 // 所以 y = 0+1 = 1 } }
3