Summary of basic Java knowledge points I

Posted by lnenad on Sun, 16 Jan 2022 20:49:35 +0100

Understanding of Java loading and execution:

There are two very important stages of java programs:
Compile phase: the source file will be java becomes a bytecode file class (compiling is essentially checking whether the syntax is correct)
Run phase: the JVM will load the bytecode file and interpret the bytecode

Basic data type

Basic data types can be divided into 4 categories and 8 categories:
Type I: integer type
byte,short,int,long (without decimal)
Type II: floating point type
float,double (with decimal)
The third category: Boolean
boolean: there are only two values: true and false. True means true and false means false
Type IV: character type
char: the character literal specified in java must be enclosed in single quotes. It belongs to text.

Take a comprehensive look, what rules should be followed in type conversion?

Article 1: among the eight basic data types, except the boolean type, the other seven types can be converted
Perform conversion;

Article 2: if the integer literal does not exceed the value range of byte, short and char, it can be assigned directly
Values are given to variables of type byte, short and char;

Article 3: the conversion from small capacity to large capacity is called automatic type conversion, and the order of capacity from small to large is:
Byte < short (char) < int < long < float < double, where short and char
Both occupy two bytes, but char can represent a larger positive integer;

Article 4: conversion from large capacity to small capacity is called forced type conversion, and "forced type converter" must be added when writing,
However, precision loss may occur during operation, so it should be used with caution;

Article 5: when performing mixed operations of byte, short and char types, first convert them into int types and then perform operations;

Article 6: for mixed operation of multiple data types, convert them to the one with the largest capacity before operation;

