Here we officially enter the javase object-oriented language section, as mentioned in JavaSE's collection (0) -- the third main line in the main line of JavaSE, I plan to use the main line-the composition element of the javase program in the first layer of the javase object-oriented language section. This main line is selected to fit the idea of object-oriented hierarchical expression (inheritance is hierarchical expression)
Here we officially enter the javase object-oriented language section, as mentioned in JavaSE's collection (0) -- the third main line in the main line of JavaSE, I plan to use the main line-the composition element of the javase program in the first layer of the javase object-oriented language section. This main line is selected to fit the idea of object-oriented hierarchical expression (inheritance is hierarchical expression)
Here we officially enter the javase object-oriented language section
As the third main line mentioned in the JavaSE collection (0) -- JavaSE main line, I plan to use the main line -- the composition element of the javase program in the first layer structure of the javase object-oriented language section. This main line is selected to fit the idea of object-oriented hierarchical expression (inheritance is the embodiment of hierarchical expression, in fact, hierarchical expression is a natural way of thinking, in reality, to facilitate the expression and memory of things, a simplified way of describing things is introduced. For example, a parent class represents the description of child classes. This method is actually a hierarchical expression, in addition, hierarchical expressions and comparative expressions are usually used in combination. Compared with combined expressions, hierarchical expressions increase the coupling of parent class sub-classes. However, this is not a hierarchical expression from concrete to abstract, it is a hierarchical expression in a combination.
From the static point of view, the composition elements of the javase program mainly include four levels of content, including packages, classes, functions, fields, and variables. If the element at the top layer is a javase application system, the element at the next layer of the javase application system is a package. A java application system consists of one or more packages. A package consists of one or more classes. The source code is a variety of java classes, and the bytecode is a variety *. class file (ps: interface, enumeration, annotation I am also included in the Class), in jvm is the instance of each class Class; the composition element of the class has a field (ps: including final modified variables), functions, classes (member classes); the composition elements of a function include local variables, classes (local classes), and statements; variables cannot be divided (ps: variable is the smallest data unit, function is the smallest behavior unit), class is the smallest closed unit.
From a dynamic perspective, variables are divided into reference and basic type variables during program execution, and the role of classes must be instantiated as objects to be reflected. Therefore, an additional basic component element-object is required.
From the perspective of OOD design, both basic type variables and reference type variables can be used as the same elements, which is the role of data storage, the implementation must be implemented in the form of basic type variables; otherwise, "reference type variables -- pointing to references" will be infinitely recursive. "When writing code to manipulate variables, these two variables are also different. Basic type variables provide direct descriptions and operations on data, and reference type variables also provide methods for encapsulated Data Operations (implemented through the dot operator, direct operations on the reference type only support assigning values and obtaining references). During the design, I considered the basic type variable as a subset of the reference type variable (ps: so I don't think there will be any basic type variable in the advanced programming language in the future. I just want to package it directly. Why do I have two sets of content !).
JVM supports eight basic data types and reference types.
From the JVM perspective, an application mainly functions in combination with the JVM. One is the local variables and operands in the stack, and the other is the objects in the heap, the other is the Class bytecode instance in the method area. During the creation of OOD, the existing javase class libraries can also be counted as component elements. During the design, they generally start from abstraction, so these specific contents can be ignored first, first abstract and then concrete (first interface and then implementation), first whole and then partial (first upper layer and then bottom layer ).
The following describes the javase object-oriented syntax rules in detail, starting with the "variable" element at the lowest level, in order to conform to our combined thinking approach.
Variables:
- Keywords
- Identifier
- Data Type
- Constant
- Constants and variables
There is no field-related thing, or the final and volaitile keywords, mainly related to local variables.
Keywords
Definition: words that are given special meanings by the java language,It is a basic element in the java language and can be used to define or modify java building units, such as variables, statements, functions, and classes., Directly implemented by javac or jvm, such as the basic data type int short long char boolean, defining the reference data type class interface enum @ interface, statement for while if switch, the access modifier private protected public static final abstract volatile and other words are all keywords.
The program we write is a string Text Document for javac, which is used for symbol parsing and lexical syntax analysis, after parsing, it can be divided into keywords, the literal value of a constant, the literal value of a sign, and operators. The Literal Value of a constant can be divided into various data types, integer, floating-point, boolean, and null reference types. In addition, there are also symbol reference denominations, such as class names, variable names, and function names.
Features: All keywords in java are in lowercase.
JAVA Keywords ):
Abstract, assert, boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, extends, final, finally, float,, goto, implements, import, instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static, strictfp, super, switch, synchonized, this, throw, throws, transient, try, void, volatile, while
Note:
1. To maintain consistency with other programming languages, JAVA retains const and goto, but does not implement it. Use "public static final" instead of "goto.
2. differentiate final and finally. As a modifier, final indicates that the value of the variable cannot be modified. When used for classes and methods, it indicates that the class or method cannot be inherited. Use finally with try to throw a new exception or capture the statements that must be executed after the exception. Only System. exit can terminate the execution of finally statement blocks.
3. Note the difference between throws and throws. Throw is used in the method body to pass the exception up to the method that calls the method. Throws is used in the method signature line, indicating that the method throws an exception. 4. native is only used for method modifiers. It is pointed out that the method is written in a platform-related language (for example, C language. Strictfp is used as a method or class modifier to indicate the fp restrictions that floating point numbers in all expressions in the method or class will strictly abide. It is never used to modify variables. If the underlying platform supports higher precision, strictfp will become invalid. Synchonized is only used for method modifiers, indicating that the method can only be accessed by one thread at the same time. Transient can be used as a member variable modifier to prevent fields from being serialized permanently. When serializing objects, fields modified with transient are always skipped.
Identifier
Definition: some names customized in the Program Act as identifiers,It defines the java program'sBuild units, and these identifiers form the literal value of these build units.These identifiersThe string is the symbol index of these building units, and reflection provides us with the ability to parse java building units based on the symbol index.In jvm, symbol indexes are mainly stored in the class constant pool. These symbol indexes can be parsed when the jvm executes the program. A very important feature of JVM is resolution symbol reference.
Java stipulates that identifiers can only consist of letters, numbers, underscores, and $ symbols, and cannot start with a number. The $ symbol is often used in internal classes and dynamically generated proxy classes.
Naming rules for identifiers in java:
All package names are in lower case xxxxyyyy
Both class name and interface name are capitalized XxxxYyyy
The variable name and function name start with lowercase letters, and other words start with xxxxYyyy.
Constants (the variable modified by final) are uppercase letters, and each word is connected to XXXX_YYYY using an underscore.
Data Type
8 Basic Data Types
Based on the significance of data in real life, eight basic data types are divided into three types.
Boolean booleaN
(What can be done) boolean data is used to express the true and false logical results in reality. It is often used where the program has execution conditions.
(What is it) true, false
Character char
(What can be done) structured data has a linguistic significance and can be used to represent our language and characters in our lives for communication.
(What is it) character data is enclosed in single quotes char c = 'a'; character data in the compiled class file uses the Unicode Character Set, so the java character data is 16 bits, unicode Code Point mechanism. In java source code, myeclipse generally sets UTF-8 encoding by default. Our operating system is generally gbk encoding.
Numeric Number
(What Can We Do) numeric data is used to express a numerical value. A numerical value is a meaning of a mathematical number. A numerical value is not only a symbol, but also a measurable meaning in a mathematical sense.
Integer
Type
- Byte 1 byte signed
- Short 2-byte signed
- Int 4-byte signed
- Long 8-byte signed
Three Methods: 10-in-10, 8-in-zero, and 16-in-Zero-x
The default integer constant is int type.
L or L followed by a long constant, long number = 50L
Floating Point
- Float 4 bytes
- Double 8 bytes
The default floating point type constant is double.
Because the same floating point number may have different precision, the floating point number may change its precision as long as it passes through the operation, so the program generally cannot use "=" for comparison, generally use greater than or less to compare (Floating Point precision in java).
In our life, integers are subsets of floating-point numbers. At the bottom of the computer, integers and floating-point numbers differ greatly from memory storage, their operation methods vary greatly, which is why integers can be used for logical operations, but floating point numbers cannot be used. This is a reference to the computer principle. I want to keep this difference to make it easier for c programmers. In fact, many JVM operation commands convert boolean char byte short operations to int operations.
Reference data type
- Null, class, Object
- Interface
- Array
- String
(Why) the biggest difference between a reference data type and a basic data type is whether there is a reference data storage in the storage form. The basic data type is only the storage of the data content and the reference data type, also saves references with data content
We divide the data represented by computers into two parts: data itself and data type. A certain type of data can belong to multiple data types (upward transformation ), the data type involves the storage and usage of data in the computer. The reference data type and basic data type are divided from this perspective. However, everything in the computer is data, and the data Type is also data. All of them specifically adopt a data Type to describe the data Type. This is the Type interface. In java, data types can be divided into six basic data types: class interface enum @ interface array, which are stored and used as Class data types, class enum @ interface arrays all have instances, and their instances are of the Ojbect type. Interfaces do not have their own instances, but they have class instances. The basic data types directly store data.
The String type is special, because the JVM generally uses c and c ++ to write data, and they themselves support the String type data, while the basic jvm data type does not have the String type, however, in the JVM constant pool, there is a UTF-8 structure used to indirectly store strings, such as class names, function names, field names, and string constants in programs, indirectly, the above types are not directly indexed with the UTF-8 structure, but the indexes of their own corresponding structures. They will eventually be indexed to the UTF-8 structure. Therefore, String-type instances are usually stored in the class constant pool in the method area, rather than in the heap. Instances of the Class data type are the same as those of the String type and stored in the method area.
Constant
(What can be done) constants (data) are used to express data to the compiler. They are used to communicate with the compiler.
Constants are divided:
- Numeric type, which contains the integer type, such as 12, decimal type, such as 12.5
- Boolean Type true false
- Character string types, such as 'A', \, '%'
- Reference type, mainly null and Object Reference (Object Reference without constants can be obtained only through reflection and enumeration before using String type data constants)
To use a string to obtain a reference type, 1. reflection can be used; 2. enumeration can be used.
Constants and variables
(What Can We Do) In reality, information such as the attributes of things changes during the development of things, or it can be determined only when things develop to a certain time point, variables are the objects used to store such information and store this information in the memory. We use statements to operate on these variables to describe the process of changing the movement of things.
(Why) from the underlying implementation, the variable is determined in the memory position (that is, when the program is executed, the variable is either in the java stack or in the heap ), storage unit with fixed size
(What is it)
Compared with common data,Constants and variables are mainly used to store the content that can be determined during program execution (runtime), rather than the content that can be determined by the compiler (during compilation )..
From the perspective of the upper layer, the variable name and the person name are the same, mainly providing methods for data operations.
Variable declaration:
Variable declaration format: int age;
In terms of implementation, the declaration of variables provides a description of the variable size and storage location (for a field, JVM determines its storage location through the symbol index. For details, see:Object Memory Structure), The type of the variable, indicating the size of the storage unit to be created, the name of the variable, and the storage location of the created storage unit.
On the abstract level, the variable declaration refers to the communication between the programmer and the computer. It refers to the interface provided by the computer to tell the computer what kind of information storage unit to create.
(Ps: variables of the basic type are the most basic meta objects. Other objects are constructed by them. The structure here is not an inherited relationship, but a composite relationship.
Compared with dynamic languages, java, a static language, specifies the variable size During Variable declaration. The dynamic language isExecution ProgramThe variable size is determined based on the type of the variable value. This is like a person's name has no type, but this person's object has a type.
Compared with a weak type language, java is a strong type language. When using a variable, you must declare the type of the variable. The weak language isCompilerThe type of the variable is automatically determined based on the value of the variable. When the type of the variable value changes, the type of the variable is also automatically changed.
It seems that the weak type and dynamic language are exactly the same, but the definition is different. I am confused !!)
Use of variables:
- Reference of variable content
- Modify variable content
Data type conversion:
In java, data type conversion mainly follows two principles: Same semantic conversion, and conversion between data types with the same semantics, mainly between numeric types; same storage conversion, data Types with the same storage method can be converted, mainly between char and integer types. Between reference types, parent and child classes can be converted. I think this should also be the same semantic principle.
When type conversion involves a range issue, if the java compiler can determine that there is no cross-border error, no error will be reported. Otherwise, an error will be reported.
Checking for type conversion is a task done by the compiler (This also shows that the type check cannot be used when reflection is used, which is why many objects use the Object type when reflection is used)Because many jvm commands do not support all types and only support the int float double long type, the java compiler must perform type conversion and perform type conversion security checks before conversion. For example
class HelloWorld{public static void main(String [] args){short s = 1;System.out.println(s);}}
This code is obtained after javap disassembly:
The two lines in the red box are short I = 1. The corresponding commands are all commands for int-type data operations, iconst_1 loads an int data from the constant pool to the operand stack of the current jvm function, and istore_1 pops up the operand stack for the int data just in the stack, and write it to the 1st index location in the local variable area (because the static function args variable occupies the 0 index location .), After javac, the local variable short s is of the int type.
For example, let's talk about javac's corresponding type conversion detection,
Short s = 1; in this expression, 1 is the default integer, so it is the int type, and s is the short type. In this way, 1 is changed to (short) 1 by default, in this default behavior, the java compiler checks whether data type conversion is feasible. Because it is a default, it is easy to make mistakes. Unlike the explicit forced type conversion, developers consciously use type conversion, in this case, the java compiler has less strict requirements on type conversion. During implicit conversion, the java compiler checks that the type conversion follows "the java compiler can determine that no error is returned if there is no cross-border. Otherwise, an error is returned .". Because 1 is a constant, the java compiler can naturally determine whether it has crossed the border. Short s = s + 1; s + 1 is a variable expression. The value of the expression must be known during program execution. In this case, the java compiler reports an error. Short s + = 1; this is a special case. Here, I have explained in one sentence that all compound assignment operators carry forced type conversion, that is, if the explicit forced type conversion is performed, the java compiler will not report an error, because once you use a composite operator, the java compiler considers that you are consciously using the type conversion. Why is the java compiler set this way, I think it may be that the composite assignment operator is a simple expression prepared for the experts. The java compiler sets the user as a master, which naturally requires a little less.
Data conversion between signed data of different lengths:
Short-> int: Fill in the short data by symbol bit
Int-> short directly truncates high positions, so if the data exceeds the range, an error occurs.