Java語言是由類和對象組成的,其對象和類又是由變量和方法組成,而方法,又包含了語句和表達式。java
1. 變量oop
Java語言提供了兩種變量:成員變量和局部變量spa
在Java中,使用任何變量以前都須要對變量進行建立,建立變量實際上就是對變量的聲明過程,須要指明變量類型和變量名。3d
1 int a; 2 boolean b; 3 char c = 'A';
1 public class DataDemo { 2 3 int a; 4 public void test() 5 { 6 boolean b=false; 7 char c='\0'; 8 } 9 10 public static void main(String[] args) { 11 float f=0; 12 String s=null; 13 14 } 15 16 }
成員變量對應的自動初始化值:code
類型變量 | 初始值 |
字節型 byte | 0 |
整型 int | 0 |
單精度型 fload | 0.0f |
字符型 char | '\u0000' |
字符串型 String | null |
短整型 short | 0 |
長整型 long | 0L |
雙精度型 double | 0.0d |
布爾型 boolean | false |
2. 4類基本數據類型對象
Java數據類型:blog
String不是基本數據類型。String類所定義的變量是一個對象,而不是簡單類型。與簡單類型不一樣,類的對象含有本身的方法,是複雜類型。ip
布爾型(boolean),用於邏輯條件判斷,只含兩個值,真(true)、假(false)。須要注意的是,在C語言中,1和true等價,0和false等價,但在Java中,boolean變量的取值只多是true或false。字符串
3. 算數運算符it
取模運算(%)中,若操做數包含正負數,則結果的正負號與左操做數一致。 例如: -8%3=-2, 8%(-3)=2
4. switch語句
public class SwitchTest { public static void main(String[] args) { int student[] = {95, 85, 75, 65, 55}; for(int i=0; i<5; i++) { switch(student[i]/10) { case 9: System.out.println("Student" + i + "'s result is A!"); break; case 8: System.out.println("Student" + i + "'s result is B!"); break; case 7: System.out.println("Student" + i + "'s result is C!"); break; case 6: System.out.println("Student" + i + "'s result is D!"); break; default: System.out.println("Student" + i + "'s result is F!"); } } } }
Student0's result is A!
Student1's result is B!
Student2's result is C!
Student3's result is D!
Student4's result is F!
public class SwitchTest { public static void main(String[] args) { int student[] = {95, 85, 75, 65, 55}; for(int i=0; i<5; i++) { switch(student[i]/10) { case 9: System.out.println("Student" + i + "'s result is A!"); case 8: System.out.println("Student" + i + "'s result is B!"); case 7: System.out.println("Student" + i + "'s result is C!"); case 6: System.out.println("Student" + i + "'s result is D!"); default: System.out.println("Student" + i + "'s result is F!"); } } } }
Student0's result is A!
Student0's result is B!
Student0's result is C!
Student0's result is D!
Student0's result is F!
Student1's result is B!
Student1's result is C!
Student1's result is D!
Student1's result is F!
Student2's result is C!
Student2's result is D!
Student2's result is F!
Student3's result is D!
Student3's result is F!
Student4's result is F!
注意:當已經進入一個case分支,同時這個case語句中並無使用break,那麼之後的每一個case都不用匹配就能夠直接進入,知道遇到break爲止。
5. 實戰練習
import javax.swing.JOptionPane; public class OddSum { public static void main(String[] args) { int sum=0; for(int i=1; i<=99; i++) { if(i%2!=0) { sum=sum+i; } } JOptionPane.showMessageDialog(null, "The sum is "+ sum, "Total Even Integer from 1 to 99", JOptionPane.INFORMATION_MESSAGE); } }
public class TestSwitch { int i=0, w=0; public TestSwitch() { for(; i<=5; i++) { switch (i) { case 3: w+=1; case 0: w+=1; case 1: w+=1; continue; case 2: w+=1; case 4: w+=1; default: w+=2; } System.out.print(" "+w); } } public static void main(String[] args) { TestSwitch testSwitch=new TestSwitch(); } }
7 13 15
import javax.swing.JOptionPane; public class MultipleLoop1 { public static void main(String[] args) { String out=""; loop: for(int row=1; row<=5; row++) { out+="\n"; for(int column=1; column<+6; column++) { if(column>row) continue loop; out+="* "; } } JOptionPane.showMessageDialog(null, out, "Test multiply loop 1", JOptionPane.INFORMATION_MESSAGE); } }
public class MultiplyLoop2 { public static void main(String[] args) { MultiplyLoop2 multiplyLoop2=new MultiplyLoop2(); multiplyLoop2.print(11); } public void print(int n) { int temp=0; for(int i=0; i< n; i++) { for(int j=0; j<Math.abs(n/2-i);j++) { System.out.print(" "); } if(i<=n/2) { temp=i; } else { temp=n-i-1; } for(int k=0; k<(2*temp +1); k++) { System.out.print("*"); } System.out.println(); } } }