Zero basics javaDay03

Posted by mdannatt on Tue, 04 Jan 2022 07:22:13 +0100

Day03

1, Arithmetic operator

Basic use of arithmetic operators

  1. +,-,*,/,%,++,–
int num1 = 10;
int num2 = 5;
int result = num1 + num2;
System.out.println(result);//15
System.out.println(num1 - num2);//5
System.out.println(10 * 5);//50
System.out.println(10 / 5);//2
System.out.println(10 % 3);//1

be careful:

  1. byte type will be transformed upward into int
  2. When the short type is operated, it will be transformed upward into int
  3. Floating point types cannot perform operations directly. BigDecimal will be used for operations

Underlying principles of i + + and i -:

int i =  0;   
i = ++i; 
//Underlying principle:
//i = (int)(i+1);
//i = i;
System.out.println(i);//1

int i = 0;   
i = i++;  
//Underlying principle:
//int temp = i;--temp is used to record I the initial value
//i = (int)(i+1);
//i = temp
System.out.println(i);//0 

Extension:

byte b = 10;
++b;//Bottom layer: b = (byte)(b+1);
short s = 10;
++s;//Bottom layer: s = (short)(s+1);
int i = 10;
++i;//Bottom layer: i = (int)(i+1);
long l = 10;
l++;//Bottom layer: l = (long)(l+1);

2, Assignment operator

=,+=,-=, *=,/= , %=

Assignment rules:
s + = 1 s = (T)((s) + (1) )
The compound assignment E1 op= E2 is equivalent to the simple assignment E1 = (T)((E1)op(E2)),
Where T is the type of E1.

3, Relational operator

==,!=,>,>=,<=

System.out.println(10 == 10);//true
System.out.println(10 != 10);//false
System.out.println(10 > 10);//false
System.out.println(10 >= 10);//true
System.out.println(10 < 10);//false
System.out.println(10 <= 10);//true

Summary:

  1. =Is the assignment number, = = is to judge whether the two values are equal
  2. The results of relational operators are boolean values

4, Logical operator

&,&&,|,||,^,!

&And & & short circuit
|Or short circuit or
^XOR
! wrong

1. Interpretation and comparison of logical operators

  1. &And: both are boolean values. If it is true at the same time, the result will be true
  2. &&Short circuit and: both are boolean values, and the result is true only when it is true
  3. &: after the former is judged as false, the latter will also be judged
    &&: if the former is judged as false, the latter will not be judged, so the efficiency is higher
  4. |Or: both are boolean values. If one is true, the result will be true
  5. ||Short circuit or: both are boolean values. If one is true, the result will be true
  6. |: judge whether the former is true or the latter
    ||: if the former is true, the latter will not be judged, so the efficiency is higher
  7. ^XOR: both are boolean values, false for the same and true for the different
  8. ! Non negative inversion

2. Cases of logical operators

stay dos Enter a number in the window to determine whether it is at 50~100 In the interval of


//1. Enter an int number
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a int Value:");
int num = scan.nextInt();

//2. Judge whether it is within the range of 50 ~ 100
boolean bool = num>50 && num<100;

//3. Output results
System.out.println("Is the value at 50~100 Within the range of:" + bool);

5, Use of Scanner class

Meaning: Java provides us with a class whose function is to input data on the console

//Create an object of the Scanner class
//Human Li Dong = new human ();
Scanner scan = new Scanner(System.in);

//Call function
int i1 = scan.nextInt();//Enter an int data in the console
int i2 = scan.nextInt();//Enter an int data in the console
double d = scan.nextDouble();//Enter a double data in the console
String str = scan.next();//Enter a String data in the console

6, String splicer+

+Both sides are numeric values. This symbol is an arithmetic operator
+There are strings on one or both sides. This symbol is a string splicer

case

System.out.println(1+2+"abc" + "def" +1+2);//3abcdeef12
		//			3 +"abc" + "def" +1+2
		//			"3abc"	 + "def" +1+2
		//			"3abcdef"		 +1+2
		//			"3abcdef1"		   +2
		//			"3abcdef12"

7, Expression

5 + 6: arithmetic expression
5> 6: relational expression
True & false: logical expression

8, Ternary operator / ternary operator

1. Syntax:

Data type variable = (expression)? Value 1: value 2;

2. Understand:

