Java: basic syntax, java basic syntax
Java is composed of classes and objects. Its objects and classes are composed of variables and methods, and the methods also contain statements and expressions.
1. Variables
Java provides two variables: member variables and local variables.
- Member variables: they are declared and defined in the class of the method in vitro and can be automatically initialized.
- Local variable: it is declared and defined in the method. It cannot be automatically initialized. After the method is executed, the local variable does not exist.
In Java, you must create a variable before using any variable. Creating a variable is actually a declaration of the variable. You must specify the variable type and name.
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 }
The automatic initialization value corresponding to the member variable:
| Type Variable |
Initial Value |
| Byte |
0 |
| Integer int |
0 |
| Single precision fload |
0.0f |
| Character char |
'\ U000000' |
| String |
Null |
| Short |
0 |
| Long Integer |
0L |
| Double Type |
0.0d |
| Boolean |
False |
2. 4 Basic Data Types
Java data type:
- Boolean
- Integer: integer, short, long, and byte
- Character Type
- Floating point type: single precision type, double precision type
String is not a basic data type. The variable defined by the String class is an object, not a simple type. Different from simple types, class objects contain their own methods and are complex types.
Boolean is used for logical condition judgment. It contains only two values, true and false ). In C, values 1 and true are equivalent. values 0 and false are equivalent. However, in Java, values of boolean variables can only be true or false.
3. Arithmetic Operators
In the modulo operation (%), if the operand contains positive and negative numbers, the positive and negative numbers of the result are consistent with those of the left operand. Example:-8% 3 =-2, 8% (-3) = 2
4. switch statement
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!
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!
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!
Note: If you have already entered a case Branch and the case statement does not use break, you can directly access each case in the future without matching.
5. Practice
- Use the for loop to implement ~ Summation of odd numbers between 99
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); }}
- Combine switch statements with for Loops
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
- Use multiple for loops to draw a right triangle with "*" and display it in the message dialog box.
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); }}
- Use "*" to output a diamond and display it
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(); } }}