java基礎資料型別 (Elementary Data Type)(二)和分支結構

來源:互聯網
上載者:User

標籤:引用   結果   color   需要   car   結構   運算式   als   false   

基礎資料型別 (Elementary Data Type)(四類八種):不能為null
一、整數型
byte----2的8次方
short----2的16次方
int----2的32次方
long----2的64次方
二、浮點型
float----4個
double---8個位元組
三、布爾型
boolean(true, false)
四、字元型
char(可以是一個字母, 也可以是一個漢字)

int a = 5;
全部存到棧
參考型別:String s = "abc";
所有的類、數組、介面

運算子 字串連接字串需要注意的地方:在輸出的時候,只要有一個參數是字串,整個輸出結果都是字串。 
異或運算子:轉換成二進位的形式來對比每一位元,不一樣的為1,一樣的為0

類型轉換的優先順序:double float long int chcar short byte

char short byte進行運算的時候,取值預設為int

隱式轉換(低--》高) 顯示轉換(高--》低)

分支
if(運算式_布爾值) {
...
}

if(運算式_布爾值) {
...
} else {
...
}

if(運算式_布爾值) {
...
} else if(運算式_布爾值) {
...
}

if(運算式_布爾值) {
...
} else if(運算式_布爾值) {
...
} else {
...
}

int a = 2;
switch(a) {
case 1
...
break;
case 3
...
break;
case 5
...
break;

default
...
}

if和switch的區別

1、switch:必須用break結束迴圈,不然會無限迴圈;if如果不滿足條件自動結束迴圈。

2、if:針對的結果是布爾型的判斷

3、能用switch的可以用if,反過來則不行

 

 

public class kehouzuoye1{

public static void main(String args[]){
int a = 3;
int c = 5 - (a--);               //c=5-(3-1)=2
boolean b = a == c;              //b=false
b = b && (a < c--) ? false : true;      //b=false
int d = b ? 7 : 9;              //d=9


int e = d - 3;                  //e=6
c *= 3;                    //c=3


int f = ((++e == c) ? 25 : 35) + (++a);     //f=38


System.out.println("f的值:" + f);
}
}

 
public class kehouzuoye2{    public static void main(String args[]){int a, b, c;        a = c = 8;        b = a++;    //b=8;a=9;            short d = 3;           long e1 = d++ + a;    //e1=12;d=4;        long e2 = d++ + a++;    //e2=13;d=5;a=10        boolean b1 = false;            boolean b2 = !b1;    //b2=true        float g = b2 ? a : b;    //g=10        double h = g -= 1;    //h=g=9        boolean b3 = g == 10;    //b3=false        char s = b3 ? ‘a‘ : ‘b‘;//s=‘b‘=98        int i = 2 * s;        //i=2‘b‘=186}}

 

迴圈
for(int i=0;i<10;i++) {
...
}

while(運算式_布爾值) {

}

do {

} while(運算式_布爾值);

java基礎資料型別 (Elementary Data Type)(二)和分支結構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.