Java study notes-01

Table of contents

jdk installation and environment configuration

Java pre-knowledge

Write a HelloWorld

constant

type of data

variable

type conversion

arithmetic operator

unary operator (++ --)

relational operator

Logical operators (and or not, logical XOR)

Ternary (ternary) operator

Simple use of the Scanner class


jdk installation and environment configuration

I saw an article that was very detailed. You can refer to this: jdk8 installation and configuration of environment variables

Java pre-knowledge

Java cross-platform principle

  • It is because the corresponding JVM (java virtual machine) is installed on the corresponding system (window, linux, mac), so it can be compiled on each platform

 The difference between JRE and JDK

  • JDK is a Java program development kit that includes the JRE and tools used by developers
  • JRE is the runtime environment for Java programs, including the core libraries required by the JVM and runtime
  • If we want to run an existing Java program, we only need to install JRE
  • If we want to develop a brand new Java program, then we need to install JDK

 computer storage unit

  • Both memory and hard disk can be stored, and the smallest information element of a computer storage device is called  a bit (also called bit / bit / b)
  • The smallest unit of storage in a computer is called a byte (byte / B)

Dos commonly used commands

  • window + R to bring up cmd
  • Drive letter: You can switch to the specified drive letter 

  • cd folder name can switch to the specified drive letter, and the file name can be completed by pressing the Tab key 
  • cd .. can switch to the previous file directory
  • cd .\ can switch to the root directory of the current file
  • dir View all file information under the folder
  • cls clear screen
  • exit exit

Write a HelloWorld

Create a new folder (javaCoding), create a new text document HelloWorld.txt in the folder, and write the following content

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

Then change the txt suffix to java, which is HelloWorld.java

Then click the folder with the mouse and enter cmd

 A cmd window will pop up, enter javac HelloWorld.java in the window and press Enter 

At this point, the compiled bytecode class file will appear in the folder

Then we enter java HelloWorld in the cmd window and the content will be displayed

 Note: If the content output in the file contains Chinese, the compiled command is

javac -encoding UTF-8 filename.java

constant

  • It is the amount that will not change. Once the initial value is given, it cannot be modified later
type illustrate example
string constant Content enclosed in double quotes "aabbcc"
integer constant number without decimal point 1
decimal constant numbers with decimals 1.1
character constant Content enclosed in single quotes, and only one 'a'
boolean constant true or false false
empty constant null (empty value) not able to output

type of data

  • Java is a strongly typed language, and each data must have a clear data type before it can be used. Different data types also allocate different memory spaces
  • Basic data types can be divided into 4 categories, namely integer type (including byte, short, int and long), floating point type (including float and double), Boolean type and character type (char)

Default value and occupied bytes

variable

  • A value whose value can change while the program is running

variable type variable name = variable value;

int num = 100;  // 定义了一个变量名为num的变量,是数字型,初始值为100
// 或者这种方式
int num;
num = 100;

Notice:

  • When defining the long type, you need to add L after it
  • When defining the float type, it needs to be on the back of the machine F
public static void main(String[] args) {
        // 声明浮点数时,再后面要加上f或者F
        float pi = 3.1415926f;
        System.out.println(pi);
        // 声明long类型时,需要在后面加上L或者l
        long count = 123456789L;
        System.out.println(count);
    }

type conversion

  • Divided into automatic type conversion and mandatory type conversion
  • Decimals are of double type by default
  • Number defaults to int type

automatic type conversion

  • Assign a value or variable representing a small range to another variable with a large data range (that is, from small to large )
double num = 10;  // 由数字型转成双精度型,会自动转换

cast (not recommended)

  • Assign a value or variable representing a large range to another variable representing a small range (that is, from large to small )

Target Data Type Variable Name = (Target Data Type) Value or Variable

int num = (int)9.99; // 9 把浮点型转成整数型,从大到小,需要强制转换

arithmetic operator

  • There are 5 types +(addition) -(subtraction) *(multiplication) /(division) %(remainder)
public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(a + b); // 30
        System.out.println(b - a); // 10
        System.out.println(a * b); // 200
        System.out.println(b / a); // 2
        System.out.println(b % a); // 0
        float c = 9.99f;
        System.out.println(a + c); // 19.99
    }
  • char type + number type If the value of char is not a number, it will be converted into the number corresponding to the ASCII code for calculation
public static void main(String[] args) {
        char a = 'a'; // a 对应的ASCII码值为 97
        int b = 20;
        System.out.println(a + b); // 117
    }
  • When an arithmetic operator contains values ​​of multiple basic data types, the type of the entire arithmetic expression will be automatically promoted

promotion rules

