1. Keywords
Keywords: Words given a specific meaning by Java language. All lowercase letters that make up the keyword. Note: goto and const exist as reserved words and are not currently used. main is not a keyword.
2. Identifier
Identifier: It is the sequence of characters used to name classes, interfaces, methods, variables, etc.
Composition rules: English uppercase and lowercase letters, numeric characters, $ and _
Note: Can't start with a number, can't be a keyword in Java, and can't be case sensitive
We usually give the following identifier:
Packages, classes or interfaces, methods and variables, constants
III. Comments
Format of single-line comments: //Comment text
Format of multi-line comments: /* Comment text*/
Format of document comments: /** Comment text*/
Note: Multi-line comments cannot be used in nested, while single-line is OK.
Document comments: parsed by the javadoc tool into a manual, which will be used in the object-oriented knowledge.
4. Constant, Category and Category Conversion
Constant: The value cannot be changed during the execution of the program.
Constant classification in Java:
Literal Constants Custom Constants (Asked in the object-oriented part)
1. Common constants:
String constants: content enclosed in double quotes
integer constants,
decimal constants,
Character constants: content enclosed in single quotes
Boolean constants: more unique, only true and false
Empty constant: null (array part explanation)
2. Category and conversion:
Java provides 4 forms of expression for integer constants: binary, octal, decimal, and hexadecimal.
Octal integers start with 0, and hexadecimal integers start with 0x.
Note: Today's computer systems rarely use octal. The binary representation is too verbose, so hexadecimal is generally preferred in programs.
Binary and octal conversion:
Binary and hexadecimal conversion:
3. Signed data representation:
In a computer, there are three notations of signed numbers: original code, inverse code and complement code. All data operations are performed using complement code.
Original code: It is the binary fixed point representation, that is, the highest bit is the sign bit, "0" means positive, "1" means negative, and the remaining bits represent the size of the value. Inverse code: The inverse code of a positive number is the same as its original code; the inverse code of a negative number is the inverse code of its original code inversely, but the sign bit (highest bit) remains unchanged. Complement: The complement of a positive number is the same as its original number; the complement of a negative number is to add 1 to the last bit of its reverse code.
Main reason: Using complement code, the symbol bits and other bits can be processed uniformly; at the same time, subtraction can also be processed by addition.
In addition, when two numbers represented by complement are added, if there is a carry on the highest bit (symbol bit), the carry is discarded.
Variables
Variable Overview: The amount whose value can change within a certain range during the execution of the program
Variable definition format: data type variable name = initialization value;
Note: Java language is a strongly typed language. For each type of data, a specific data type is defined. Memory space variables of different sizes are allocated in memory by data type:
Note: There is also a special null type in the reference type. The so-called reference data type is a reference to an object, and the object includes two types: instance and array. In fact, a reference type variable is just a pointer, but there is no pointer in Java.
Variables are divided by their declared location:
Local variables: variables defined inside a method or statement block member variables: variables defined inside a method and class
Six or eight basic data types and type conversions
1. The basic unit of computer data storage:
Byte is the basic calculation unit of computer file size.
1 byte (Byte) = 8 bits (Bit)
Bit means "bit" or "bit", which is the basis of computer computing; the bit in the binary is the smallest information unit in the binary. The binary bit can be used to represent a simple positive/negative judgment, with two states of switches (such as light switches).
illustrate:
During memory calculation, data with fewer bytes is faster to calculate; in hard disk storage, data with fewer bytes can also fully store more data.
2. Eight basic data types:
Note:
(1) Integer default: int. Declare long constants must be added 'l' or 'L' after the number, such as: long l1 = 8888888888L; // l must be added otherwise an error will occur
(2) Floating point constant (decimal) default: double. Declare a float constant, you need to add f or F after the number, such as: double d = 12345.6; //Correct float f = 12.3f; //F must be added otherwise an error will occur
(3) Java characters are encoded by Unicode, each character occupies two bytes, so they can be represented in hexadecimal encoding. Note: Unicode is a global language unified encoding. The character char in Java language can store a Chinese character because the characters in Java language occupy two bytes.
(4) The boolean type is suitable for logical operations and is generally used for program flow control.
Boolean type data only allows values of true or false, and integers with 0 or non-0 can be replaced by true and false, which is different from C.
(5) When defining byte and short, they actually receive a value of type int. This is done by myself. If it is no longer within their range, an error will be reported.
128:10000000
-128:10000000 (The 1 here is the sign bit, and it is also a numeric bit)
Notes on using variables:
Scope: In which level of braces is defined in, which braces are the scope of this variable. Two variables with the same name cannot be defined in the same scope. Initialization value: You cannot use it directly without an initialization value. It is recommended to define only one variable on a line: multiple variables can be defined, but it is not recommended.
3. Data type conversion:
Note: boolean type cannot be converted to other data types
Default conversion: (Conversion from small to large)
byte,short,char―int―long―float―double
Byte, short, char complement each other, and they participate in the operation and first convert it to the int type
Cases:
Target type variable name = (target type) (transformed data);
Summary: So, which conversion do we use?
It is generally recommended that it is best not to use cast type conversion at will, as it can easily cause loss of data accuracy.
(1) When converting data types with large capacity to data types with small capacity, cast characters should be added, but they may cause reduction in accuracy or overflow; pay special attention when using them.
(2) 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 performs calculations.
Interview questions:
A: Are there any differences between the following two methods?
float f1 = 12.345f;
float f2 = (float)12.345;
Answer: There is a difference: f1 is actually converted through a double type; while f2 itself is a float type.
B: Is there any problem with the following program? If so, where is it?
byte b1 = 3;
byte b2 = 4;
byte b3 = b1 + b2;
byte b4 = 3 + 4;
Answer: byte b3 = b1 + b2; there is a problem. Because variables are added, we will first look at the type issue, and we will also consider the type issue when we assign the result in the end.
Add constants, calculate the result first, and then see if it is within the range of byte. If not, an error will be reported.
C: What are the results of the following operations?
byte b = (byte)130;
System.out.println(b);
Answer: -126
D: Character participation operation: It is to find the value 'a' in ASCII 97
'A' 65
'0' 48
System.out.println('a');
System.out.println('a' + 1);
E: Strings participate in operation: This is actually the string splicing System.out.println("hello"+'a'+1);
System.out.println('a'+1+"hello");
System.out.println("5+5="+5+5);
System.out.println(5+5+"=5+5");
Note: The + here is not an addition operation, but a string concatenator.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.