1. 程式人生 > >黑馬程式設計師——java面試題之基礎語法

黑馬程式設計師——java面試題之基礎語法

型別轉化

		float f1 = (float)12.345;
		float f2 = 12.345f;
		
		f1其實是通過一個double型別轉換過來的。
		而f2本身就是一個float型別。
/*
	面試題:
		byte b1=3,b2=4,b;
		b=b1+b2;
		b=3+4;
		哪句是編譯失敗的呢?為什麼呢?
		b = b1 + b2;是有問題的。
		因為變數相加,會首先看型別問題,最終把結果賦值的也會考慮型別問題。
		常量相加,首先做加法,然後看結果是否在賦值的資料類型範圍內,如果不是,才報錯。
*/
class DataTypeDemo6 {
	public static void main(String[] args) {
		//定義了三個byte型別的變數,b1,b2,b3
		//b1的值是3,b2的值是4,b沒有值
		byte b1 = 3,b2 = 4,b;
		
		//b = b1 + b2; //這個是型別提升,所有有問題
		
		b = 3 + 4; //常量,先把結果計算出來,然後看是否在byte的範圍內,如果在就不報錯。
	}
}
/*
	byte b = 130;有沒有問題?如果我想讓賦值正確,可以怎麼做?結果是多少呢?
	
	練習:byte b = (byte)300;
*/
class DataTypeDemo7 {
	public static void main(String[] args) {
		//因為byte的範圍是:-128到127。
		//而130不在此範圍內,所以報錯。
		//byte b = 130; 
		
		//我們可以使用強制型別轉換
		byte b = (byte) 130;
		
		//結果是多少呢?
		System.out.println(b);//-126
	}
}
/*
	分析過程:
		我們要想知道結果是什麼,就應該知道是如何進行計算的。
		而我們又知道計算機中資料的運算都是補碼進行的。
		而要得到補碼,首先要計算出資料的二進位制。
		
		A:獲取130這個資料的二進位制。
			00000000 00000000 00000000 10000010
			這是130的原碼,也是反碼,還是補碼。
		B:做擷取操作,截成byte型別的了。
			10000010 
			這個結果是補碼。
		C:已知補碼求原碼。
					符號位		數值位
			補碼:	1			0000010
			
			反碼:	1			0000001
			
			原碼:	1			1111110
*/
class DataTypeDemo8 {
	public static void main(String[] args) {
		//直接輸出一個字元
		System.out.println('a'); //a
		//輸出一個字元和一個整數做加法
		System.out.println('a'+1); //98
	}
}
class DataTypeDemo9 {
	public static void main(String[] args) {
		System.out.println("hello"+'a'+1); //helloa1
		System.out.println('a'+1+"hello"); //98hello
		
		System.out.println("5+5="+5+5); //5+5=55
		System.out.println(5+5+"=5+5"); //10=5+5
	}
}

算術運算

