Write in front: Hello everyone, I'm a rookie xiaopang p. I haven't seen you for a long time. I recently finished learning Java syntax and accumulated my notes locally. I will summarize these Java syntax notes and publish them to csdn. Limited ability, where there are mistakes, welcome to correct! About the blog content, if you don't understand, you can also leave a message in the comment area. You don't know much, but you don't say anything.
First acquaintance with Java
First java program
The file name is HelloWorld java
public class HelloWorld { public static void main(String[] args) { System.out.println("HelloWorld"); } }
-
There can only be one public class in a java file, and the class name should be the same as the file.
-
Class exists in the source file; Methods exist in classes; Statement exists in method.
-
The class name shall be named in the form of large hump (all initials are capitalized).
How to run a java program
The general process is that we have written it java files (source files) are compiled by the compiler into The class file (bytecode) is then loaded into memory by the class loader of the JVM, and some verification is done through the bytecode verifier. After the verification is passed, the interpreter interprets the bytecode file into machine instructions that can be recognized by the computer.
JDK Java development environment; JRE Java running environment; JVM Java virtual machine
Observe the following code and execution results:
class zzz { public static void main(String[] args) { System.out.println("zzz"); } } public class HelloWorld { public static void main(String[] args) { System.out.println("HelloWorld"); } }
In Java, a class generates a bytecode file. The code above has two classes, so two bytecode files (. class) are generated
Why is this set? For ease of use, if you need the HelloWorld class, load the corresponding bytecode file.
data type
Variables and types
Memory is divided into internal memory and external memory, and variables are stored in internal memory.
variable
plastic
int i=10; //Define an integer variable System.out.println(i);
An int variable takes up 4 bytes.
What are bytes?
Bytes are the basic unit of space in a computer Computers use binary to represent data We consider eight bits as one byte Our computer usually has 8GB memory, which means 8G bytes Where 1KB = 1024 Byte, 1MB = 1024 KB, 1GB = 1024 MB So 8GB is equivalent to more than 8 billion bytes
System.out.println(Integer.MAX_VALUE); // 2147483647 System.out.println(Integer.MIN_VALUE); // -2147483648
The data range represented by 4 bytes is - 2 ^ 31 - > 2 ^ 31-1, which is about - 2.1 billion to + 2.1 billion
int maxValue = Integer.MAX_VALUE; System.out.println(maxValue+1); // -2147483648 int minValue = Integer.MIN_VALUE; System.out.println(minValue-1); // 2147483647
If the result of the operation exceeds the maximum range of int, overflow will occur.
Long integer
long takes up 8 bytes, and the value range is - 2 ^ 63 - > 2 ^ 63-1
long num=10L; System.out.println(num); System.out.println(Long.MAX_VALUE); System.out.println(Long.MIN_VALUE);
When initializing long integer variables, remember to add L after the number
Double precision floating point
double takes up 8 bytes. The storage of floating-point numbers in memory is different from shaping. Its value range cannot be simply represented by 2^n, and there will be precision errors in the storage of floating-point numbers.
double num = 1.1; System.out.println(num * num) // 1.2100000000000002
Single precision floating point
float takes up 4 bytes. f should be added after the number during initialization
float num = 1.0f; // Writing 1.0F is also OK System.out.println(num);
character
char occupies 2 bytes in java
char ch='A';
- Java uses single quotation marks + single letters to represent character literals
- A character in a computer is essentially an integer ASCII is used to represent characters in C language, while Unicode is used to represent characters in Java Therefore, a character occupies two bytes and represents more types of characters, including Chinese
- When using a Chinese character to represent a character, the error is to use javac -encoding UTF-8 file name java
Byte type
Byte takes up 1 byte, and the range size is - 128 – > 127
The addition and subtraction of byte type will carry out shaping and promotion, which should be saved with int.
Short
short takes up 2 bytes. The range size is - 2 ^ 15 - > 2 ^ 15-1. The range is small and generally not used.
Boolean
boolean has only two values, true or false. boolean and integer in Java cannot be converted to each other, and boolean cannot be represented by 1 or 0.
String type
String takes up 8 bytes. Java uses double quotation marks + several characters to represent the literal value of the string.
String s = "Fat tiger";
Use + to splice strings. When splicing any type of variable with strings, it will be converted into strings.
int a=20; int b=21; System.out.println(""+a+b); //2021
summary
constant
final int a = 10; a = 20; // Compilation error Tip: unable to assign value to final variable a
Constants cannot be modified while the program is running.
Type conversion
- Assignment between variables of different numeric types indicates that types with smaller range can be implicitly converted to types with larger range
- If you need to assign a type with a large range to a type with a small range, you need to cast the type, but the precision may be lost. Unrelated types cannot be type converted.
- When assigning a literal constant, Java will automatically check the range of numbers
summary
operator
Arithmetic operator
+-*/%
System.out.println(5/2); // 2 System.out.println(5.0/2); //2.5 System.out.println((double)5/2); //2.5 System.out.println((double)(5/2)); // 2.0
System.out.println(5%2); // 1 System.out.println(-5%2); //-1 System.out.println(5%-2); //1 System.out.println(-5%-2); //-1
In Java, you can modulo not only int, but also double.
It is worth noting that i=10 in the figure below, while c language is 11.
Relational operator
== != < > <= >=
The return values of relational operator expressions are Boolean values.
Logical operator
&& || !
In Java, logic is not! Can only act on Boolean values
int a=10; int b=20; System.out.println(!a < b);
Bitwise Operators
& | ~ ^
Shift Operators
>> << >>>
Unsigned shift right > > >: the rightmost bit is not needed, and the leftmost bit is filled with 0
int a = 4; System.out.printf("%x\n", a >>> 1);//2
- Shift left by 1 bit, equivalent to the original number * 2 Shift N bits to the left, which is equivalent to the nth power of the original number * 2
- Shift 1 bit to the right, equivalent to the original number / 2 Shift N bits to the right, which is equivalent to the nth power of the original number / 2
- Because the shift efficiency of computer calculation is higher than that of multiplication and division, shift operation can be used instead when a code is exactly multiplied and divided to the nth power of 2