The first chapter Java experience

Java beginner

Learn about the history of Java

Origin: The monolithic computer system was born in the 1990s (background factor)

In order to seize market opportunities, SUN established a project team named "Green" led by James Gosling in 1991. The group developed an object-oriented language (a combination of software and hardware) called "Oak" in 1992, which was shelved because it was not favored by hardware vendors. In 1994, the project team saw the future development prospects of the browser and decided to apply Oak to the World Wide Web. In 1995, they used Oak language to develop a technology that can embed small programs into web pages for execution-Applet, at the same time Oak officially changed its name to Java.

Evolution: In January 1996, Sun introduced JDK 1.0. After the release of JDK 1.0, many programmers learned and used Java to make web pages. JDK includes two parts: development tools and operating environment. The operating environment includes core API, integrated API, user interface API, publishing technology, and Java Virtual Machine (JVM). The release of JDK 1.0 marks that Java has become an independent development tool.

Representative technologies of JDK 1.0 include: Java Virtual Machine, Applet, AWT, etc.

In February 1997, SUN Company immediately launched JDK 1.1. Representative technologies of JDK 1.1 include: JDBC, JavaBeans, RMI, JAR file format, internal classes and reflection in Java syntax. On December 8, 1998, JDK 1.2, the enterprise version of the second-generation Java platform, J2EE, was released.

In June 1999, Sun company divided the Java system into three directions:

J2ME (Java 2 Micro Edition, the micro version of the Java 2 platform), used in mobile, wireless and resource-limited environments;

J2SE (Java 2 Standard Edition, the standard edition of the Java 2 platform), used in the desktop environment;

J2EE (Java 2 Enterprise Edition, the enterprise edition of the Java 2 platform), used in Java-based application servers.

With different vendors, Java APIs are also divided into three categories: Java Core API: Java core class library; Java Optional API: extension of the previous API; Special API: API provided by a special manufacturer or organization. The release of the Java 2 platform is the most important milestone in the development of Java, marking the beginning of the popularization of Java applications. Representative technologies of JDK 1.2 include: Swing, Java IDL, EJB, Java Plug-in, etc. In the API documentation, the strictfp keyword and Collections collection class are added.

In June 2005, at the Java One conference, Sun released Java SE 6. At this time, various versions of Java have been renamed, and the number 2 has been cancelled. For example, J2EE was renamed JavaEE, J2SE was renamed JavaSE, and J2ME was renamed JavaME.

On July 28, 2011, Oracle, which acquired Sun, released Java SE 7, which introduced new features such as binary integers, switch statements that support strings, prismatic syntax, multiple exception capture, and a try language that automatically closes resources.

On March 18, 2014, Oracle Corporation released Java SE 8. This version upgrade brings a lot of new features such as brand-new Lambda expressions and streaming programming to Java. These new features make Java more powerful.

On September 22, 2017, Oracle Corporation released Java SE 9. This version upgrade strengthens the Java modular system, makes the Java language lighter, and uses a more efficient and smarter G1 garbage collector. A lot of updates on the class library can further simplify programming.

On March 21, 2018, Java SE 10 was released. The Java 10 version brought many new features. Among them, the most concerned by developers is local variable type inference. In addition, it also includes new features such as garbage collector improvement, GC improvement, performance improvement, thread management and control.

On September 26, 2018, Java SE 11 was released. This is the first long-term support version after the major version cycle of Java (LTS version continues to be supported until September 2026). Java 11 brings important features such as ZGC and Http Client, and includes a total of 17 JEPs (JDK Enhancement Proposals).

Java system and characteristics

JavaSE: Java Platform, Standard Edition: the foundation of each application platform, desktop development and low-end business application solutions.

JavaEE: Java Platform, Enterprise Edition Enterprise Edition: a solution for developing applications based on the enterprise environment

JavaME: Java Platform, Micro Edition: The best solution for consumer products and embedded devices

What can Java do? Develop desktop applications (banking software, shopping mall settlement software); develop Internet-oriented web applications (portal website (ICBC), online shopping mall, Alibaba, e-commerce website); provide solutions for various industries (finance, telecommunications, Electricity...); Embedded development (mobile phones, video phones, digital set-top boxes and car navigation systems).

Java features

A pure object-oriented programming language.

A platform-independent (cross-platform) language. (It provides an interpretation environment that runs on different platforms)

A robust language that absorbs the advantages of the C/C++ language.

Have high security. (Automatic garbage collection, mandatory type checking, cancel pointer)

Java cross-platform principle

