Getting to know Java for the first time——Introduction to Java

First, the basic structure of the Java file

        Generally, a java file corresponds to a java class (class), and in a class, a class has its own main function.

        The main function also has its own structure, which consists of modifiers, return value, function name, etc. The detailed structure is as follows.

public class MyFirstJava {
    //public 访问修饰符,表示该类被访问的权限
    //class 表示这段内容表示一个类
    public static void main(String[] args) {
        //这里为主函数,执行类文件时,执行的就是主函数内的代码
        //public 同上,为访问修饰符
        //static 关键字,表示静态方法,可以直接调用,不需要通过创建对象来调用方法
        //void 返回类型,void表示函数执行完后,什么都不返回。
        //main 函数名,这里表示主函数

    }
}

2. Simple input and output 

        The output string can use the System.out.println() statement;

        For input, you need to create an object of the Scanner class first, and then call the nextInt() method to implement it.

       If you want to understand this part, you can first understand the concepts of classes (class), objects and methods in java.

        System.out.println("Hello World!");//调用系统方法,在控制台输出Hello World
        Scanner input = new Scanner(System.in);//创建一个Scanner对象,命名为input。
        int num1 = input.nextInt();//创建一个int类型的变量,命名为num1,值为输入的内容(这里只能输入int类型的数值)
//      int 数据类型是32位、有符号的以二进制补码表示的整数;
//        最小值是 -2,147,483,648(-2^31);
//        最大值是 2,147,483,647(2^31 - 1);
//        一般地整型变量默认为 int 类型;
//        默认值是 0 ;
        System.out.println("你输入了"+num1);

        Here we have involved the int data type. Regarding the data type, you can learn more about the basic data types in Java.

3. Input and output strings 

        Note that strings are a relatively special data type.

System.out.println("你输入了"+num1);
        //在 System.out.println("你输入了"+num1); 中
        //""中的内容表示字符串,符合+表示将字符串和变量num1连接起来,最终输出
        //下面就会将三个字符串拼接输出:请输入你的名字:
        String str1 = "请输入";
        //创建一个字符串变量,命名为str1,赋值 请输入
        String uName = "你的名字";
        //创建一个字符串变量,命名为str1,赋值 请输入
        System.out.println(str1+uName+":");
        uName = input.next();//调用input的next()方法,获取控制台输入的字符串(注意:获取到空格为止)
        //给变量uName赋值,值为控制台输入的内容
        System.out.println("你的名字是:"+uName);

4. Simple operators 

        There are many operators in Java, +-*/= and so on.

        System.out.println("请输入数字1:");
        int num1 = input.nextInt();
        System.out.println("请输入数字2:");
        int num2 = input.nextInt();
        System.out.println(num1+num2);
        //当输出两个int变量时,符号+,表示加法的+号,这里就会输出num1和num2之和
        int sum = num1+num2;
        //定义一个int变量,赋值时num1、num2之和
        System.out.println(sum);

 5. Complete code display

public class MyFirstJava {
    //public 访问修饰符,表示该类被访问的权限
    //class 表示这段内容表示一个类
    public static void main(String[] args) {
        //这里为主函数,执行类文件时,执行的就是主函数内的代码
        //public 同上,为访问修饰符
        //static 关键字,表示静态方法,可以直接调用,不需要通过创建对象来调用方法
        //void 返回类型,void表示函数执行完后,什么都不返回。
        //main 函数名,当我们需要自己写一个函数的时候,可以给函数起名;主函数格式固定,不需要修改
        System.out.println("Hello World!");//调用系统方法,在控制台输出Hello World
        Scanner input = new Scanner(System.in);//创建一个Scanner对象,命名为input。
        int num1 = input.nextInt();//创建一个int类型的变量,命名为num1,值为输入的内容(这里只能输入int类型的数值)
//      int 数据类型是32位、有符号的以二进制补码表示的整数;
//        最小值是 -2,147,483,648(-2^31);
//        最大值是 2,147,483,647(2^31 - 1);
//        一般地整型变量默认为 int 类型;
//        默认值是 0 ;
        System.out.println("你输入了"+num1);
        //在 System.out.println("你输入了"+num1); 中
        //""中的内容表示字符串,符合+表示将字符串和变量num1连接起来,最终输出
        //下面就会将三个字符串拼接输出:请输入你的名字:
        String str1 = "请输入";
        //创建一个字符串变量,命名为str1,赋值 请输入
        String uName = "你的名字";
        //创建一个字符串变量,命名为str1,赋值 请输入
        System.out.println(str1+uName+":");
        uName = input.next();//调用input的next()方法,获取控制台输入的字符串(注意:获取到空格为止)
        //给变量uName赋值,值为控制台输入的内容
        System.out.println("你的名字是:"+uName);
        System.out.println("请输入数字:");
        int num2 = input.nextInt();
        System.out.println(num1+num2);
        //当输出两个int变量时,符号+,表示加法的+号,这里就会输出num1和num2之和
        int sum = num1+num2;
        //定义一个int变量,赋值时num1、num2之和
        System.out.println(sum);

    }
}

Guess you like

Origin blog.csdn.net/Alcaibur/article/details/128790557