Java basic grammar (1)-first knowledge of Java

One, Java language overview


1. What is Java


  Java is an excellent programming language with a pleasing syntax and easy-to-understand semantics.

  Not only that, Java is also a technical system formed by a series of computer software and specifications. This technical system provides a complete support environment for software development and cross-platform deployment, and is widely used in embedded systems, mobile terminals, enterprise servers, Various occasions such as mainframe.


  Let's use a set of data officially given by Java to get a feel for how popular Java is.


Insert picture description here

2. The popularity of Java


  In the major programming language rankings, the Java language has always been at the forefront and is a very mainstream computer language.


  The following provides the ranking list of computer languages ​​in recent years


Insert picture description here
  Java still occupies the mainstream position in the world's computer languages.


3. Java features


(Just for understanding, we will gradually feel it in the following study)


(1) Simplicity

  Java grammar is a "pure version" of C++ grammar. There are no header files, pointer arithmetic (even pointer syntax), structures, unions, operator overloading, virtual base classes, etc. Not only that, the Java development environment is far beyond the development environment of most other programming languages.


(2) Object-oriented

  What is object-oriented? Here we use a carpenter as an analogy. An "object-oriented" carpenter always pays attention to the chair that is made, and the second is the tool used; and a "non-object-oriented" carpenter first considers the used tool.

  In the Java world, everything is an object.

  The object-oriented features of Java are on par with C++, and the main difference from C++ is multiple inheritance. In Java, it is replaced by a simpler interface concept. And compared with C++, Java provides richer runtime introspection functions.


(3) Distributed (microservices)

  Java has a rich library of routines for handling TCP/IP protocols like HTTP and FTP. Java applications can open and access objects on the network through URLs, just as convenient as accessing local files.