When byte / short / char participate in the operation, it will be automatically promoted to int type

When int participates in the operation, it will be automatically promoted to long type

When long participates in the operation, it will be automatically promoted to float type

When float participates in operations, it will be automatically promoted to double type

  • If it is a string + operation, the characters are connected
public static void main(String[] args) {
        String a = "999";
        System.out.println(a + 0); // 9990
    }

unary operator (++ --)

  • Pre-increment or pre-decrement will first increase or decrease itself, and then participate in other adjacent operations
public static void main(String[] args) {
        // 单独使用
        int a = 10;
        int res = (++a);
        System.out.println(res); // 11
        // 参与运算
        int b = 99;
        int res2 = ++b + --b + b;
        // 1. ++b 会先自增,此时b也就是(99 + 1),然后再参与临近的运算  b = 100
        // 2. --b 会先自减,此时的b也就是(100 - 1),然后再参与运算(99 + 1) + (100 - 1)  b = 99
        // 3. 然后再+b (99 + 1) + (100 - 1) + 99  b = 99
        System.out.println(res2); // 298
        System.out.println(b); // 99
    }
  • After self-increment or post-decrement, they will first participate in other adjacent operations, and then increase or decrease themselves
public static void main(String[] args) {
        // 单独使用
        int a = 10;
        int res = (a--);
        System.out.println(res); // 10
        System.out.println(a); // 9
        // 参与运算
        int b = 99;
        int res2 = b++ + b-- + b;
        // 1. b++ 会先参与临近的运算,也就是 99 + ?(这里的问好是因为后面临近的运算也是b的操作,
        // 不是确定的值) 然后再自增,此时b也就是(99 + 1)  b = 100
        // 2. b-- 会先参与临近的运算,也就是 99 + 100 然后再自减,此时的b也就是(100 - 1)  b = 99
        // 3. 然后再+b 99 + 100 + 99  b = 99
        System.out.println(res2); // 298
        System.out.println(b); // 99
    }

relational operator

  • The results of relational operators are boolean, that is, true or false. Relational operators produce a Boolean result
  • == != > >= < <= There are 6 types in total, used for judgment
  • Double equality requires that both type and value must be the same
public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(a == b); // false
        System.out.println(a != b); // true
        System.out.println(a > b); // false
        System.out.println(a < b); // true
        System.out.println('1' == 1); // false
    }

Logical operators (and or not, logical XOR)

  • Judging whether the expression is true, the result of the judgment is also true or false
  • AND & short-circuit AND && or | short-circuit or || not! Logical XOR ^ 
the sign illustrate example
&

Only when the expressions before and after are true, it is true

judgment before and after

It can be judged that the front and back are not of the same type

0 & 1 // 0

false & true // false

true & true // true

&&

Only when the expressions before and after are true, it is true

But if the previous one is false, the latter will not be judged, and false will be returned directly

Before and after the type must be consistent, in order to judge

0 && 1 // will directly report an error
false && true // false
true && true // true
|

The expressions before and after are all false, so it is false

judgment before and after

It can be judged that the front and back are not of the same type

0 | 1 // 1

false | true // true

false | false // false

||

The expressions before and after are all false, so it is false

But if the previous one is true, the latter will not be judged, and true will be returned directly

Before and after the type must be consistent, in order to judge

0 || 1 // will directly report an error

false || true // true

false || false // false

! Negative operation, true will return false, false will return true

!true // false

!false // true

^ XOR operation, a ^ b (a and b results are different if true)

true ^ false // true

true ^ true // false

Ternary (ternary) operator

Format:

relational expression? expression1: expression2;

  • If the expression is true, execute expression 1, otherwise, execute expression 2
int a = true ? 10 : 20;
System.out.println(a);
  • Of course, chain calls can also be made (not recommended, it is messy, it is better to use if or switch)
int a = true ? 10 : true ? 20: true ? 30 : 0;
System.out.println(a);

Simple use of the Scanner class

  • Need to use the Scanner class
  • The first step: carry out the import package
import java.util.Scanner;
  • The second step: new Scanner class, create a specific instantiation object
Scanner input = new Scanner(System.in);
  • Step 3: Receive the data entered by the user and perform input constraints
int num = input.nextInt(); // 规定用户只能输入数字型

Full code:

public static void main(String[] args) {
        // 提示用户输入
        System.out.println("请您输入一个整数数字:");
        // 接收用户输入
        Scanner input = new Scanner(System.in);
        // 规定用户只能输入数字型
        int num = input.nextInt();
        // 显示用户输入的内容
        System.out.println("您输入的数字为:" + num);
    }

Guess you like

Origin blog.csdn.net/qq_52845451/article/details/130365610