class OperatorTest {
	public static void main(String[] args) {
		int a = 10;
		int b = 10;
		int c = 10;

		a = b++; //a=10,b=11,c=10
		c = --a; //a=9,b=11,c=9
		b = ++a; //a=10,b=10,c=9
		a = c--; //a=9,b=10,c=8
		
		System.out.println("a:"+a);
		System.out.println("b:"+b);
		System.out.println("c:"+c);
		System.out.println("--------------");
		
		int x = 4;
		int y = (x++)+(++x)+(x*10);
		//4+6+60
		//x=5,6
		
		System.out.println("x:"+x);
		System.out.println("y:"+y);
	}
}
賦值運算
/*
	面試題:
		short s=1;s = s+1; 
		
		short s=1;s+=1;
		上面兩個程式碼有沒有問題,如果有,那裡有問題。
		
		為什麼第二個木有問題呢?
			擴充套件的賦值運算子其實隱含了一個強制型別轉換。
			
			s += 1;
			不是等價於 s = s + 1;
			而是等價於 s = (s的資料型別)(s + 1);
*/
class OperatorTest {
	public static void main(String[] args) {
		//short s = 1;
		//s = s + 1;
		//System.out.println(s);
		
		short s = 1;
		s += 1; //好像是 s = s + 1;
		System.out.println(s);
	}
}
位運算子
/*
	<<:左移	左邊最高位丟棄,右邊補齊0
	>>:右移	最高位是0,左邊補齊0;最高為是1,左邊補齊1
	>>>:無符號右移 無論最高位是0還是1,左邊補齊0
	
	面試題:
		請用最有效率的方式寫出計算2乘以8的結果?
			2 * 8
			
			2 << 3

*/
class OperatorDemo3 {
	public static void main(String[] args) {
		//<< 把<<左邊的資料乘以2的移動次冪
		System.out.println(3 << 2); //3*2^2 = 3*4 = 12;
	
		//>> 把>>左邊的資料除以2的移動次冪
		System.out.println(24 >> 2); //24 / 2^2 = 24 / 4 = 6
		System.out.println(24 >>> 2);
		
		System.out.println(-24 >> 2); 
		System.out.println(-24 >>> 2);
	}
}
/*
	計算出3的二進位制:11
		00000000 00000000 00000000 00000011
	(00)000000 00000000 00000000 0000001100
		
	>>的移動:	
	計算出24的二進位制:11000
		原碼:10000000 00000000 00000000 00011000
		反碼:11111111 11111111 11111111 11100111
		補碼:11111111 11111111 11111111 11101000
		
		11111111 11111111 11111111 11101000
		1111111111 11111111 11111111 111010(00) 補碼
		
		補碼:1111111111 11111111 11111111 111010
		反碼:1111111111 11111111 11111111 111001
		原碼:1000000000 00000000 00000000 000110
		
		結果:-6
		
	>>>的移動:
		計算出24的二進位制:11000
		原碼:10000000 00000000 00000000 00011000
		反碼:11111111 11111111 11111111 11100111
		補碼:11111111 11111111 11111111 11101000
		
		11111111 11111111 11111111 11101000
		0011111111 11111111 11111111 111010(00)
		
		結果:
*/
迴圈語句標籤的使用
/*
	控制跳轉語句:
		break:中斷
		continue:繼續
		return:返回
	
	break:中斷的意思
	使用場景:
		A:switch語句中
		B:迴圈語句中。
			(迴圈語句中加入了if判斷的情況)
		注意:離開上面的兩個場景,無意義。
		
	如何使用呢?
		A:跳出單層迴圈
		B:跳出多層迴圈
			要想實現這個效果,就必須知道一個東西。帶標籤的語句。
			格式:
				標籤名: 語句
*/
class BreakDemo {
	public static void main(String[] args) {
		//在 switch 或 loop 外部中斷
		//break;
		
		//跳出單層迴圈
		for(int x=0; x<10; x++) {
			if(x == 3) {
				break;
			}
			System.out.println("HelloWorld");
		}
		
		System.out.println("over");
		System.out.println("-------------");
		
		wc:for(int x=0; x<3; x++) {
			nc:for(int y=0; y<4; y++) {
				if(y == 2) {
					//break nc;
					break wc;
				}
				System.out.print("*");
			}
			System.out.println();
		}
	}
}
陣列
/*
	陣列操作的兩個常見小問題:
		ArrayIndexOutOfBoundsException:陣列索引越界異常
			原因:你訪問了不存在的索引。
		
		NullPointerException:空指標異常
			原因:陣列已經不在指向堆記憶體了。而你還用陣列名去訪問元素。
			
*/
引數傳遞
/*
	思考題:看程式寫結果,然後分析為什麼是這個樣子的。並畫圖講解。最後總結Java中引數傳遞規律。
	
	Java中的引數傳遞問題:
		基本型別:形式引數的改變對實際引數沒有影響。
		引用型別:形式引數的改變直接影響實際引數。
*/
class ArgsDemo {
	public static void main(String[] args) {
		int a = 10;
		int b = 20;
		System.out.println("a:"+a+",b:"+b); //a:10,b:20
		change(a,b);
		System.out.println("a:"+a+",b:"+b); //???	a:10,b:20

		int[] arr = {1,2,3,4,5}; 
		change(arr);
		System.out.println(arr[1]); //???	4
	}

	public static void change(int a,int b) { //a=10,b=20
		System.out.println("a:"+a+",b:"+b); //a:10,b:20
		a = b;	//a=20
		b = a + b; //b=40
		System.out.println("a:"+a+",b:"+b); //a:20,b:40
	}

	public static void change(int[] arr) { //arr={1,2,3,4,5};
		for(int x=0; x<arr.length; x++) {
			if(arr[x]%2==0) {
				arr[x]*=2;
			}
		}
		//arr={1,4,3,8,5};
	}
}