The result of an expression must be of type boolean
true - assigns a value of 1 to the variable
false - assigns the value 2 to the variable

3. Examples:

int num = (false)?10:20;
System.out.println(num);

4. Case:

Enter two numbers of type int in the console and output the maximum value

//Create scan object of Scanner class
Scanner scan = new Scanner(System.in);

//Enter two numbers
System.out.println("Please enter the first number:");
int a = scan.nextInt();
System.out.println("Please enter the second number:");
int b = scan.nextInt();

//Judge size
int max = (a>b)?a:b;//Judge whether a is greater than b. if it is greater than b, return a; otherwise, return b

//Maximum output
System.out.println("The maximum value is:" + max);

Enter three numbers of type int in the console and output the maximum value

Scanner scan = new Scanner(System.in);

System.out.println("Please enter the first number:");
int a = scan.nextInt();
System.out.println("Please enter the second number:");
int b = scan.nextInt();
System.out.println("Please enter the third number:");
int c = scan.nextInt();

int max = (a>b)?a:b;
max = (max>c)?max:c;

System.out.println("The maximum value is:" + max);

Input three int type numbers on the console and output them from small to large

Scanner scan = new Scanner(System.in);
	
System.out.println("Please enter the first number:");
int a = scan.nextInt();
System.out.println("Please enter the second number:");
int b = scan.nextInt();
System.out.println("Please enter the third number:");
int c = scan.nextInt();

//Get maximum
int max = (a>b)?a:b;
max = (max>c)?max:c;
//Get minimum value
int min = (a<b)?a:b;
min = (min<c)?min:c;
//Get intermediate value
int mid = a+b+c-max-min;

System.out.println(min + "<" + mid + "<" + max);

5. Go deep into the three item operator

Extended interview question 1

int a = 5;
System.out.println((a<5)?10.9:9);//9.0

Extended interview question 2

char x = 'x';//'x' - ASCII - 120
int i = 10;
System.out.println(false?i:x);//120

Extended interview question 3

char x = 'x';//'x' - ASCII - 120
System.out.println(false?100:x);//x
System.out.println(false?65536:x);//120

6. Return value rule of ternary operator:

  1. When values 1 and 2 are constants, data will be returned according to types with a wide range of values
  2. When values 1 and 2 are variables, data will be returned according to types with a wide range of values
  3. If value 1 is a constant and value 2 is a variable, is value 1 within the value range of the type to which value 2 belongs
    In, the data is returned according to the value 2 type
    If not, the data is returned according to the value 1 type

9, Bitwise operator

&And | or ^ XOR < < shift left > > shift right > > unsigned bit shift right

1. Meaning:

Convert decimal data into binary before operation

2. Interpretation:

  1. &: in the same bit comparison, if the two are 1, the result is 1
  2. |: if there is 1 between them, the result is 1
  3. ^: in the same bit comparison, the same is 0 and the difference is 1
  4. &, |, ^: both before and after are numeric values, and the symbol is a bit operator
    &, |, ^: both are boolean values, and the symbol is a logical operator
  5. < <: move n bits to the left as a whole, and use n zeros to fill the bits
  6. '> >': move n bits to the right as a whole, and use n highest bits to fill the bits
  7. '> >': move n bits to the right as a whole, and use n zeros to fill the bits

Note: > > and > > > if they are operands, the effect is the same

3. Classic interview questions

Classic interview question 1: calculate 2 * 8 in the most efficient way

System.out.println(2<<3);

Classic interview question 2: describe the output of the following code

//-1 int:1111,1111,1111,1111,1111,1111,1111,1111
//(byte):1111,1111
//(char): 1111111111111111111111 -- char type is transformed upward, and 0 is used to fill the bit
//(int) :0000,0000,0000,0000,1111,1111,1111,1111
System.out.println((int)(char)(byte)-1);//65535

10, Escape character

meaning:

Character itself with special meaning

\n: Indicates a line break
": indicates a double quotation mark
': indicates a single quotation mark
\: indicates a slash
\t: Indicates horizontal tabulation

11, Constant

1. Meaning:

An immutable quantity during program execution

2. Classification:

  1. Numeric literal (ps: 69, 100, 200)
  2. Literal constant (ps: "abcd", "Hello teacher")
  3. Final modified variable

Literal constants and final (final) decorated variables: stored in the constant pool and not destroyed until the end of the project

Topics: Java