1. 程式人生 > >java基本語法(運算符)

java基本語法(運算符)

|| ++ println style 結果 小數 else 功能 自動

技術分享圖片

如果是對負數取模,可以把模數符號忽略不計, 如:5%-2 = 1

對於/,整數除和小數除是有區別的,整數之間作除法,保留整數去除余數

+除了字符串相加功能外,還能把非字符串轉換成字符串

System.out.println("5+5="+5+5);    //5+5=55
System.out.println(‘*‘ + ‘\t‘ +‘*‘);  //93
System.out.println("*" + ‘\t‘ +‘*‘);  //* *

public class TestApp {

    @Test
    public void test() {

            
int i1 = 10, i2 = 20; int i = i1++; System.out.print("i="+i); //i=10 System.out.println("i="+i1); //i=11 i = ++i1; System.out.print("i="+i); //i=12 System.out.println("i="+i1); //i=12 i = i2--; System.out.print(
"i="+i); //i=20 System.out.println("i="+i2); //i=19 i = --i2; System.out.print("i="+i); //i=18 System.out.println("i="+i2);//i=18 } }

賦值運算符:

當兩側數據不一致的時候,可以使用自動類型進行類型轉化

public class TestApp {

    @Test
    public void test() {
        
boolean b1 = false; // 區分好==和=的區別。 if (b1 = true) System.out.println("結果為真"); else System.out.println("結果為假"); } } 結果為真
public class TestApp {

    @Test
    public void test() {
        int i = 1;
        i *= 0.1;
        System.out.println(i);//0
        i++;
        System.out.println(i);//1

    }
}

比較運算符

技術分享圖片

邏輯運算符

技術分享圖片

邏輯運算符用於連接布爾型的表達式

&和&&的區別:

&,無論真假,兩邊都要進行運算

&&,左邊為假,不在進行右邊的運算

^(異或)與(|)的差異:兩邊都為true的時候,結果為false

@Test
    public void test() {
        int x = 1;
        int y=1;
        if(x++==2 & ++y==2){
            x =7;
        }
        System.out.println("x="+x+",y="+y);   //x=2,y=2

        
        x = 1;
        y = 1;
        if(x++==2 && ++y==2){
            x =7;
        }
        System.out.println("x="+x+",y="+y);  //x=2,y=1
        
        
        x = 1;
        y = 1;
        if(x++==1 | ++y==1){
            x =7;
        }
        System.out.println("x="+x+",y="+y);  //x=7,y=2
        
        x = 1;
        y = 1;
        if(x++==1 || ++y==1){
            x =7;
        }
        System.out.println("x="+x+",y="+y);   //x=7,y=1
    }

  

位運算符

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

java基本語法(運算符)