java basic learning notes

Posted by patrickcurl on Sun, 02 Jan 2022 21:29:05 +0100

1, History of computer language development

  • First generation language: machine language
  • Second generation language: assembly language
  • Third generation language: high level language (process oriented: c language. Object oriented: java, c + +)

Born in 1972 – > born in 1982 – > born in 1995 (Java se: Standard Version [occupy desktop] javaME: mobile version [occupy mobile phone] javaEE: enterprise version [occupy server])

2, java features and advantages

Simplicity

  • object-oriented
  • Portability
  • High performance
  • Distributed
  • Dynamics
  • Multithreading
  • Security
  • Robustness

JDK:java development kit
JRE:java Runtime Environment
JVM:java Virtual Machine

JDK: is a software development kit (SDK) for Java language. Including jre and jvm
[without JDK, java programs (referring to java source code. java files) cannot be compiled. If you want to run only java programs (referring to class or jar or other archive files), ensure that the corresponding JRE is installed.]

3, java Foundation

1. Notes

  • Single line note//
  • Multiline comment/**/
  • Document comments / * **/

2. Identifier

Class names, variable names, and method names are all called identifiers.
be careful:
[1] Start with a letter (A-Z or A-Z), dollar sign ($), or underscore (). It can be followed by the previously mentioned and numbers
[2] Case sensitive
[3] Keywords cannot be used as variable or method names

  • Keyword (abstract new try class float public static...)

3. Data type

  • Strongly typed language: the use of variables complies with regulations, and variables are defined before use
  • Weakly typed language

java data types fall into two categories

  • Basic type

  • Reference data type: class, interface, array

What is a byte: it is the basic unit of data processing in a computer (B)
What is a bit: the smallest unit of data storage within a computer
1B = 8bit (bit)

  • 1KB=1024B
  • 1M=1024KB
  • 1G=1024M

Data type extension

package com.yu.type;

public class MyType {
    public static void main(String[] args) {

        int i=5;
        int i1=010;//octal number system
        int i2=0x10;//hexadecimal
        System.out.println(i);
        System.out.println(i1);
        System.out.println(i2);
    }
}


Floating point type

package com.yu.type;

public class MyType {
    public static void main(String[] args) {

        float a=0.1f; //The rounding error is approximately equal to
        double b=0.1;
        double c=1/10;
        double d=1.0/10;
        System.out.println(a==b);
        System.out.println(c);
        System.out.println(d);
        
        

    }
}


character

package com.yu.type;

public class MyType {
    public static void main(String[] args) {

        String java1 = new String("java");
        String java2 = new String("java");
        System.out.println(java1==java2);
        System.out.println("===================");
        String java3="java";
        String java4="java";
        System.out.println(java3==java4);
        System.out.println("===================");
        System.out.println(java1==java3);



    }
}


4. Type conversion
Low --------------------------------------- > High
byte,short,char–>int–>long–>float–>double

  • Cast: high – > low
  • Automatic type conversion: low – > High

4, Variable, constant, scope

Data type variable name = value: multiple variables of the same type can be declared with commas

Illegal format
1,

  int a,b,c=9;
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

2,

      int a=b=c=9;
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);


Legal form

  int a=1,b=2,c=9;
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);


Global variable: uninitialized, default value of basic type: 0.0, boolean type: false The rest are null by default
Local variable: must be declared and initialized. It is only valid in the method body
Constant: cannot be modified after being set. Use final

Naming conventions for variables

  • Variable, class name, method name: see the meaning of name
  • Class member variables: initial lowercase, hump principle.
  • Local variables: initial lowercase, hump principle
  • Constants: uppercase letters and underscores
  • Class name: initial capital, hump principle
  • Method name: initial lowercase, hump principle

5, Basic operator

  • Arithmetic operators: +, -, *, /,%, + +, –
  • Assignment operator:=
  • Relational operators: >, <, > =, < ===
  • Logical operators: & &, |!
  • Bitwise operators: &, |, ^, > >, <, > > >
  • Conditional operator:?:

Self increasing and self decreasing

package com.yu.type;

public class MyType {

    public static void main(String[] args) {
        int a = 3;
        int b = a++;
        int c = ++a;
        System.out.println(b);//b=3, a=4. After assigning the value of a to b, a adds 1
        System.out.println(c);//c=5, a=5. Before the assignment, a adds 1 Then assign it to the right
        System.out.println(a);

        int d=a--;
        int e=--a;
        System.out.println(d);//d=5
        System.out.println(e);//e=3


    }
}


String splicing

package com.yu.type;

public class MyType {

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

    }
}


Put in front represents string splicing. Placing it behind does not affect the operation

Ternary operator

package com.yu.type;

public class MyType {

    public static void main(String[] args) {
        int a = 3;
        int b=4;
        int c=a>b?1:2;
        System.out.println(c);//Expression judgment is correct, before colon execution, error, after colon execution

    }
}

6, Process control

Use of scanner

1. Use next() to get the data

package com.yu.type;

import java.util.Scanner;

public class MyType {

    public static void main(String[] args) {

        //Keyboard receiving data
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter:");
        String str=scanner.next();
        System.out.println("The input is:"+str);
    }
}


Note: use next() to receive data. 1. Automatically removes spaces before valid characters. 2. If there is a space between two characters, the following characters will be automatically removed. 3. Cannot get a string with spaces

2. Receive data using nextLine()

package com.yu.type;

import java.util.Scanner;

public class MyType {

    public static void main(String[] args) {

        //Keyboard receiving data
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter:");
        String str=scanner.nextLine();
        System.out.println("The input is:"+str);
    }
}


The input string is the same as the output string

1. Sequential structure
2. Select structure
3. Cyclic structure

Topics: Java