Java basic syntax
Sort out some knowledge points of learning java basic syntax before.
JRE and JDK
If we want to run an existing Java program, we just need to install JRE.
If we want to develop a new Java program, we must install JDK.
constant
classification
variable
data type
Data type classification
Java data types fall into two categories:
Basic data type
Four categories and eight basic data types:
Data type conversion
The data required to participate in the calculation in Java programs must ensure the consistency of data types. If the data types are inconsistent, type conversion will occur.
Automatic conversion
Conversion rules
Types with a small range are promoted to types with a large range. Byte, short and char operations are directly promoted to int.
byte、short、char‐‐>int‐‐>long‐‐>float‐‐>double
Force conversion
Ternary operator
Jshell scripting tool
Jshell scripting tool is a new feature of jdk9
When will we use the jshell tool? When we write very little code, but we are not willing to write classes, main methods, or compile and run, we can use the jshell tool at this time.
Start the jshell tool and directly enter the jshell command on the DOS command line.
Next, you can write java code without writing classes and methods. You can write the code in the method directly. At the same time, you can enter directly without compiling and running
+=Extension of symbols
Is there a problem with the following procedure?
public static void main(String[] args){
short s = 1;
s+=1;
System.out.println(s);
}
Analysis: S + = 1 is logically regarded as s = S + 1. The calculation result is promoted to int type, and an error occurs when assigning value to short type, because types with large value range cannot be assigned to types with small value range. However, s = S + 1 is operated twice, and + = is an operator, which is operated only once, with the characteristics of forced conversion, that is, S + = 1 is s = (short) (s + 1). Therefore, the program has no problem, the compilation passes, and the running result is 2
Operation of constants and variables
Is there a problem with the following procedure?
public static void main(String[] args{
byte b1=1;
byte b2=2;
byte b3=1 + 2;
byte b4=b1 + b2;
System.out.println(b3);
System.out.println(b4);
}
Analysis: B3 = 1 + 2. 1 and 2 are constants and are fixed data. During compilation (compiler javac), it has been determined that the result of 1 + 2 does not exceed the value range of byte type and can be assigned to variable B3. Therefore, B3 = 1 + 2 is correct.
On the contrary, B4 = B2 + B3, B2 and B3 are variables, and the value of variables may change. During compilation, the compiler javac is not sure what the result of B2 + B3 is, so it will process the result as int type, so int type cannot be assigned to byte type, so compilation fails.
Method overloading
array
Definition of array
Mode 1
Mode II
Mode III
Memory partition of Java virtual machine
In order to improve the operation efficiency, the space is divided into different regions, because each region has a specific data processing mode and memory management mode.
An array memory graph
public static void main(String[] args) {
int[] arr = new int[3];
System.out.println(arr);//[I@5f150435
}
When the above method is executed, the output result is[ I@5f150435 , is the address of the array in memory. The contents of new are stored in heap memory, while the variable arr in the method stores the address of the array.
Outputting arr [0] will output the elements on the 0 index in the array in the memory address saved by arr
Array inversion
Object oriented thought
summary:
Java language is an object-oriented programming language, and object-oriented idea is a programming idea. Under the guidance of object-oriented idea, we use java language to design and develop computer programs. The object here generally refers to all things in reality, and each thing has its own attributes and behavior. The object-oriented idea is the design idea of abstracting the attribute characteristics and behavior characteristics of things with reference to real things in the process of computer programming and describing them as computer events. It is different from the process oriented idea, which emphasizes that the function is realized by calling the behavior of the object, rather than operating and realizing it step by step.
characteristic:
Object oriented thinking is an idea more in line with our thinking habits. It can simplify complex things and turn us from executor to commander. Object oriented language contains three basic features, namely encapsulation, inheritance and polymorphism.
What is a class
What is an object
Relationship between classes and objects
Class definition format
public class ClassName{
//成员变量
//成员方法
}
Use format of object
Create object:
类名 对象名 = new 类名();
Use the object to access the components given in the class:
对象名.成员变量;
对象名.成员方法();
Differences between member variables and local variables
Variables have different names according to their definition positions. As shown in the figure below:
encapsulation
summary
Object oriented programming language is a simulation of the objective world. In the objective world, member variables are hidden in the object, and the outside world can't operate and modify them directly. Encapsulation can be considered as a protective barrier to prevent the code and data of this class from being freely accessed by other classes. To access the data of this class, you must use the specified method. Proper encapsulation can make the code easier to understand and maintain, and also strengthen the security of the code.
principle
Hide the property. If you need to access a property, provide public methods to access it.
Steps for encapsulation
Meaning of private
This keyword
This represents the reference (address value) of the current object of the class, that is, the reference of the object itself.
Tip: when there is only one variable name in the method, the default is to use this modifier, which can be omitted.
Construction method
When an object is created, the constructor is used to initialize the object and assign the initial value to the member variable of the object.
Definition format of construction method
修饰符 构造方法名(参数列表){
// 方法体
}
matters needing attention:
Standard code -- JavaBean
JavaBean is a standard specification for classes written in the Java language. Classes that conform to JavaBean are required to be concrete and public, have parameterless construction methods, and provide set and get methods for operating member variables.