Introduction to Java and basic syntax

1. Introduction to java

Demonstrate Java programming through a simple example, create a file HelloWorld.java (the file name must be consistent with the class name) , the code is as follows:


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

Note: Both String args[] and String[] args can be executed, but String[] args is recommended to avoid ambiguity and misunderstanding.

运行以上实例,输出结果如下:

$ javac HelloWorld.java
$ java HelloWorld
Hello World

Execution command analysis:

Above we have used two commands javac and java.

javac is followed by the file name of the java file, for example HelloWorld.java. This command is used to compile java source files into class bytecode files , such as: javac HelloWorld.java.

After running the javac command, if there is no error in the successful compilation, a HelloWorld.class file will appear.

Java is followed by the class name in the java file, for example, HelloWorld is the class name, such as: java HelloWorld.

Note: Do not add .class after the java command.

2. Java Basic Grammar

A Java program can be thought of as a collection of objects that work together by calling each other's methods. The following briefly introduces the concepts of classes, objects, methods, and instance variables.

对象:对象是类的一个实例,有状态和行为。例如,一条狗是一个对象,它的状态有:颜色、名字、品种;行为有:摇尾巴、叫、吃等。

类:类是一个模板,它描述一类对象的行为和状态。

方法:方法就是行为,一个类可以有很多方法。逻辑运算、数据修改以及所有动作都是在方法中完成的。

实例变量:每个对象都有独特的实例变量,对象的状态由这些实例变量的值决定。
  • first java program

Let's look at a simple Java program that will output the string Hello World


public class HelloWorld {
    
    
    /* 第一个Java程序
     * 它将输出字符串 Hello World
     */
    public static void main(String[] args) {
    
    
        System.out.println("Hello World"); // 输出 Hello World
    }
}

insert image description here

  • basic grammar

When writing Java programs, you should pay attention to the following points:

大小写敏感:Java 是大小写敏感的,这就意味着标识符 Hello 与 hello 是不同的。

类名:对于所有的类来说,类名的首字母应该大写。如果类名由若干单词组成,那
么每个单词的首字母应该大写,例如 MyFirstJavaClass 。

方法名:所有的方法名都应该以小写字母开头。如果方法名含有若干单词,则后面的
每个单词首字母大写。
源文件名:源文件名必须和类名相同。当保存文件的时候,你应该使用类名作为文件
名保存(切记 Java 是大小写敏感的),文件名的后缀为 .java。(如果文件名和类名
不相同则会导致编译错误)。
主方法入口:所有的 Java 程序由 public static void main(String[] args) 
方法开始执行。
  • Java identifier

All components of Java need names. Class names, variable names, and method names are all called identifiers.

There are a few things to note about Java identifiers:
insert image description here

  • Java modifiers

Like other languages, Java can use modifiers to modify methods and properties in classes. There are two main types of modifiers:

访问控制修饰符 : default, public , protected, private
非访问控制修饰符 : final, abstract, static, synchronized
  • Java variables

There are mainly the following types of variables in Java

局部变量
类变量(静态变量)
成员变量(非静态变量)
  • java array

An array is an object stored on the heap, which can hold multiple variables of the same type.

  • Java enum

Enumerations restrict variables to only pre-set values . Using enums can reduce bugs in your code.

For example, let's design a program for a juice shop that will limit the juice to small, medium, and large cups. That means it doesn't allow customers to order juices other than those three sizes.


class FreshJuice {
    
    
   enum FreshJuiceSize{
    
     SMALL, MEDIUM , LARGE }
   FreshJuiceSize size;
}
 
public class FreshJuiceTest {
    
    
   public static void main(String[] args){
    
    
      FreshJuice juice = new FreshJuice();
      juice.size = FreshJuice.FreshJuiceSize.MEDIUM  ;
   }
}

Note: Enumerations can be declared individually or within a class. Methods, variables, constructors can also be defined in enums.

  • Java keywords

The Java keywords are listed below. These reserved words cannot be used in the names of constants, variables, and any identifiers.

insert image description herewait.

  • java annotation

Similar to C/C++, Java also supports single-line and multi-line comments. Characters in comments will be ignored by the Java compiler.


public class HelloWorld {
    
    
   /* 这是第一个Java程序
    * 它将输出 Hello World
    * 这是一个多行注释的示例
    */
    public static void main(String[] args){
    
    
       // 这是单行注释的示例
       /* 这个也是单行注释的示例 */
       System.out.println("Hello World"); 
    }
}

  • Java blank line

Blank or commented lines are ignored by the Java compiler.

  • inherit

In Java, a class can be derived from other classes. If you want to create a class, and there is already a class with the properties or methods you need, then you can inherit the newly created class from that class.

Using inherited methods, you can reuse methods and properties of existing classes without rewriting these codes. The inherited class is called a super class, and the derived class is called a sub class.

  • interface

In Java, an interface can be understood as a protocol for communicating between objects. Interfaces play an important role in inheritance.

The interface only defines the methods used by the derivation, but the specific implementation of the method depends entirely on the derived class.

  • The difference between Java source program and compiled operation

insert image description here
Learn from "https://www.runoob.com/java/java-basic-syntax.html"

Summarize

The external things are illusory, and the only way to go into your own heart is the wisest choice. Only by constantly reflecting on and strengthening ourselves, and exploring the meaning of life, can we better break through ourselves and enable ourselves to reach a higher level.

Guess you like

Origin blog.csdn.net/weixin_51884452/article/details/130516547