Java---Chapter 1
A hello world and JDK, JRE, JVM
public class Hello{
public static void main(String[ ] args){
System.out.println("Hello,World");
}
}
explain:
- public class Hello—>Create a Hello class, which is a public public class
- public static void main(String[ ] args) represents a main method, which is the entry point of the program
- System.out.println("Hello, World") is the output statement
JDK=JRE+development tool set (for example: javac, java compilation tools, etc.)
JRE=JVM+Java SE standard library (java core class library)
double escape character
character | meaning |
---|---|
\t | A tab stop to implement the alignment function |
\n | line break |
|a\ | |
" | one" |
’ | one' |
\r | A carriage return System.out.println (overwrite text from the beginning of the line) |
practise:
code:
public class study {
public static void main(String[] args){
System.out.println("书名\t作者\t价格\t销量\n三国\t罗贯中\t120\t1000");
}
}
three notes
- Single-line comment
Basic format
// 单行注释
- Multi-line comments (do not allow nested multi-line comments)
basic format
/* 多行注释 */
- Document comment
Explanation: The content of the comment can be parsed by the tool javadoc provided by the JDK to generate a set of documentation for the program in the form of a web page file, usually written in the class
/**
* @author 汴京城下君
* @version 1.0
*/
public class study {
public static void main(String[] args){
System.out.println("文档注释");
}
}
After that, the corresponding files are generated in the temp folder of the D drive
Four code specification
- Annotations for classes and methods start withWriting in javadoc
- Non-javadoc comments, often for code maintainers, focusing on telling readers why they write this way, how to modify it, what problems to pay attention to, etc.
- useTab operation to achieve indentation, move to the right as a whole by default, or move to the left as a whole with shift+tab
- It is customary to add one to both sides of the operator and =space. For example: 2 + 4 * 5 +345 -89
- Source files useutf-8 encoding
- Line width should not exceed80 characters
- code writingSecondary styleandtail style
Secondary style
public class study
{
public static void main(String[] args)
{
}
}
tail style
public class study {
public static void main(String[] args){
}
}
Five DOS commands (understand)
Introduction:
Dos: Disk Operating Systemdisk operating system
relative path:
Start positioning from the current directory to form a path
Absolute path:
Start positioning from the top-level directory , forming a path
Principle:
Common Dos commands:
Check what dir is in the current directory
dir d:\abc2\test200- Switch to another disk: the drive letter cd (change directory)
is switched to the C drive, cd/D c: - Switch to another directory of the current disk
cd d:\abc2\test200 cd…\abc2\test200 - switch to previous level
cd... - Change to the root directory
cd\ - View all subdirectories under the specified directory
tree d:\abc2 - clear screen
cls - exit DOS
exit
six variables
Note on variables
- A variable represents a storage area in memory (different variables have different types and occupy different sizes of space)
- This area has its own name [variable name] and type [data type]
- Variables must be declared first and then used, that is, there is an order
- The area's data can vary continuously within the same type range
- Variables cannot have the same name in the same area
- Variable = variable name + value + data type (three elements of variable)
1. Use of the plus sign
- When the left and right sides are both numeric types, do the addition operation
- If one of the left and right sides is a string, the concatenation operation is performed
public class study {
public static void main(String[] args){
System.out.println(100+3+"90");
System.out.println("hello"+3+100);
}
}
output:
10390
hello3100
2. Data type
integer
nitty gritty:
- Each integer type in Java has a fixed range and field length, which is not affected by the specific OS (operating system) to ensure the portability of Java programs
- Java integersDefault is int type, to declare long constants must be followed by "l" or "L"
- The variables in the Java program are declared as int type, unless it is not enough to represent a large number, use long
- bit: The smallest storage unit in a computer.
- byte: The basic storage unit in a computer. 1byte=8bit
floating point
Java's floating-point type can represent a decimal, such as 1.2, etc.
nitty gritty:
- Similar to the integer type, the Java floating-point type also has a fixed range and field length, which is not affected by the specific OS
- Java's floating point typeThe default is double type, to declare a float type constant, add "f" or "F" after it
- Floating-point constants have two forms of expression
Decimal: such as: 5.12, 512.0f, 0.512, .123 (must have a decimal point)
Scientific notation: 5.12e2[ 5.12*10 to the 2nd power ], 5.12E-2[ 5.12/10 to the 2nd power ] - Normally, the double type should be used because it is more precise than the float type
- Gotcha: 2.7 and 8.1/3 comparison
8.1/3=2.69999999997
a decimal close to 2.7
If you need to compare, the correct code is as follows
public class study {
public static void main(String[] args){
double a = 2.7;
double b = 8.1/3;
if(Math.abs(a - b) < 0.00001) {
System.out.println("可以认为a和b相等");
}
}
}
output:
a and b can be considered equal
character type
character type can representsingle character, the character type is char, char is two bytes (can store Chinese characters)
Multiple characters we use String(String will be explained in detail later)
Code example:
public class study {
public static void main(String[] args){
char a1 = 'a';
char a2 = '\t';
char a3 = '汴';
char a4 = 97; //ASCII码值
System.out.println(a1); // a
System.out.println(a2); //
System.out.println(a3); // 汴
System.out.println(a4); //a
}
}
nitty gritty:
- Character constants are enclosed in single quotes (' '), for example: char c1 = 'a'; double quotes are strings
- Escape characters are also allowed in Java
- In Java, the essence of char is an integer, and when output, it is the character corresponding to the unicode code
- You can directly assign an integer to char, and then output according to the corresponding unicode character
- The char type can be operated, which is equivalent to an integer, because it has a corresponding unicode code
'a'+10----->107
URL of unicode encoding conversion tool
The nature of character types
- To store character types in the computer, you need to find out the ASCII code value corresponding to the character, such as 'a':
storage: 'a'–>code value 97—>binary—>storage
read: binary——>97— > 'a' --> show - The correspondence between characters and ASCII code values is specified in the character encoding table
Encoding format | meaning |
---|---|
ASCII | One byte representation, one is 128 characters . In fact, a byte can represent 256 characters, only 128 characters are used |
Unicode | Fixed-size encoding, using two bytes to represent characters, letters and Chinese characters take up two bytes, a bit of a waste of space |
utf-8 | Variable size encoding , characters use 1 byte, Chinese characters use 3 bytes |
GBK | It can represent Chinese characters, and has a wide range. Letters use 1 byte, and Chinese characters use 2 bytes. |
GB2312 | Can represent Chinese characters, GB2312<GBK |
BIG5 | Traditional Chinese (Taiwan, Hong Kong) |
ASCII code
In the 1990s, the United States specified a set of character encoding (using one byte), and made a unified regulation on the relationship between English characters and binary, which is called ASCII code. The ASCII code specifies the encoding of 128 characters , which only occupies the last seven bits of a byte, and the first bit is specified as 0.
Disadvantage:
cannot represent all characters
Unicode encoding
Benefits:
One encoding that incorporates all characters in the world, and each character is granted a unique encoding. There is no problem of garbled characters when using Unicode. Disadvantages
:
An English letter and Chinese characters both occupy 2 characters, which is a bit of a waste of storage space
The 16th power of 2 is 65536, so the maximum encoding is 65536 characters.
The characters encoding 0~127 are the same as ASCII codes
utf-8
It can be understood as an improved version of Unicode. It
is the most widely used Unicode implementation on the Internet.
UTF-8 is a variable-length encoding method. It can represent a symbol with 1 to 6 bytes. According to different symbols For codes with variable byte length and
variable size, letters occupy 1 byte, and Chinese characters occupy 3 bytes.
Boolean type
basic introduction:
- The Boolean type is also called the boolean type, which only allows the value true or false, without NULL
- This type occupies 1 character
- Suitable for logical operations, generally used for program flow control
if-else
while
do-while
for loop
0 or non-zero can not be used instead of true and false(Different from C language...)
automatic type conversion
Automatic Type Conversion Notes and Details
- When there are multiple types of data mixed operations, the system first automatically converts all data into the data type with the largest capacity, and then calculates
- When we assign data with high precision (capacity) to a data type with small precision (capacity), an error will be reported, otherwise, the type will be converted automatically
- (byte, short) and char will not be automatically converted to each other
- Byte, short, and char can be calculated, and the precision will be increased to int type first when calculating
- boolean does not participate in conversion
- Auto-promotion principle: The type of the expression result is automatically promoted to the largest type among the operands
cast
Introduction:
The inverse process of automatic type conversion, which converts a data type with a large capacity into a data type with a small capacity. When using it, you need to add a mandatory type conversion character (), but it may cause precision reduction or overflow.pay extra attention
public class study {
public static void main(String[] args){
int i = (int)1.9;
System.out.println("i="+i);// 1 造成精度损失
int n = 2000;
byte b = (byte)n;
System.out.println("b="+b);// -48 造成数据溢出
}
}
nitty gritty:
- When the size of the data, from large – > small, you need to use coercion
- The coercive symbol is only valid for the nearest operand, and parentheses are often used to increase the priority
//int y = (int)10*3.5+6.1*7;
int y = (int)(10*3.5+6.1*7);
System.out.println(y);
- The char type can save the constant value of int, but it cannot save the int value and needs to be converted
char c1 = 100;
int m = 100;
//char c2 = m; 报错
char c3 = (char)m;
System.out.println(c3);//输出100对应的字符
- Byte and short types are treated as int types when performing operations
String type
- Convert basic data type to String type
Basic data type + " "
int i = 10;
String n = i + "";
- Convert String type to basic data type
Call the parseXX method through the wrapper class of the basic data type ( detailed introduction when object-oriented )
String a = "123";
int num1 = Integer.parseInt(a);
double num2 = Double.parseDouble(a);
short num3 = Short.parseShort(a);
long num4 = Long.parseLong(a);
float num5 = Float.parseFloat(a);
byte num6 = Byte.parseByte(a);
boolean b = Boolean.parseBoolean("true");
Seven API documents
API is the basic programming interface provided by Java (classes provided by Java and related methods)
Chinese online documentation
The Java language provides a large number of basic classes, so Oracle also provides corresponding API documents for these basic classes to tell developers how to use these classes and the methods contained in these classes