1. JAVA language Overview
Java is simple, object-oriented, secure, interpreted, platform-independent, and Multithreading
Object-Oriented Programming
-Java is an object-oriented programming language that fully supports the three basic features of object-oriented: encapsulation, inheritance, and polymorphism.
-The smallest unit of a Java program is a class, which encapsulates the data and data processing methods.
Example:
-To build a car class, you need to extract the common states and behaviors of all car objects. State is represented by variables, and behavior is represented by methods.
Class Car {
Intcolor_number;
Intdoor_number;
Int speed;
......
Void brake (){... }
Void speedUp (){...};
Void slowDown (){... };
......
}
1.2 features of Java
Object-oriented
Security
-Pointers are not supported in Java.
-Java internal security measures
Platform independence
-The compiled bytecode corresponds to the Java Virtual Machine and can be run on different platforms.
Multithreading
-Java is the first advanced language that provides internal to multi-threaded support at the language level.
Memory Management
-Java automatically manages memory and recycles garbage
Differences between Java and C/C ++
-Java does not provide preprocessing functions such as # include and # define. Other classes and packages are included in the import Statement;
-Java does not contain structure, union, and typedef;
-In Java, there are no functions that do not belong to class members, no pointers or multi-inheritance. Java only supports single-re-inheritance;
-Disable goto in Java, but the goto keyword is retained;
-There is no operator overload in Java;
-Java does not have global variables. You can define public and static data members in the class to implement the same functions.
1.3 Java class library
L for most common functions, there are a large number of compiled and tested classes. The set of these classes is the Java class library.
The lJava class library is provided along with the compiler, and some class libraries are provided by independent software developers.
2. Principles of cross-platform Java
3. Java development tools and application examples
Java development tools include
Javac: a Java compiler used to compile a java program into Bytecode.
Java: Java interpreter. Execute a java application that has been converted to Bytecode.
Jdb: Java debugger used to debug java programs.
Javap: decompilation to restore class files back to methods and variables.
Javadoc: Document builder that creates HTML files.
Appletviwer: the Applet interpreter used to explain the java small application that has been converted into Bytecode.
After JDK is installed, the following directory is generated:
\ Bin directory: Java development tools, including Java compilers and interpreters
\ Demo Directory: some instance programs
\ Lib directory: Java Development class library
\ Jre Directory: Java Runtime Environment, including Java Virtual Machine and runtime class library
4. Basic data types and expressions
Variables and constants
Variable
-An item named by an identifier
-Each variable has a type, such as the int or Object type, and the variable has a scope.
-The value of a variable can be changed.
Constant
-Constants cannot be changed once they are initialized.
Final int PRICE = 30;
Identifier
-The identifier is a name that corresponds to a location (Address) in the memory.
-The first character of the identifier must be one of the following:
Uppercase letters (A-Z)
Lowercase letter (a-z)
Underline (_)
Dollar sign ($)
-The second and subsequent characters of the identifier must be:
Any character in the above list
Digit (0-9)
Basic Data Type
-Byte 8 bits-128 ~ + 127
-Short 16 bits-32768 ~ + 32767
-Int 32 bits-231 ~ (231-1)
-Long 64 bits-263 ~ (263-1)
-Char 16 bits 0 ~ 65535
Integer Operation
-Comparison operators (Relational operators)
Arithmetic comparison operator <, <=,>, and> =
Equal arithmetic comparison operator = and! =
-Arithmetic Operators
Unary operator + and-+,-*,/, and % (remainder)
Auto-increment/auto-increment operator ++ /--
Shift Operator <, >>, and >>>
Bitwise operators ~, &, |, And ^
-Conditional operators? :
-Type conversion Operator
Floating Point Number
-Float: Single-precision floating point number
32-bit
-M · 2e ~ M · 2e
-M is a positive integer smaller than 224.
-E is an integer between-149 and 104.
-Double: double-precision floating point number
64-bit
-M · 2e ~ M · 2e
-M is a positive integer smaller than 253.
-E is an integer between-1045 and 1000.
Boolean Type and Boolean Value
-The boolean type indicates a logical volume. Two values are available: true and false.
Boolean operator
-Relational operators = and! =
-Logical "Non" Operator!
-Operator & |
-Conditional operators? :
String -- String
-String is a class.
-A part of the String JDK standard class set
String animal = "walrus ";
String animal = new String ("walrus ");
Expressions and operators
An expression is composed of a series of variables, operators, and method calls. An expression can calculate a value. A lot of work in the program is done by calculating the value of the expression.
-Sometimes the side effects of expressions are required. For example, a value assignment expression assigns a value to a variable.
-More often, it is the expression value. This value can be used as a method parameter, a larger expression operand, or affects the statement execution sequence.
Value assignment operator
-Simple value assignment operator =
-Composite value assignment operator * =/= % = + = <<=>>>>>=<=^ = | =
Relational operators
-The relational expression type is always Boolean (bool ).
-Arithmetic comparison operator <, <=,>, and> =
-Type comparison operator instanceof
For example, e instanceof Point // Point is a class.
If and while statements use boolean expressions or boolean values (true or false) as conditions for control:
Int a = 1;
If (a) // Error
{
// Do something;
}
Note: Java prohibits the use of a number as a Boolean value.
Which of the following relational expressions is true:
String s = "hello ";
// String s = new String ("hello ");
String t = "hello ";
// String t = new String ("hello ");
Char c [] = {'h', 'E', 'l', 'l', 'O '};
A. s. equals (t );
B. t. equals (c );
C. s = t;
D. t. equals (new String ("hello "));
E. t = c;
A, C, and D are correct. Because s and t are not created using new, they point to the same String constant in the memory pool, so their addresses are actually the same, so C is correct.
Logical operators
-"And" Operation &: if both operands have true values, the result is true. Otherwise, the result is false.
-"Or" Operation |: if the values of both operands are false, the result is false. Otherwise, the result is true.
-"Non" Operator!
The operand type must be boolean.
If the result of the operand is false, the result of the expression is true. If the result of the operand is true, the result of the expression is false.
Conditional operators (expression 1? Expression 2: expression 3)
-Calculate expression 1 first.
-If expression 1 is set to true, select expression 2.
-If expression 1 is set to false, select expression 3.
Type conversion
Each expression has a type. If the expression type is inappropriate for the context
-Sometimes compilation errors may occur.
-Sometimes the language performs implicit type conversion.
Extended Conversion
-Byte, short, int, long, float, double, char
-There is no loss of information when converting from one Integer type to another Integer type or from float to double type.
Narrow Conversion
-Double, float, long, int, short, byte, char
-Narrow conversion may cause loss of information
Short s1 = 1; s1 = s1 + 1; is there a problem?
The result of the s1 + 1 operation is int type. Forced type conversion is required here. That is, s1 = (short) (s1 + 1)