為什麼會有這樣的結果?

來源:互聯網
上載者:User

Java很神秘,有一層紙沒有被捅破的話真的有很多東西貌似是解釋不通,仔細探究之後就真相大白了。

這裡收集幾個典型的案例。

(1)字串的那些事

public class StringTest {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubString a1 = "a"+1;String b1 = "a1";String c1 = new String("a1");String d1 = "a";String i1 = "1";String x1 = d1+i1;System.out.println(a1=="a" + "1");System.out.println(a1=="a" + i1);System.out.println(a1==x1);System.out.println(a1==(d1+i1));System.out.println(a1==b1);System.out.println(a1==c1);System.out.println(a1==(d1+1));System.out.println(c1==(d1+1));System.out.println(a1==(d1+"1"));System.out.println(b1==(d1+"1").intern());}}

執行結果:

true
false
false
false
true
false
false
false
false
true

(2)類載入與執行個體初始化

public class Parent {static {pA = 2;System.out.println("Parent static .");}static int pA = 1;{p = 2;System.out.println("Parent block .");}int p = 1;public Parent() {System.out.println("Parent .");}}
public class Sub extends Parent {static {pB = 2;System.out.println("Sub static .");}static int pB = 1;private int b = pB;{b = 20;System.out.println("Sub block 1.");}public Sub() {System.out.println("Sub .");}{b = 10;System.out.println("Sub block 2.");}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println(new Sub().b);System.out.println("-----1 finish---------------");System.out.println(new Parent().p);System.out.println("-----2 finish---------------");System.out.println(new Parent().pA);}}

執行結果:

Parent static .
Sub static .
Parent block .
Parent .
Sub block 1.
Sub block 2.
Sub .
10
-----1 finish---------------
Parent block .
Parent .
1
-----2 finish---------------
Parent block .
Parent .
1
(3)類型轉換

public static void main(String[] args) {// TODO Auto-generated method stubint x=1;System.out.println((x>1)?1.1:1);}

執行結果:

1.0

(4)類載入

public class ClassLoadTest {private int[] args = new int[this.size];private int size =2;/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubClassLoadTest ct = new ClassLoadTest();System.out.println(ct.args.length);}}

執行結果:

0

(5)基本類型轉換

float f = 1.2;double d = 2.3d;double d1=2.3D;double d2=2.3E10;int i = 0xFF;int i1=025;Integer iObj =1;byte b =128;char c=17c;byte b =1;b=b+1;byte b =1;b+=1;

錯誤的運算式:

float f = 1.2;

byte b=128

char c=17c;

byte b=1;b=b+1;

(6)switch的條件判定

public static void main(String[] args) {int i=1;switch (i) {case 1:System.out.println("1");case 2:System.out.println("2");break;case 3:System.out.println("3");default:System.out.println("default");}}

輸出結果:

1
2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.