Basic grammar
Common naming conventions
- Package name: Use lowercase letters xxXYYYZZz for multiple words
- Class name, interface name: Uppercase XxxYyyZzz (big hump)
- Variable name, method name: When multiple words are formed, the first word starts with lower case and each following word starts with upper case xxxYyyZzz
- Constant name: all letters are capitalized and multiple words are connected by _
variable
The data type
Eight basic data types +
Automatic type promotion
A byte, char, or short operation results in an int. For example, short+char or short+short requires an int
Cast casting
Reverse operation of automatic type promotion to change a large type to a small type
Long
You need to add an L after the number to store larger numbers. Float, again, you have to add F
char
Char is converted to int for addition
string
It is a reference data type and can only be concatenated. A string concatenated to any primitive data type results in a string
Scanner
import java.util.Scanner Scanner scanner=new Scanner(System.in); Int num= scanner.nextint () double num=scanner.nextDouble() String s=scanner.next() char c= s.charat (0)Copy the code
Loop label
Before a loop, you can use break or continue to specify that a level of loop is skipped
An array of
Declaration and initialization of one-dimensional arrays
- Static initialization
Int [] array = new int [] {2} 1001100;Copy the code
- Dynamic initialization
String[] array=new String[5];
Copy the code
- The length of the array
array.length
Copy the code
- Default initialization values for array elements
Integer arrays: 0: byte, short, int, and long Floating point arrays: 0.0: float, double: ASCII 0: char Boolean array: false: Boolean String array: null: String
Declaration and initialization of multidimensional arrays
- Static initialization
Int [] [] array = new int [] [] {{1, 2}, {3, 4}}Copy the code
- Dynamic initialization
int[][] array=new int[2][3]
int[][] array=new int[2][]
array[0]=new int[3]
array[1]=new int [4]
Copy the code
- The length of the array
Int [][] array=new int[2][3] array.length// is 2 aray[0]. Length // is 3Copy the code
- Multi-dimensional array traversal
for(int i=0; i<array.length; i++){ for(int j=0; j<array[i].length; j++){ System.out.println(array[i][j]); }}Copy the code
The Arrays tools
- Call way
Int a [] = new int [] {1, 2, 3}; Int [b] = new int [] {4 and 6}; Arrays.equals(a,b); Int [c] = new int [] {1, 2, 3}; System.out.println(Arrays.toString(a));Copy the code