(4) Robustness

  The biggest difference between Java and C++ is that the pointer model adopted by Java can eliminate the possibility of rewriting memory and corrupting data (for those who have spent several hours checking memory conflicts caused by pointer bugs, they must really like Java's this One feature). Not only that, the Java compiler can detect many problems that can only be detected at runtime in other languages.


(5) Security

  Java is suitable for network/distributed environment. In order to achieve this goal, a lot of energy has been invested in security. Use Java to build an anti-virus and anti-tampering system.

(6) Portability

  Unlike C/C++, there is no "rely on specific implementation" in the Java specification. The size of the basic data types and related operations are clearly explained. For example, int in Java is always a 32-bit integer, while in C/C++, int may be a 16-bit integer, a 32-bit integer, or other sizes specified by the compiler provider. In Java, data types have a fixed size, which eliminates the main headache when porting code.


(7) Interpretative

  The Java interpreter can execute Java bytecode on any machine where the interpreter is ported. Because linking is an incremental and lightweight process. So the development process has become faster and more exploratory.


(8) High performance

  Although I am satisfied with the interpreted bytecode performance, more efficient performance may be required in some situations. The bytecode can be dynamically translated (at runtime) into the machine code corresponding to the specific cpu running the application.


(9) Multithreading

  Java was very advanced at the time. It is the first mainstream language to support concurrent programming. Multithreading can bring better interactive response and real-time behavior. Concurrent programming is by no means easy, but Java performs well in this area and can manage this work well.


(10) Dynamic

  Java is more dynamic than C/C++. It can adapt to the evolving environment. New methods and instance variables can be added freely in the library without any impact on the client. Finding the runtime type information in Java is very simple (the feature of reflection, you will learn later)


Second, the historical development of Java

  Here we briefly introduce that the Java language originated from the Oak project led by Sun Company James Gosling in 1991. In 1995, Sun Company officially named Java and proposed "Write once, Run anywhere"


the founder of java

James Gotling

Insert picture description here
The origin of java naming


  Gosling loves to drink coffee to refresh himself while writing computer programs. So when naming java, he thought about coffee-Java Island is rich in coffee, so it was named java. We can observe the official icon of java, which is a cup of steaming coffee.


Insert picture description here


Three, Java development environment installation


The first step is to install JDK


Install JDK, blog version:

https://www.cnblogs.com/gaobo123/articles/13304599.html


Install JDK, video explanation version:

https://www.bilibili.com/video/BV1N54y1U7AA


1. Variable name: JAVA_HOME

Variable value: the value is the installation directory of the JDK


2. Variable name: Path

Variable value: C:\Program Files\Java\jdk1.8.0_131\bin


3. Configure CLASSPATH

Variable name: CLASSPATH

Variable value: .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar

Pay attention to the previous one. This dot cannot be omitted



The second step IDEA installation steps


https://www.bilibili.com/video/BV1HA411s7xG

Here we explain what is JDK, and what is the concept of IDEA and other operating environments.


Insert picture description here
Insert picture description here
Insert picture description here

Fourth, run the Java program


  Let's not use IDEA Java code editor, first use Notepad to write the code, and try to run the Java code program


  First create a file in a folder, create a text file, change the suffix name to .java, and then open it with notepaid++, or Sublime Text, we will write the Hello World code in Java language.


Insert picture description here

  Press Ctrl + S to save the code, and then the java file code is successfully written to the disk. At this time, the java code is just a bunch of strings written to the disk, so how do we run it?


  Win +R open the run box, enter cmd, open the command line.


Insert picture description here

Insert picture description here

Insert picture description here
  Hello World is printed out at this time! The string that runs the Java code successfully.

  In this process, the compilation process is as follows:

Insert picture description here

  The bytecode file stores the binary numbers of the related information of the class in the java file, and the bytecode file will continue to run on the JVM.


5. Analyze Java basic grammar from Hello World


Insert picture description here

Insert picture description here
  So if we write another public class in Java Notepad


Insert picture description here

  The compile Javac + file name in the command line is as follows:


Insert picture description here


  What does this compilation error indicate?

Insert picture description here
  When we remove the public before test in the Java file

Insert picture description here
  Compile again


Insert picture description here

Insert picture description here
  A Test class is generated in the folder. What does this mean?

Insert picture description here
Notes for writing:
Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here
  Add public static before the main function


  A function is called a method in Java. The writing form of the method is as shown above, which is roughly the same as the C language, but the difference is that the access modifier qualifier needs to be added before the return value.


  Here we will briefly understand the access restriction modifier

Insert picture description here
what is this?Insert picture description here

It is equivalent to the array int arr[] in C language

This can be written as int []

What is this again?Insert picture description here

Equivalent to printf ("% d \ n", 10) in c language;


Six, use IDEA to write java code


How to use IDEA?

Create project

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here

Finally, we write the code in the corresponding editing area.


How does the written code work?


Insert picture description here

  Here we explain that psvm is the shortcut key of the main method, and sout is the shortcut key of printf{" \n", },.


Seven, Java beginner knowledge details


1. Java Notes

Insert picture description here

2. String splicing


public class Test {
    
    

    public static void main(String[] args) {
    
    
        int a=10;
        System.out.println(a);  //行注释

        int b=20;
        System.out.println(b);

        System.out.println("a:"+a+"  b:"+b);
    }
}

What is the output result?

Insert picture description here
Description:


(1). Any variable of string splicing will become a string, at this time + means splicing


So in this code, will a+b calculate the result?

  System.out.println("a:"+a+b);

Code display effect:
Insert picture description here
There is no addition, as long as there is a string in the front, the + after it will not be operated on, it just means splicing.

So in this code, what result is displayed?

  System.out.println(a+b+"hehe");

Compilation result:
Insert picture description here

Description:

(2) If the string is not the first to appear in front, then it must be calculated first


3. int in java


  In the C language, int occupies 2 bytes on a 16-bit platform, int occupies 4 bytes on a 32-bit platform, and 8 bytes on a 64-bit platform.


  However, int is 4 bytes in Java! ! ! There is no so-called number of platforms, this is the so-called portability of java! ! !


Portability : No matter how many platforms, Java code can run! !

Insert picture description here

Cross-platform

  It means that the code written in windows can also run on the Mac. why? ? Because the Java code runs on the virtual machine (JVM) -> JDK.


5. The range that int can represent in Java


  In the c language, there is a signed number, unsigned , but remember, there is no so-called unsigned number in Java! ! !

Int binary representation:

Insert picture description here
So the range of int is

Insert picture description here
Insert picture description here


This sharing ends here, thank you for your appreciation and attention! ! !




To be continued...

Guess you like

Origin blog.csdn.net/rain67/article/details/115307923