Overview of Java
Configure environment variables
The purpose of configuring environment variables is to allow command lines and development tools to find java, javac and other commands in the JDK bin directory. After configuration is complete, you can execute these commands in any directory.

Add the JDK’s bin directory to the system’s Path environment variable and move it to the appropriate location. After saving the configuration, you need to reopen the command-line window.

You can check whether the configuration was successful by executing the following command:
java -version
javac -version
Writing the first Java program
Step 1: Create source files
Create a HelloWorld.java file in the specified directory.
Step 2: Write code
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Step 3: Compile the source file
Go to the directory where the source file is located on the command line, and then execute:
javac HelloWorld.java

You can also write out the full path to the source file in the command.

If there is a problem with the file name, path, environment variables, or source code, the compiler will output an error message.

Step 4: Run the program
Execution after successful compilation:
java HelloWorld

Write the class name HelloWorld at runtime, not HelloWorld.class. The java command launches the class rather than directly executing the class file name on the disk.
Code structure description
class definition
public class HelloWorld {
}
public is an access permission modifier that means that this class can be accessed from other locations. class is used to define classes, and HelloWorld is the class name.
Note: For top-level classes modified by public, the class name must be consistent with the source file name.
A source file can define multiple top-level classes, but there can only be one public top-level class at most. In actual development, usually only one main class is defined in a file, which is easy to maintain.
identifier naming
Class names usually use the big hump nomenclature, such as HelloWorld; variable names and method names usually use the small hump nomenclature, such as studentName and getName().
Java identifiers can consist of letters, numbers, underscores, and dollar signs, but they cannot start with a number or be the same as keywords. Meaningful English names should be used first in actual development, and the dollar sign is not recommended at will.
main method
public static void main(String[] args) {
}
The main method is the entry point for ordinary Java applications. Only classes that need to be started directly must define the main method, and not every class must write it.
output statements
System.out.println("Hello World");
System.out represents the standard output stream and is output to the console by default. println() outputs the content and breaks the line, and print() does not break the line after outputting it.
The content in double quotes is a string. When using a plus sign, if both sides are numeric values, addition is performed; if either side is a string, string splicing is performed.
System.out.println(1 + 2);
System.out.println("结果:" + 1 + 2);
System.out.println("结果:" + (1 + 2));
Source files, compilers and editors should use UTF-8 coding uniformly as much as possible to avoid Chinese garbled coding.

binary
Common decimal system
Common bases in computers include binary, octal, decimal and hexadecimal.
In Java integer literals:
- Binary begins with
0bor0B. - Octal begins with
0. - Hexadecimal begins with
0xor0X.
int binary = 0b111001;
int octal = 071;
int decimal = 57;
int hexadecimal = 0x39;
The values stored in the above four variables are all decimal 57.
hexadecimal conversion
When a decimal integer is converted to another base, you can divide it continuously by the target base and record the remainder, and then arrange the remainder in reverse order.
When other decimal units are converted to decimal units, each digit can be multiplied by the corresponding weight and summed. For example, binary 00011001 corresponds to:
1 × 2^4 + 1 × 2^3 + 1 × 2^0 = 25
Storage of data in memory
Data in memory is ultimately stored in binary form. Common storage units are as follows:
1 bitrepresents a binary bit.1 byteequals8 bit.1 KBusually stands for1024 byte.
Storage of integers
Java signed integers are represented in two’s complement. Complement’s complement allows addition and subtraction to use unified hardware arithmetic rules.
Taking one byte as an example, the range that can be represented is -128 to 127; the signed integer range of two bytes is -2^15 to 2^15 - 1.
Storage of floating point numbers
Java’s float and double use the IEEE 754 floating point format. Floating-point numbers are represented by symbols, exponents, and significant digits, so the range is wide, but many decimal fractions cannot be accurately represented in binary.
System.out.println(0.1 + 0.2);
Scenarios involving amounts that require accurate decimal calculation, such as amounts, should use BigDecimal instead of relying directly on float or double.
Pictures, audio and video
Pictures, audio and video are also stored in binary form. The encoder converts the original information into byte data in a specific format, and the decoder restores the content in the same format.
Character sets and character encoding
Unicode allocates unified code points to characters around the world; UTF-8 is a variable-length encoding method of Unicode. GBK is mainly used for Chinese character encoding.
The ASCII standard defines 128 characters, of which the encoded value of character A is 65, and the encoded value of character a is 97. Common encodings such as UTF-8 and GBK are compatible with the basic character range of ASCII.
Garbled code may occur when encoding and decoding use different character sets. The source files, configuration files and data transmission in the project should be as clear as possible and use UTF-8 uniformly.
Java annotations
single-line comments
// 单行注释
multiline comments
/*
* 多行注释
*/
documentation comments
/**
* 文档注释
*/
Document comments can be extracted by the javadoc tool and generated API documents.
IntelliJ IDEA Basic Operations
new construction
Java source files are usually created in the project. New projects can be built through File, New, and Project.
Common contents in the project catalog are as follows:
- The
.ideadirectory holds the engineering configuration of IntelliJ IDEA. - The
srcdirectory is usually used as the source code directory. - Compilation results are usually output to the
outdirectory or to the directory specified by the build tool.

In a simple IDEA project, the directory labeled Sources Root is used to hold the source code. The compiled class file is located in the output directory.

global settings
Through File and Settings, you can modify global settings such as themes and fonts.


project sets
Through File and Project Structure, you can set the JDK, language level and output directory used by the project.

Common shortcut keys
Alt + Enter: View error fixes or code operation suggestions.Ctrl + E: Open the list of recently used files.
If you enjoyed this, leave a comment~