Java source code (.java file) is compiled to generate Java bytecode (cross-platform, it is a .class file). The Java bytecode runs on the Java Virtual Machine (JVM) and then mobilizes the operating systems of each platform such as Windows, Mac, Linux to achieve specific operations.

Two core mechanisms of Java technology

Java Virtual Machine (Java Virtual Machine) JVM

Insert picture description here

JVM can be understood as a virtual computer system that can run Java bytecode

It has an interpreter component that can realize the communication between Java bytecode and the computer operating system

For different operating platforms, there are different JVMs.

JVM shields the difference of the underlying operating platform and realizes "compile once, run anywhere".

Garbage Collector GC

The memory space that is no longer used should be reclaimed-garbage collected. In languages ​​such as C/C++, the programmer is responsible for recycling useless memory. The Java language eliminates the programmer's responsibility to reclaim useless memory space: JVM provides a system thread to track the allocation of storage space. And when the JVM is idle, check and release the storage space that can be released. The garbage collector is automatically activated during the running of the Java program, and the programmer cannot precisely control and intervene.

Conceptual diagram of the composition of Java SE

Insert picture description here

JDK与JRE

JDK (Java Development Kits)-Java development tool set JRE (Java Runtime Environment) Java runtime environment

Insert picture description here

JDK mainly contains three parts, the first part is the Java runtime environment, JVM . The second part is the basic class library of Java , the number of this class library is still very considerable. The third part is Java development tools , they are all tools to help you better use Java.

Java API (application programming interface): Java compiler (javac.exe), Java runtime interpreter (java.exe), Java documentation tool (javadoc.exe) and other tools and resources

The three main functions of JRE: Load code: completed by the class loader;

Verification code: completed by byte code verifier;

Execution code: Completed by the runtime interpreter.

Java program

The term program comes from life, and usually refers to an established way and process of completing certain tasks. In daily life, a program can be regarded as a description of the execution process of a series of actions

A program in a computer: A collection of ordered instructions (ie commands) written in order to allow the computer to perform certain operations or solve certain problems

Types of Java programs: command line programs; GUI programs; Applet programs; J2EE web applications

Three steps of Java program development:

1. Write the source program (such as HelloWorld.Java);

2. Compile the program (HelloWorld.class is generated by the compiler);

3. Run (run on the Java runtime platform)

grammar

Annotation: There are three annotation methods for java programs:

Single line comment: // comment

Multi-line comment: /* comment*/

Document note: /** note*/ (this note can directly display the note of the method when the mouse is placed on the method)

The location of the java code: class must be written in the .java file (refer to HelloWorld project for details)

Grammar rules:

  1. java is strictly case sensitive
  2. java is a free-form language
  3. The code is divided into structure definition statement and function execution statement (there is {} after the structure definition statement, and the function execution statement must end with a semicolon)

Master the environment construction

eclipse download and installation

Log in to eclipse official website: www.eclipse.org

Insert picture description here

Click to download the package

Insert picture description here

Click on Windows x86_64, then jump to the page and click to download (Note: If you jump to the donation page, you can re-enter without donating, eclipse is completely open source and free)

Download the compressed package and unzip it.

Then open eclipse.exe, set the folder where the workspace is located, and then you can open it normally.

But it's not over yet. You need to install the JDK to make the opened eclipse.exe compile and run the Java program normally.

JDK download and installation

Log in to Oracle official website: www.oracle.com

Then click on the view account in the upper right corner to log in to the account (the oracle account needs to be registered, and you may need to go over the wall to register, here is not how to use vpn to go over the wall)

Then click on the product above to select Java, swipe down the webpage to find Download Java immediately, and then click to enter the page

Insert picture description here

Click JDK Download to get the JDK installation package

Why choose JDK 11? Because JDK 11 is the latest Java long-term support version

Then open and follow the package all the way to install according to the process. Note that the installation location is best for beginners to choose the default installation path so that it is easy to remember, even if you forget it, it is easy to find the default installation path online.

This is not over yet, there is the last step to configure environment variables

Enter in the Win10 search bar: environment variables to open the following interface

Insert picture description here

Click the environment variable button to open the interface

Insert picture description here

Look at the system variables in the lower half and find Path. There are two ways to set JDK system variables, it is recommended to use the second way

Method 1: After finding the Path, double-click to open it, and then create a new line to enter the file path of the JDK installation. The default path is: "C:\Program Files\Java\jdk-11.0.6\bin", and then move the line up Go to the first line of all variables in Path (ps: If multiple versions of JDK are not moved to the first line, the latest version of the installed JDK will not be used by the system, and the old version of JDK may be used) And when the JDK version needs to be frequently changed, it is inconvenient to frequently change the path of the bin

