1. 程式人生 > 其它 >02 型別轉換、進位制、運算子

02 型別轉換、進位制、運算子

單字元型別

轉義字元

\n:換行
\r:回車 
\t:Tab鍵
\\:\
\":"
\':'
\b:刪除鍵Backspace

字元和字串

char c = '尚';
String s = '尚';//錯誤的,哪怕是一個字元,也要使用雙引號
		
char c2 = '';//錯誤,單引號中有且只能有一個字元
String s2 = "";//可以,雙引號中可以沒有其他字元,表示是空字串

ASCII編碼

字元 數值
0 48
A 65
a 97

進位制

  • 二進位制:0b開頭
  • 八進位制:0開頭
  • 十六進位制:0x開頭

十進位制轉二進位制

二進位制轉十進位制

轉換關係

  • 8 bit = 1 Byte

  • 1024 Byte = 1 KB

  • 1024 KB = 1 MB

  • 1024 MB = 1 GB

  • 1024 GB = 1 TB

基本資料型別轉換

自動轉換

強制型別轉換

int i = (int)3.14;//強制型別轉換,損失精度

double d = 1.2;
int num = (int)d;//損失精度

int i = 200;
byte b = (byte)i;//溢位

特殊型別的強制轉換

  • 任意資料型別的資料與String型別進行“+”運算時,結果一定是String型別

    System.out.println("" + 1 + 2);//12
    
  • 但是String型別不能通過強制型別()轉換,轉為其他的型別

    String str = "123";
    int num = (int)str;//錯誤的
    int num = Integer.parseInt(str);//藉助包裝類的方法才能轉
    

運算子

+ 的兩種用法

  • 兩邊都是數值的話,+就是加法的意思
  • 兩邊至少有一邊是字串得話,+就是拼接的意思

i++ 和 ++i

  • ++在前,先自加,後使用
  • ++在後,先使用,後自加

案例1

public class LogicExer1{
	public static void main(String[] args){
		int x = 1;
		int y = 1;

		//x==2 ,x++  false  x = 2 左邊為false
		//右邊繼續
		//++y  y==2  y=2  y==2成立  右邊為true
		//false & true 結果false
		if(x++==2 & ++y==2){
			x =7;
		}
		System.out.println("x="+x+",y="+y);//x=2,y=2
	}
}
public class LogicExer2{
	public static void main(String[] args){
		int x = 1,y = 1;

		//x==2,x++  左邊條件為false,x=2
		//因為短路與,右邊不算
		//false && ? 結果是false
		if(x++==2 && ++y==2){
			x =7;
		}
		System.out.println("x="+x+",y="+y);//x=2,y=1
	}
}
public class LogicExer3{
	public static void main(String[] args){
		int x = 1,y = 1;

		//x==1,x++  左邊為true,x=2
		//因為是邏輯與,右邊繼續  
		//++y, y==1  y=2 右邊為false
		//條件true | false,最終為true
		if(x++==1 | ++y==1){
			x =7;
		}
		System.out.println("x="+x+",y="+y);//x=7,y=2
	}
}	
public class LogicExer4{
	public static void main(String[] args){
		int x = 1,y = 1;

		//x==1,x++  左邊為true,x=2
		//因為是短路或,左邊為true,右邊就不看了
		//整個條件為true
		if(x++==1 || ++y==1){
			x =7;
		}
		System.out.println("x="+x+",y="+y);//x=7,y=1

	}
}

案例2

public class LogicExer5{
	public static void main (String []  args)  {
		boolean x = true;
		boolean y = false;
		short z = 42;
        
		if((z++==42)&&(y==true))	z++; //z=43
	
		if((x=false) || (++z==45))  z++; //false || false = false

		System. out.println("z="+z);//44
	}
}
class  Test4_2  {
	public static void main (String []  args)  {
		boolean x = true;
		boolean y = true;
		short z = 42;
		
		if(y=true){
			if((z++==42)&&(y==true)){
				z++; //z=44
			}
		}
		if((x=false) || (++z==45)){
			z++;
		}
		System. out.println("z="+z);//46
	}
}

案例:獲取一個四位數的個、十、百、千位

public class Test01 {
	public static void main (String [] args) {
		int num = 1234;
		int ge = num % 10;
		int shi = num / 10 % 10;
		int bai = num / 100 % 10;
		int qian = num / 1000 % 10;
		
		System.out.println(num + "個位上的數字是:" + ge);
		System.out.println(num + "十位上的數字是:" + shi);
		System.out.println(num + "百位上的數字是:" + bai);
		System.out.println(num + "千位上的數字是:" + qian);
	}
}

&& 和 &

  • 左邊為false,則右邊就不看

|| 和 |

  • 左邊為true,則右邊就不看

'^'(異或)

  • 相同為false,不同為true

'<<'

  • 左移幾位就相當於乘以2的幾次方

'>>'

  • 右移幾位就相當於除以2的幾次方

'>>>'

  • 往右移動後,左邊空出來的位直接補0,不看符號位

三元運算子

public static void main(String[] args) {
    int i = (1==2 ? 100 : 200);
    System.out.println(i);//200
    int j = (3<=4 ? 500 : 600);
    System.out.println(j);//500
}