`All written test questions can not exceed the above six rules. Memorize by rote.

Small grammar

The class body can only be composed of attributes and methods, not direct java statements (class = attribute + method)

When the JVM executes the program, it will take the initiative to find the main() method. Without this method, the program cannot be executed.

1,This content has no reason. It can only be tested and memorized according to the test results.
2,First conclusion?
	One java Multiple can be defined in the source file class. 

3,Second conclusion?
	public Class is not required. No.

4,The third conclusion?
	There is only one in the source file class The definition of, then it is bound to generate a corresponding one class File.

5,Fourth conclusion?
	public You can have no classes, but if you have one, public The modified class name must be consistent with the source file name.

6,Fifth conclusion?
	public There can only be one class.
7,Sixth conclusion?
	One java Duplicate classes cannot exist in the source file 

identifier

What can an identifier identify?
Can identify:
Class name, interface name
Method name
Variable name, constant name

1. Identifier naming rules

	Rule 1: identifiers can only consist of numbers, letters (including Chinese) and underscores_,Dollar sign $form,
	Cannot contain other symbols.

	Rule 2: identifiers cannot start with numbers

	Rule 3: keyword cannot be used as identifier. For example: public class static void These blue fonts
	All are keywords. Keywords cannot be used as identifiers.

	Rule 4: identifiers are strictly case sensitive. Capitalize A And lowercase a dissimilarity.

	Rule 5: identifiers are theoretically unlimited in length.

2. Identifier naming convention

		Specification 1: see the name to know the meaning (when this identifier is named, it's best to know what it means by looking at this word.)

		Specification 2: follow the hump naming method. What is a hump (one high and one low, one high and one low)...)
			Humps are good for separating words from each other
			BiaoShiFuTest,This is very good. You can see four words at a glance.

		Specification 3: there are special requirements for class name and interface name
			Class name and interface name are capitalized, and each subsequent word is capitalized.
				StudentTest,UserTest ,This is the class name and interface name.

		Specification 4: there are special requirements for variable name and method name
			Variable and method names start with lowercase letters, followed by each word with uppercase letters.
				nianLing(NianLing That doesn't fit.)
				mingZi(MingZi This is not consistent.)
		
		Specification 5: all "constant" names are capitalized and underlined between words.
			USER_AGE : User age
			MATH_PI: Fixed constant 3.1415926.....

variable

Variable: a variable is actually the most basic unit for storing data in memory. A variable is a box that stores data.

The java virtual machine allocates memory space to variables according to their types

What's the use of data types?
Remember: different data types allocate different amounts of space in memory.
In other words, how much space the Java virtual machine allocates to this data mainly depends on the data type of this variable.
Allocate different sizes of space according to different types.

Note: in java In language, "data" is called "literal".
		10
		1.23
		true
		false
		'a'
		"abc"
		These are data, which are called "literal" in the program.

		Literal quantity can be divided into many types:
			Integer literal: 1 2 3 100 -100 -20 ....
			Floating point font: 1.3 1.2 3.14.....
			Boolean literal: true,false There are no other values, indicating true and false,true It means true, false Indicate false

			Character type literal:'a','b','in'
			String literal:"abc","a","b","China"
	public class VarTest06{
	public static void main(String[] args){
		// Declare three variables, all of type int, named a,b,c
		// Through the test, it is concluded that a and B are not assigned, and c is assigned 100
		int a, b, c = 100;
	// Variables must be declared and assigned before they can be accessed. Have the two variables A and B been assigned?
	//Error: variable a may not have been initialized
	//System.out.println(a);
	//Error: variable b may not have been initialized
	//System.out.println(b);
	System.out.println(c);
	// Assign a value
	a = 200;
	// Assign a value to b
	b = 300;
	System.out.println(a);
	System.out.println(b);
	}
}

1. A classification of variables
Variables are divided according to where they appear:
In the body of a method variable declaration.
Outside the method body, variables declared in the class body: member variables (divided into instance variables and static variables).

2. Note: local variables are only valid in the method body. After the execution of the method body, the memory of the variable is released.

3. Scope of variable
1. What is scope?
The valid range of the variable.
2. As for the scope of variables, you can remember one sentence:
We don't know each other without braces. (memorize this sentence.)
3. There is a very important principle in java:
Proximity principle. (not only in java, but also in other programming languages.)
Visit whichever is near me.

data type

There is a very important conclusion in java. You must remember:
In any case * *, integer literal / data is treated as int by default * *. (just remember)
If you want the integer literal to be treated as a long type, you need to add L/l after the literal
It is recommended to use uppercase L, because lowercase L and 1 are indistinguishable.

// Title:
		// OK? Is there a type conversion?
		// 2147483647 is treated as int by default
		// d variable is of long type. Small capacity can be automatically assigned to large capacity, and automatic type conversion
		long d = 2147483647; // 2147483647 is the int maximum.
		System.out.println(d);

		// Compiler error
		// In java, as soon as an integer literal comes up, the compiler will regard it as an int type
		// 2147483648 is out of the range of int, so an error occurs before there is no assignment.
		// Remember, it's not that e can't hold 2147483648. E is a long type, which can hold 2147483648
		// Only 2147483648 itself is beyond the int range.
		// Error: integer too large
		//long e = 2147483648;

		// How to solve this problem?
		long e = 2147483648L;
		System.out.println(e);

1. Small capacity can be directly assigned to large capacity, which is called automatic type conversion.

2. Large capacity cannot be directly assigned to small capacity, and forced conversion is required.
However, it should be noted that after strengthening the type converter, although the compilation passes, it runs
Accuracy may be lost when.

3. There is a syntax rule in java:
When the integer literal does not exceed the value range of byte, this
Integer literals can be directly assigned to byte type variables.

4. byte, char and short are converted to int before mixed operation.

5. When multiple data types perform mixed operations, the final result type is the type corresponding to "maximum capacity". Except char+short+byte. Because when char+short+byte is mixed, they will be converted to int before operation.

6. Escape character \ t, \ n\

// 0 if you add single quotation marks, 0 is not the number 0, it is the text 0, it is 1 character.
		char c3 = '0';
		System.out.println(c3);

		// Error: incompatible type: String cannot be converted to char
		//char c4 = "a";

		// Error: open character text
		//char c5 = 'ab';

		// According to what we have learned before, the following code should report an error.
		// After testing, the following code is actually 1 character and is not a string
		// Two characters together represent one character, where \ t represents "tab"
		char c2 = '\t'; //Equivalent to the tab key on the keyboard

7,char x = 97;
This java statement is allowed, and the output result is' a '
After this test, two conclusions are drawn:
The first conclusion: when an integer is assigned to a char type variable, it will be automatically converted to char type,
The end result is a character

The second conclusion: when an integer does not exceed the value range of byte short char,
This integer can be directly assigned to a variable of type byte short char.

8. Floating point data is double by default, and f is added after float data

operator

Difference between basic assignment operator and extended assignment operator

//Conclusion: x += 1 is different from x = x + 1.
// Research:
		// Are i += 10 and i = i + 10 really exactly the same?
		// In fact, the answer is not exactly the same.
		byte x = 100; // 100 does not exceed the value range of byte type, and can be assigned directly
		System.out.println(x); // 100

		// Analysis: can this code be compiled?
		// Error: incompatible type: conversion from int to byte may be lost
		//x = x + 1; //  The compiler has detected that x + 1 is of type int. can int be directly assigned to variable X of type byte?

		// Can I use the extended assignment operator?
		// Yes, so it is concluded that x += 1 is different from x = x + 1.
		x += 1;// In fact, x += 1 is equivalent to: x = (byte)(x + 1);
		System.out.println(x); // 101

		// The value range of byte has long been exceeded.
		x += 199; // x = (byte)(x + 199);
		System.out.println(x); // 44 (of course, the accuracy will be lost automatically.)
  • Operator:
    1. The + operator has two functions in the java language.
    Function 1: Sum
    Function 2: String splicing

    2. When to sum? When to splice strings?
    When both sides of the + operator are numeric types, sum.
    When "any side" on both sides of the + operator is a string type, this + will perform string splicing.

    3. Be sure to remember that the result of string splicing is still a string.

    Control statement

Switch (value){
case value 1:
java statement;
java statement
break;
case value 2:
java statement;
java statement
break;
case value 3:
java statement;
java statement
break;
default:
java statement;
}
The above is a complete switch statement:
Including: break; Statement is not required. The default branch is not required.

	switch What values are supported by the statement?
		support int Type and String Type.
		But be careful JDK Version of, JDK8 Not previously supported String Type, only supported int. 
		stay JDK8 After that, switch Statement start support string String Type.

		switch Statements are essentially only supported int and String,however byte,short,char it's fine too
		Use in switch In the statement, because byte short char Automatic type conversion is possible.

		switch It will be used when comparing "value" with "value 1" and "value 2" in the statement“=="Compare.
for(;;){
			System.out.println("Dead cycle");
		}

// The scope of the i variable is expanded.
		int i = 0;
		for(;i < 10;i++){
			System.out.println("i --> " + i); // 0 1 2 3....9
		}

// deformation
		for(int k = 1; k <= 10; ){
			System.out.println("k --> " + k); 
			k++;
		}


Topics: Java Programming JDK javac