Introduction to Java (1) Basic syntax, type conversion, operators

1. Introduction


1 Introduction

Java is the general name for the Java object-oriented programming language and Java platform launched by Sun Microsystems in May 1995. It was jointly developed by James Gosling and colleagues and officially launched in 1995.
Later, Sun was acquired by Oracle, and Java became an Oracle product.

  • Java is divided into three systems:

    • JavaSE (J2SE) (Java2 Platform Standard Edition, java platform standard edition)
    • JavaEE (J2EE) (Java 2 Platform, Enterprise Edition, java platform Enterprise Edition)
    • JavaME (J2ME) (Java 2 Platform Micro Edition, java platform micro edition).

In June 2005, the JavaOne conference was held, and SUN released Java SE 6. By this time, various versions of Java had been renamed to remove the number "2": J2EE was renamed Java EE, J2SE was renamed Java SE, and J2ME was renamed Java ME.

2. JDK composition

  • JVM (Java Virtual Machine): Java virtual machine, where Java programs are actually run.
  • Core class library: Programs written by Java and called by programmers themselves
  • JRE (Java Runtime Environment): Java runtime environment
  • JDK (Java Development Kit): Java development tool kit

2. Basic grammar


1. Basic data types

Insert image description here

    public static void main(String[] args) {
        // 1.byte字节型 占1个字节 -128 - 127
        byte number = 98;
        System.out.println(number);

        // 2.short短整型 占2个字节
        short money = 30000;

        // 3.int 整型 默认的类型 占4个字节
        int it = 234234412;

        // 4.long长整型 占8个字节
        long lg = 123423123;
        // 注意:随便写一个整数默认是int类型
        // 如果希望整数变成long类型,在后面加L/l
        long lg2 = 1231231233455434L;

        // 5.浮点型(小数)
        // float单精度 占4个字节
        // 注意:随便写一个小数字默认是double类型,float需要在后面加上F/f
        float score = 92.1F;

        // 6.double双精度 占8个字节
        double score2 = 982.13;

        // 7.字符类型:char
        char ch = 'a';
        char ch2 = '中';

        // 8.布尔类型:boolean
        boolean rs = true;
        boolean rs2 = false;

        // --------------引用数据类型--------------
        String name = "西门吹雪";
        System.out.println(name);
    }

2. Keywords

Insert image description here

3. Identifier

(1) Introduction:
A name composed of some characters and symbols, a rule for naming classes, methods, variables, etc.

(2) Requirements

  • Basic requirements: composed of numbers, letters, underscores (_) and dollar signs ($), etc.
  • Mandatory requirements: cannot start with a number, cannot be a keyword, case sensitive

(3) Naming guidelines

  • Variable name: meets the identifier rules, it is recommended to be in English, meaningful, with the first letter lowercase, and meets the "camel case", int studyNum = 23
  • Class name: meets the identifier rules, it is recommended to be in English, meaningful, with the first letter capitalized, and satisfies the "camel case", HelloWorld.java

3. Type conversion


1. Automatic type conversion

Variables with a small type range can be directly assigned to variables with a large type range
byte --> short --> int --> long --> float --> double

    public static void main(String[] args) {
        byte a = 12;
        int b = a;
        System.out.println(b);
    }

2. Expression automatic type conversion

In expressions, variables of small range types will be automatically converted to current larger range types and then operated on
byte, short, char --> int --> long --> float --> double.
In expressions, byte, short, char Short and char are directly converted into int types to participate in operations.

    public static void main(String[] args) {
        byte a = 10;
        int b = 20;
        double c = 1.0;
        double rs = a + b + c;
        System.out.println(rs);

        // 会自动转换为int类型,需要int来接收
        byte i = 10;
        byte j = 20;
//        byte k = i + j;
        int k = i + j;
    }

3. Forced type conversion

Forced type conversion may cause data (loss) overflow. The
floating point type is forced to be converted into an integer type. The decimal part is directly discarded and the integer part is retained and returned.

    public static void main(String[] args) {
        int a = 20;
//        byte b = a;  //报错
        byte b = (byte) a;  //快捷键byte,Alt+Enter
        System.out.println(b);
    }

4. Operators


1. Basic arithmetic operators

Symbols that operate on literals or variables

Insert image description here

    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        System.out.println(a + b);
        System.out.println(a - b);
        System.out.println(a * b);
        System.out.println(a / b);
        System.out.println(a * 1.0 / b);
        System.out.println(a % b);
    }

result:

13
7
30
3
3.3333333333333335
1

2. Data splitting case

    public static void main(String[] args) {
        int data = 321;

        // 1.各位
        int ge = data % 10;
        System.out.println(ge);

        // 2.十位
        int shi = data / 10 % 10;
        System.out.println(shi);

        // 3.百位
        int bai = data / 100;
        System.out.println(bai);
    }

result:

1
2
3

3. Add symbols as connectors

The "+" symbol is used as a connector when operating on strings, and the result is still a string.

    public static void main(String[] args) {
        int a = 8;
        System.out.println("wielun" + a);
        System.out.println("wielun" + a + 'a');
    }

result:

    public static void main(String[] args) {
        int a = 8;
        System.out.println("wielun" + a);
        System.out.println("wielun" + a + 'a');
    }

4. Increment and decrement operators

Put it in front of the variable, first perform +1 and -1 on the variable, and then perform operations on the value of the variable. Place it
after the variable, first perform operations on the value of the variable, and then perform +1 and -1 on the variable.

Insert image description here

    public static void main(String[] args) {
        int a = 8;
        int b = ++a;
        System.out.println(a);
        System.out.println(b);

        int c = 8;
        int d = c++;
        System.out.println(c);
        System.out.println(d);
    }
    //        ------------ 拓展案例 ------------
        int i = 6;
        int j = 8;
        // i 6 7 8 7
        // j 8 7 6 7
        // rs  6 + 8 - 7 + 7 - 8 + 7 + 2
        int rs = i++ + ++i - --j + j-- - i-- + ++j + 2;
        System.out.println(i);  // 7
        System.out.println(j);  // 7
        System.out.println(rs); // 15

result:

9
9
9
8
7
7
15

5. Assignment operator

Insert image description here

6. Relational operators

It is a symbol for conditional judgment on data, and will eventually return a Boolean result of comparison.

Insert image description here

7. Logical operators

You can put the Boolean results of multiple conditions together and finally return a Boolean result.

Insert image description here

8. Ternary operator

Format: conditional expression? value 1 : value 2

    public static void main(String[] args) {
        double score = 98;
        String rs = score >= 60 ? "yes" : "no";
        System.out.println(rs);
    }

5. Case technology


1. Keyboard entry

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // 等待用户输入数据
        System.out.println("please input your age:");
        int age = sc.nextInt();
        System.out.println("your age is : " + age);
    }

Guess you like

Origin blog.csdn.net/Dream_ya/article/details/122472888