Method 2: Create a new system variable named JAVA_HOME and its address is the JDK installation path, for example: "C:\Program Files\Java\jdk-11.0.6", and then create a new line in Path: %JAVA_HOME%/bin The JDK configuration can be realized. At the same time, when changing the JDK version in the future, you only need to change the JDK installation path of JAVA_HOME, without changing the content in the Path path, which is more convenient and faster.

After configuring the JDK, eclipse can run normally

Build your first Java project with eclipse

After opening eclipse, click new above, select project and continue to select java project to create a java project, and then follow the process, just fill in the project name, I created the first java project day01_test1,

Insert picture description here

Right-click src, select new, select class, create package day01 and Demo1.java, Demo2.java

Insert picture description here

Then learn the automatic shutdown program, Demo1 is the automatic shutdown program, Demo2 is the cancel automatic shutdown setting program

At the same time, I learned how to use System.out.println ("output content") to output. The following is a program display diagram

Insert picture description here

Insert picture description here

Java basic syntax

Binary overview

The data in the computer is stored in binary numbers.

Binary: Enter one every two. That is, there are only two values, 0 and 1. Such as: decimal 10 is saved as binary 1010 in the computer

The storage unit of information in a computer:

Bit: Represents a binary number 0 or 1, which is the most basic unit for computer storage and processing of information.

Byte: A byte consists of 8 bits. It represents 8 binary digits as a complete processing unit.

Complement code: In fact, the binary value in the computer is expressed in the form of complement code. The form of the complement of a positive number is the same as the original code.

The complement of a negative number is: in the binary form of the absolute value of the number, the bit is inverted and then 1 is added. It can be seen that the highest bit (leftmost bit) of the two's complement value is the sign bit: the bit is 0, which means the value is positive; the bit is 1, which means the value is negative. The main reason: using the complement code, the sign bit and other bits can be processed uniformly; at the same time, the subtraction can also be processed as the addition. In addition, when adding two numbers represented by one's complement, if the highest bit (sign bit) has a carry, the carry is discarded.

Interchange between binary and decimal:

Binary numbers are converted to decimal numbers and added by weight: First write the binary numbers as weighted coefficient expansions, and then sum them according to the decimal addition rules.

The decimal number is converted into binary integer part: "Division by 2 take remainder, reverse order" method Decimal part: "Multiply by 2 round, order" method The integer part is actually a division method

Hexadecimal binary representation is too verbose, so hexadecimal is generally used in programs. Hexadecimal: The base is sixteen, and every sixteen is entered. It uses abcdef to represent values ​​above 0-9. The hexadecimal data in Java should start with 0x or 0X. Such as: 0x23D hexadecimal to binary conversion, only need to replace each hexadecimal digit with the corresponding four binary digits.

Octal Octal: The base is eight. Octal data in Java should start with 0. Such as: 0123 octal to binary: just replace each octal digit with the corresponding three binary digits. Current computer systems rarely use octal systems.

variable

A variable is a memory unit allocated by the system for the program to store various types of data. Since the data in the storage unit can be changed, it is named "variable

Variable classification:

Divided by the data type to which it belongs: Basic data type variables refer to data type variables

Divided by the declared position: Local variables: variables defined inside methods or statement blocks Member variables: variables defined outside methods and inside classes

Special attention: there can be no variable declaration outside the class!!!

Due to the different size of the data content of the variable record, the required storage unit size is different. The concept of data type is used in the Java language to describe it. Byte is a unit of measurement used by computer information technology to measure storage capacity, as a unit A string of binary digits to be processed is a small unit of information. A byte in Java is an eight-bit byte, that is, it contains an eight-bit binary number.

Insert picture description here

The Java language provides eight basic types. Six types of numbers (four integers, two floats), one character type, and one Boolean type.

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

Reference type

  • In Java, reference type variables are very similar to C/C++ pointers. The reference type points to an object, and the variable that points to the object is a reference variable. These variables are specified as a specific type when they are declared, such as Employee, Puppy, etc. Once the variable is declared, the type cannot be changed.
  • Objects and arrays are all reference data types.
  • The default value of all reference types is null.
  • A reference variable can be used to refer to any compatible type.
  • Example: Site site = new Site("Runoob").

Java constants

Constants cannot be modified while the program is running.

Use the final keyword to modify constants in Java, the declaration method is similar to that of variables

The Java language supports some special escape character sequences.

Insert picture description here

Automatic type conversion

Integer, real (constant), and character data can be mixed. In the operation, different types of data are first converted to the same type, and then the operation is performed.

Conversion from low-level to high-level.

------------------------------------>

