Overview of the Java language

Java language running mechanism and running process

  1. Features of the Java language

JVMS vary from operating system to operating system.

TFboys held concerts in Beijing, Shanghai and Gansu. JVM is equivalent to the stage in different places, and TFboys’ singing and dancing is equivalent to JAVA code, which can be performed on different stages.

  1. Java Core Mechanism

Java Environment Setup

Companies generally do not exceed 8.

JDK, JVM, and JRE

The official version

  1. Download and install

  1. Configuring environment Variables

Initial development experience

class HelloChina {
   public static void main(String[] args) {
   	System.out.println("hello world"); }}Copy the code

annotation

The API documentation

Java8 API documentation

www.matools.com/api/java8

First programming summary

Good programming style

Java common integrated development environment

Java basic syntax

Keywords and reserved words

identifier

variable

  1. Basic data types

  1. Conversion between basic data type variables

  1. Basic data type and String conversion

  1. Conversion from base to base

Binary to decimal:

For example: 11000101 1*2^0+0+1*2^2+0+0+0+1*2^6=1+4+64=69

Find the decimal value of 10111011:

—-> The bottom layer of the computer is stored in the way of complement, so 10111011 is the complement. On the far left is the negative digit, which means the digit is a negative number.

—-> complement -1, inverse 10111010

—-> In addition to the sign bit, inverse, source code 11000101

—-> Switch to base 10, 1*2^0+0+1*2^2+0+0+0+1*2^6=1+4+64=69

—-> minus sign, -69

Why is the result -128 when 128 is strong converted to byte?

int 128 —-> 0000 0000 0000 0000 0000 0000 1000 0000

Force conversion to byte —-> Remove 24 bits of the signature, resulting in 1000 0000, or -128

Decimal to binary:

Take the inverse mod by 2, e.g. 13.

Binary to octal/hexadecimal:

16 / octal to binary:

Transfer function

The operator

1.False x=2,ture y=2

2. False = 2 x, y = 1

3. True x = 2, false y = 2 – > x = 7

4. If x=2, y=1 — >x= 2

Program flow control

Branching structure
  • if-else

  • swith-case

Output season according to the specified month :(case conditions can be output in this way)

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int month = scan.nextInt();
        switch (month){
        case 3:
        case 4:
        case 5:
            System.out.println("Spring");
            break;
        case 6:
        case 7:
        case 8:
            System.out.println("Summer");
            break;
        case 9:
        case 10:
        case 11:
            System.out.println("Autumn");
            break;
        case 12:
        case 1:
        case 2:
            System.out.println("Winter");
            break; }}}Copy the code

Loop structure

  • The for loop

public static void main(String[] args) {

        for(int i=1; i <=150; i++){ System.out.print(i+"");
            if(i%3= =0){
                System.out.print("foo ");
            }
            if(i%5= =0){
                System.out.print("biz ");
            }
            if(i%7= =0){
                System.out.print("baz "); } System.out.println(); }}Copy the code

public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter two positive integers");
        int positiveIntOne = scan.nextInt();
        int positiveIntTwo = scan.nextInt();
        int min = (positiveIntOne<=positiveIntTwo)? positiveIntTwo:positiveIntOne;
// Define the greatest common divisor
        int greatestCommonDivision = 0;
        for(int i=1; i<=min; i++){if(positiveIntOne%i==0&&positiveIntTwo%i==0){ greatestCommonDivision = i; }}// Define the least common multiple
        int leastCommonMultiple = 0;
        for(int i=1; i<=positiveIntOne*positiveIntTwo; i++){if(i%positiveIntOne==0&&i%positiveIntTwo==0){
                leastCommonMultiple = i;
                break;
            }
        }
        System.out.println("Greatest common divisor:"+greatestCommonDivision);
        System.out.println("Least common divisor:"+leastCommonMultiple);
    }
Copy the code
  • The while loop

  • The do while loop

public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter an integer. Zero means the input is complete.");
        int positive_count=0;
        int negative_count=0;
        while (true) {int num = scan.nextInt();
            if (num>0){
                System.out.println("This is a positive number.");
                positive_count++;
            }
            if (num<0){
                System.out.println("This is a negative number.");
                negative_count++;
            }
            if (num==0){
                System.out.println("End of program");
                break;
            }

        }
        System.out.println("The positive numbers are:"+positive_count+"Zero, negative numbers are:"+negative_count+"个");

Copy the code

for(;;) It means always loop. Equivalent to while (true).

  • Nested loop

99 times table

public static void main(String[] args) {


        for(int i=1; i<=9; i++){// Control the number of rows
            for(int j=1; j<=i; j++){// Control column number
                System.out.print(i+"*"+j+"="+i*j+"\t");

            }
            System.out.println();


        }
Copy the code

All primes up to 100

public static void main(String[] args) {

        Boolean isFlag =true;// if I is divided by j, change its value

        for(int i=2; i<=100; i++){// iterate over the number up to 100
            for (int j=2; j<=i-1; j++){//i%j
                if (i%j==0){
                    isFlag = false; }}if(isFlag==true){
                    System.out.print(i+"");
                }
            // Reset isFlag at the end of the inner loop
            isFlag=true; }}Copy the code

Prime code optimization

  • break continue

Marked break continue

public static void main(String[] args) {

        label:for (int i=1; i<=4; i++){for (int j=1; j<=10; j++){if (j%4= =0) {// break; // The current loop, the inner loop, is broken out by default
// continue; // By default, the current loop of the current loop, i.e., the inner loop, is skipped
// break label; // Break out of the loop that specifies the label
                    continue label; // Break the loop of the layer that defines the label} System.out.print(j); } System.out.println(); }}Copy the code

Prime code optimization – two

Measure the merits of a code

  1. Function correctly

  2. readability

  3. Robustness (handling of abnormal input)

  4. High efficiency vs. low storage (time complexity, space complexity (measure algorithm))