Introduction to Java

Time:2021-6-12

Java past and present

Java was first developed by James Gosling (commander Gao, known as the father of Java) of Sun company (which has been acquired by Oracle) in the early 1990s. It was originally named oak. Its goal is to target at the embedded applications of small household appliances. As a result, the market has no response. Who expected the rise of the Internet to revitalize oak? So sun transformed oak and officially released it under the name of Java in 1995. The reason is that oak has been registered, so sun registered the trademark of Java. With the rapid development of the Internet, Java has gradually become the most important network programming language.

Java is between compiled language and interpreted language.In order to ensure that virtual machines developed by different platforms and companies can correctly execute Java bytecode, Sun company has formulated a series of Java virtual machine specifications. From a practical point of view, the compatibility of the JVM is very good, and the lower version of Java bytecode can run normally on the higher version of the JVM.

With the development of Java, sun has divided Java into three different versions

  • Java SE:Standard Edition
  • Java EE:Enterprise Edition
  • Java ME:Micro Edition

What is the relationship between the three?

Introduction to Java

In short, Java se is the Standard Version, including the standard JVM and standard library, while Java EE is the enterprise version. It only adds a large number of APIs and libraries on the basis of Java se to facilitate the development of web applications, databases, message services, etc. the virtual machine used by Java EE applications is exactly the same as Java se.

There is no doubt that Java se is the core of the entire Java platform, and Java EE is necessary for further learning web applications. The familiar frameworks like spring are all part of Java EE open source ecosystem.

Noun explanation

  • JDK: Java Development Kit
  • JRE: Java runtime environment (Java runtime environment, mainly composed of JVM running Java bytecode file, namely Java virtual machine)

The relationship between them is as follows

Introduction to Java

In short, JRE is a virtual machine running Java bytecode. However, if you only have java source code, you need JDK to compile it into Java bytecode, because JDK not only contains JRE, but also provides development tools such as compiler and debugger.

Environment configuration

Learning any programming language, there are predecessors recommendedIDEOr official development tools, as well as the configuration of the running environment, which are necessary. Xiaobian chose it hereIDEA 2021andjdk8

JDK download and installation

1. Download

Official website download link:https://www.oracle.com/java/technologies/javase-downloads.html

The jdk8 version used here is a more stable version, which is recommended. If you can’t download it, you can access it herehttps://pan.baidu.com/s/1tb1AG-FBHpYRsw0Q5U9YLwExtraction code: 3kwk

2. Installation

Take win10 system as an example, you can unzip it after downloading, but you don’t need to install the version. The decompression path should be noted:Do not use paths with Chinese characters. In addition, it is strongly recommended that you change your computer’s drive name and account name to English or Pinyin, so that you can avoid some strange problems. Although these problems may not appear until you install a database or other software later, the world of the program does not recognize Chinese characters.

3. Configuration environment

desktopThis computerAccording to the following operation sequence: right mouse button > Properties > Advanced System Settings > advanced environment variables:

Introduction to Java

After selecting environment variables, we can see the following interface. At this time, select system variable — > new, and the new system variable dialog box will pop up. Enter Java in the variable name_ Home, enter the JDK installation path in the variable value (the JDK of Xiaobian is the directory after decompression), and click OK.

Introduction to Java

We also need to modify the systemPathVariable. Add the following two paths after the variable:

%JAVA_HOME%\bin
%JAVA_HOME%\jre\bin

Introduction to Java

New / modifyCLASSPATHvariable

If there is a classpath variable, select Edit. If not, click new.

Enter / add after the existing variable value:

Variable name: classpath
Variable value:;% JAVA_ HOME%\lib\dt.jar;% JAVA_ HOME%\lib\tools.jar;

Introduction to Java

Verify that the installation is successful

Through the shortcut keyWin+RAnd inputcmdCall up the command line window

Introduction to Java

inputjavaIf the information similar to the figure below appears, the configuration is successful.

Introduction to Java

Then enter the command in turnjavacjava -versionCommand, if there is no error message, it is OK. The latter command is to check your JDK version number. If you forget which version of JDK you installed at that time after a long time, you can check it through this command.

Download and install idea

Download address:https://www.jetbrains.com/idea/download/#section=windows

Xiaobian chose it hereUltimateVersion 64 bit, the general opportunity to download intelligent identification of the current computer system, small series of downloaded exe files, as well as ZIP format, you can try to ha.

Installation is simple, almost all the waynextThat’s it. It is suggested that computer is so laggy that the C disk will be bigger and bigger, and the computer will be stuck.

I suggest you revise itThe font size of the edit areaandDisplay method split line. In the latter case, when you have more methods, it’s not easy to distinguish the beginning and end of the method, especially when you collapse all the methods.

File — > settings, and then modify it by referring to the following figure:

Introduction to Java

File — > settings, and then modify it by referring to the following figure:

Introduction to Java

In addition to the two most basic settings, the others can be adjusted to be the most comfortable to use according to personal preferences.

Hello world

This part of the network, many authors put it behind to explain, because it involves a little more things, here refers to the noun, which for novices, it is difficult to understand, but Xiaobian carefully pondered, in the beginning of the explanation is actually no problem, listen to it is always good, at least have a low heart.

Write the first Java program

Create the first Java program:File — > New — > Project (in this step, you can see our installed JDK) — > next — > next — > you will see the following interface:

Introduction to Java

