Table of contents
1. Java language introduction and development overview
2. Advantages of the Java language
2.4 A large number of class libraries
3. The difference between Java language and C/C++
4. Get to know the main method of Java program entry
5. Comments, identifiers, keywords
1. Java language introduction and development overview
Java is an object-oriented programming language introduced in 1995 by Sun Microsystems (now Oracle). The design goal of the Java language is to provide a portable, high-performance, object-oriented programming language suitable for Internet applications in distributed environments.
The Java language is a general-purpose, high-level, object-oriented programming language, which has the following characteristics:
- Simplicity : The syntax of the Java language is relatively simple and easy to learn and understand.
- Object-oriented : The Java language is a pure object-oriented programming language that supports object-oriented concepts such as encapsulation, inheritance, and polymorphism.
- Portability : The compiler of the Java language can compile the source code into bytecode, and the bytecode can run on any platform that supports the Java Virtual Machine (JVM), thus realizing cross-platform applications.
- Security : The Java language provides security mechanisms at the language level, such as class loaders, access control, and exception handling, which can effectively prevent malicious code.
- High performance : The Java language uses a JIT compiler to convert bytecode into native machine code, thereby achieving faster execution speed than interpreted execution.
- A large number of class libraries : The Java language has rich class libraries and tools that can be used to develop various types of applications, such as Swing, JDBC, Servlet, etc.
The development of the Java language has gone through three main stages:
1.1 JDK 1.x
JDK 1.x is the initial version of the Java language, including basic language features and class libraries. At this stage, the Java language is still in its infancy, and many features and class libraries have not been perfected, but it already has basic features such as cross-platform and object-oriented.
1.2 JDK 1.2 ~ JDK 1.4
JDK 1.2 ~ JDK 1.4 is the stage of rapid development of the Java language, introducing many new features and class libraries, such as Swing, JDBC, JNDI, etc. At this stage, the Java language has become a very popular programming language and is widely used in Web development, enterprise applications and other fields.
1.3 JDK 5 and above
JDK 5 and above are modern versions of the Java language, introducing important features such as generics, enumerations, annotations, and automatic boxing and unboxing. At this stage, the performance and security of the Java language have been greatly improved, and it has become a more mature and stable programming language.
2. Advantages of the Java language
The Java language has the following advantages:
2.1 Portability
The compiler of the Java language can compile the source code into bytecode, and the bytecode can run on any platform that supports the Java Virtual Machine (JVM), thus realizing cross-platform applications. This feature is one of the most important features of the Java language and one of its greatest strengths.
For example, a Java program written on the Windows operating system can run on other operating systems such as Linux and Mac OS X without any modification.
2.2 Object-oriented
The Java language is a pure object-oriented programming language that supports object-oriented concepts such as encapsulation, inheritance, and polymorphism. This enables the Java language to better support complex application development, and also makes the code easier to maintain and expand.
For example, by encapsulating data and methods, Java programs can better protect data security and integrity, and at the same time better hide object implementation details, improving code security and maintainability.
2.3 Security
The Java language provides security mechanisms at the language level , such as class loaders, access control, and exception handling , which can effectively prevent malicious code. The security of the Java language makes it a very suitable programming language for developing web applications and mobile applications.
For example, in Java web applications, the security mechanism of the Java language can help developers prevent common web security issues such as SQL injection and XSS attacks, thereby protecting user privacy and data security.
2.4 A large number of class libraries
The Java language has rich class libraries and tools that can be used to develop various types of applications, such as Swing, JDBC, Servlet , etc. These class libraries and tools can help developers quickly build applications, but also improve code reusability and maintainability.
For example, in a Java web application, the Servlet API can help developers quickly build the back-end logic of the web application, while the JDBC API can help developers easily interact with the database to achieve data storage and retrieval.
3. The difference between Java language and C/C++
Compared with the C/C++ language, the Java language has the following differences:
3.1 Memory Management
The Java language has the feature of automatic memory management, which can automatically perform garbage collection, thereby reducing the programmer's memory management burden. The C/C++ language requires programmers to manage memory manually, which is prone to problems such as memory leaks and memory overflow, which increases the complexity and difficulty of the program.
3.2 Portability
The Java language is cross-platform and can run on different operating systems and hardware platforms. The C/C++ language needs to be compiled and linked on different platforms, which increases the difficulty of development and maintenance.
3.3 Security
The Java language provides a security mechanism at the language level, which can effectively prevent malicious codes and security holes. The C/C++ language is prone to buffer overflow and other security issues, requiring programmers to take additional security measures.
4. Get to know the main method of Java program entry
The entry point of the Java program is the main method, which is the main function of the Java program and also the starting point of the program. When a Java program starts, the JVM will automatically search for and execute the class containing the main method.
The main method is defined as follows:
public static void main(String[] args) {
System.out.println("Hello,Java");
}
The main method is of public, static, and void types, and the representation of the method is as follows:
public | This method is public and can be accessed by other classes; |
static | Indicates that the method is static and can be called directly through the class name; |
void | Indicates that the method does not return a value. |
The program will output:
5. Comments, identifiers, keywords
5.1 Notes
Java comments are text used to explain and illustrate code. Java provides three types of annotations, as shown in the following table:
single line comment | Comments starting with "//", the comment content goes to the end of the line. |
multiline comment | Comments that start with "/*" and end with "*/" can span multiple lines. |
Documentation comments | Comments starting with "/**" and ending with "*/" can be used to generate API documentation. |
For example:
// this is a single line comment
/*
This is a multi-line comment
This is the second line of the comment
*/
/**
* This is a documentation comment
* used to generate API documentation
*/
Comments can improve the readability and maintainability of the code, it is recommended to add comments when writing the code.
5.2 Identifiers
A Java identifier is a name used to identify elements such as variables, methods, classes, interfaces, etc. A Java identifier must satisfy the following rules:
- Must start with a letter, underscore, or dollar sign.
- Can contain letters, numbers, underscores, or dollar signs.
- case sensitive.
- Cannot be a Java keyword.
For example:
int count;
String userName;
double totalPrice;
When writing code, try to use meaningful identifiers to improve the readability and maintainability of the code.
5.3 Keywords
Java keywords are words with special meaning in the Java language and cannot be used as identifiers. Java keywords have the following functions:
- Define language structure and grammar.
- Specify access rights and scopes.
- Identify specific operations and relationships.
Java keywords cannot be used as identifiers, for example:
public class Test {
int public; // 错误,不能使用关键字作为标识符
}
When writing code, avoid using Java keywords as identifiers to avoid compilation errors.
keywords | illustrate |
abstract | abstract class or abstract method |
assert | Affirmation |
boolean | Boolean type |
break | break out of the loop |
byte | byte type |
case | Branching in a switch statement |
catch | catch exception |
char | character type |
class | kind |
const | Constant, deprecated and no longer used |
continue | skip a loop in the loop body |
default | Default branch in switch statement |
do-while | cycle |
double | double precision floating point type |
else | branch in if statement |
enum | enumerated type |
extends | class inheritance |
final | immutable, or declared as constant |
finally | The last statement block of exception handling |
float | single precision floating point type |
for | for loop |
goto | Obsolete and no longer used |
if | if statement |
implements | implement the interface |
import | import class |
instanceof | Determine if an object is an instance of a class |
int | integer type |
interface | interface |
long | long integer type |
native | Declare native methods |
new | create object |
package | Bag |
private | private |
protected | be protected |
public | public |
return | return value |
short | short integer type |
static | still |
strictfp | Normalization of Floating Point Precision and Handling |
super | Call the constructor or method of the parent class |
switch | select statement |
synchronized | synchronized method or synchronized block |
this | current object |
throw | Throw an exception |
throws | Exceptions that a method declaration may throw |
transient | Transient variables, no serialization required |
volatile | Mutable, for multi-threaded programming |
void | empty type |
while | while loop |
It is not easy to create, I hope to help everyone, if there are any mistakes, please point them out in the comment area, if possible, I also hope to support it three times!
Jerry would appreciate it