byte,short,char> int> long> float> double 

Data type conversion must meet the following rules:

  • \1. The boolean type cannot be converted.

  • \2. The object type cannot be converted into an object of an unrelated class.

  • \3. When converting a large-capacity type to a small-capacity type, forced type conversion must be used.

  • \4. The conversion process may cause overflow or loss of precision, for example:

int i =128;   
byte b = (byte)i;

Because the byte type is 8 bits and the maximum value is 127, when int is cast to byte type, the value 128 will cause overflow.

  • \5. The conversion of floating-point numbers to integers is obtained by discarding decimals instead of rounding, for example:
(int)23.7 == 23;        
(int)-45.89f == -45

Automatic type conversion

The number of bits of the data type before conversion must be lower than that of the data type after conversion. For example, if the number of bits of the short data type is 16 bits, the int type with the number of bits 32 can be automatically converted, and the number of bits of the float data type is the same It is 32, which can be automatically converted to a 64-bit double type.

Insert picture description here

Forced type conversion

  • \1. The condition is that the converted data types must be compatible.
  • \2. Format: (type) value type is the data type to be coerced

Implicit coercion

  • \1. The default type of integer is int.
  • \2. This situation does not exist for floating-point types, because the number must be followed by F or f when defining the float type.

method

Overview: The method is used to encapsulate a specific logic function. The main elements of the method are: permission modifier method name, parameter list and return value.

format:

Permission modifier return value type declaration method name (parameter list) {

The logical function encapsulated in the method;

return return value;

}

Detailed explanation of permission modifiers

Insert picture description here

Detailed return value

After the method call ends, a piece of data can be returned, which is called the return value. The method must specify the type of return value when it is declared. Through the return statement, the function of the return statement is to end the method and return the data. If the method has no return value (that is, the method does not need to return data), the return value type needs to be declared as void.

Note that return usually only returns one value, and cannot return multiple values

Detailed parameter list

Method parameter list: the data that needs to be processed by the method passed to the method when it is called. When defining a method, you need to declare the parameter variables required by the method. When the method is called, the actual parameter value is passed to the parameter variable of the method. It must be ensured that the type and number of passed parameters conform to the method declaration.

Note: There is only value passing in Java! ! ! (How to understand?, refer to the explanation in the following URL:

https://www.zhihu.com/question/31203609;https://blog.csdn.net/bjweimengshu/article/details/79799485

ASCII code table

Insert picture description here

Operator

Arithmetic Operator

Arithmetic operators are used in mathematical expressions, and their functions are the same as in mathematics. The following table lists all arithmetic operators. The example in the table assumes that the value of integer variable A is 10 and the value of variable B is 20:

Insert picture description here

++: If it is a ++ variable: add 1 to this variable first, and then perform other operations. If it is a variable ++: perform other operations of the current statement first, and then add 1 to this variable

–: If--Variable: Decrease this variable by 1 before performing other operations. If it is a variable --: perform other operations of the current statement first, and then decrement this variable by 1

Assignment operator

The assignment operator is used to assign a value to a variable, the order of operations is from right to left

Insert picture description here

Relational operator

The role of the relational operator is to compare the operands on both sides, and the result is always boolean; the value of the instance integer variable A in the table is 10, and the value of the variable B is 20:

Insert picture description here

Logical Operators

Logical operators are used to perform operations on expressions with boolean results. The result of the operation is always boolean, assuming that the Boolean variable A is true and the variable B is false

Insert picture description here

Bit operator

Java defines bitwise operators, which are applied to integer types (int), long integers (long), short integers (short), character types (char), and byte types (byte). Bit operators act on all bits and perform bitwise operations. The following table lists the basic operations of bitwise operators, assuming that the value of integer variable A is 60 and the value of variable B is 13:

Insert picture description here

String concatenation operator

String s="He" + "llo"; As a result, "Hello" "+" can be used for string concatenation, and can also concatenate a string with other data types into a new string. Such as: String s="x" + 123; the result is "x123"

Note: + can be used in System.out.println ("Chinese characters" + variables) to simultaneously output Chinese characters and variables

Ternary operator

Conditional operators are also called ternary operators. This operator has 3 operands and needs to determine the value of the Boolean expression. The main point of this operator is to determine which value should be assigned to the variable.

variable x = (expression) ? value if true : value if false

Operator precedence

When multiple operators appear in an expression, who comes first? This involves the issue of operator precedence. In a multi-operator expression, the different operator precedence will cause the final result to be very different. The operator with the highest priority in the following table is at the top of the table, and the operator with the lowest priority is at the bottom of the table.

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40694640/article/details/112597955