Project nameIt’s the name of the project we created. You can name it by yourself. English or Pinyin is recommended< br/>
Project locationIt is not recommended to use the path with Chinese where the project we created is placed on the computer.

And then there’s thatfinishNow that we have finished, we will see the following interface:

Introduction to Java

This is the default content provided by the system. Our main work in the early stage is tosrcIn this directory, take the first new Java program as an example:
staysrcRight click, select new — > java class –, enter the class name to be created (the naming rules of class name will be discussed later in this article) – > Enter.

Introduction to Java

For example, Xiaobian, the class name entered here isMainHello

public class MainHello {
    
}

This step is equivalent to creating a java file, but there is no function entry executed by the system. For the moment, this entry can be understood as the entrance of a certain scene in real life (for example, the entrance of a scenic spot). However, there is only one java file entry. Next, we will create this entry function:

You can use shortcut inputmain+Enter to quickly generate this method

Introduction to Java

In fact, it’s very easy to modify the shortcut input. In fact, the default intelligent prompt limit is removed (by default, only uppercase initials will prompt complete system classes and keywords, etc.)

Introduction to Java

The method is as follows:

public class MainHello {

    public static void main(String[] args) {
        
    }
}

At this time, you will see the Green Run button coming out. This is the default entry of the system. The next thing we learn will be done here. Xiaobian first uses the output statement provided by the system here to output the output we are familiar withHello World

public class MainHello {

    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

Then click Run to see the result.

analysis

Here is a simple analysis of the Java program’s entry, that is, the main function. First of all, take a look at the rectangles marked with different colors in the figure below

Introduction to Java

  • publicThis is called in JavaModifier qualifierBesides the first timeprivateprotectedanddefault(this is not written by default). Its function is to limit the access rights of class files or methods in class files. It can be seen from the following figure:

Introduction to Java

  • classIt’s in the Java languagekeywordIn addition, there are many keywords andReserved wordThe latter is that the system is not used for the time being, but it is reserved. Maybe it will be used later, but developers can’t use it. For example:goto. Learn about the following:

Introduction to Java

  • MainHelloThat’s what we defineidentifier In Java language, the naming of identifier is regular

    • Strictly case sensitive
    • Cannot start with a number
    • It can only be completed by one or more of letters, numbers, underscores and $
    • Can’t occupy keywords and reserved words in Java
    • To see the name and know the meaning
    • Nomenclature, recommended hump nomenclature (not required, recommended)

Identifier acts onClass nameMethod namevariableStatement block, which we use most.

  • main(String[] args)Is one of the function forms in Java: with a parametermethod. Functions and methods in Java actually refer to the same thing. Methods in Java can be divided into two types according to the number of parameters:Nonparametric methodandMethod with parameters (the number of parameters can be customized)The parameter type can also be customized.

Here the main method takes a stringarrayA new term appears herearrayIn Java, arrays can be divided into three types according to their dimensionsOne dimensional arrayandMultidimensional array

The understanding of array can be simply understood as a billiard ball in a box. The number on each ball is the characteristic of array, which is called array in Java languageIndexesThe index starts from 0. For example, if there are 10 balls in total, then the index is 0 ~ 9.

Summary

This is the introduction of the relevant content involved in the entry of Java program, and the specific content will be explained in detail later.

data type

Data types in Java can be divided into two categoriesBasic data typeandReference data type. Through the following map, you can see clearly:

Introduction to Java

Base system

There are four forms of integer in Java: decimal (we usually use it), binary, octal and hexadecimal.

  • Binary: starts with 0b or 0b, including numbers from 0 to 1.
  • Decimal system: numbers from 0 to 9.
  • Octal: starts with 0, including numbers from 0 to 7.
  • Hexadecimal: starts with 0 X or 0 x, including numbers 0 ~ 9, and letters a ~ F, a ~ F.

Base conversion

Other decimal to decimal

Formula: the weight power of coefficient * base is added (coefficient refers to each number, base refers to the number of bases, weight refers to right to left, starting from 0, power is the power)

For example: calculate the decimal value of 0x100:

0x100 = 1*16^2 + 0 * 16^1 + 0 * 16^0

       =  16*16

       =  256

Decimal to other decimal

Formula: divide the base (Radix, that is, the base to be converted) and take the remainder (remainder)

For example: calculate the binary of 60:

Introduction to Java

variable

  • In memory
  • According to the scope, it can be divided into local variables and global variables
  • Declaration rule: variable type, variable name, for example: int stuage
  • Variable assignment character:=
  • Variable usage rule: declare before use
  • Variable default value: basic data type has corresponding default value, reference type default value isnull, Boolean, default isfalse

Constant is actually a special case of variable. Constant usefinalTo modify, its value will not be changed twice, generally using capital letters to define.

summary

Yesterday saw a very interesting video about programming, I recommend you to take a lookhttps://youtu.be/dU1xS07N-FA

I believe everyone who studies Java knows this sentence:Compile once, run everywhere. So what is the principle behind it?

“The java compiler does not compile all classes into one machine code program. Instead, it compiles each class independently, not into machine code, but into special intermediate code (bytecode). When the program starts, the bytecode is compiled into machine code. “

This is why Java is widely used in many industries, and more and more developers take it as one of the preferred learning languages.

Xiao Bian has deliberately created a official account.Recommend learning java, will share withjavaRelated content, and to the original, welcome to search attention (attention to send Xiaobian selected quality video tutorial), learn java together!