Basic knowledge of JavaSE 3: data types and operators

Posted by mihomes on Thu, 03 Mar 2022 15:36:38 +0100

1, Data type
1. Basic data type
meaning:
The data of the corresponding type is assigned to the variable of the corresponding type
Automatic type lifting:
Small ----- > Large
It can be stored normally, but it may cause a waste of memory
Cast type:
Large ----- > small
Can not occur, which may cause loss of accuracy
Small range type variable name = (small range type) large range type data;
be careful:
(1) If multiple types of data are stored to participate in the operation, the result type is the largest type participating in the operation
(2) If the result type is < int, it will be automatically promoted to int
(3) When assigning values to byte, short and char, constants of type int can be assigned successfully as long as the range allows
boolean does not participate in type conversion
byte --> short --> int --> long --> float --> double

2,Reference data type
	Guide Package : Indicates where to use the type
		import Package name.Class name;
		Defined above the class
	Reference data type or variable name = new
		Reference data type();
	Use function:
		quote.Public name();
	Scanner
		Receive user keyboard input
		Import:
			import java.util.Scanner;
		establish Scanner Reference to type:
			Scanner sc = new Scanner(System.in);
		Use function:
			(1)quote.nextInt()
				Receive user keyboard input int Content of type
			(2)quote.nextByte()
				Receive user keyboard input byte Content of type
			(3)quote.nextShort()
				Receive user keyboard input short Content of type
			(4)quote.nextLong()
				Receive user keyboard input long Content of type
			(5)quote.nextDouble()
				Receive user keyboard input double Content of type
			(6)quote.nextFloat()
				Receive user keyboard input float Content of type
			Received from a valid character, encountered enter Key end function
			(7)quote.next()
				Receive user keyboard input String Content of type
				Receive from valid characters,Do not receive when encountering spaces, etc,encounter enter Key end function
			(8)quote.nextLine()
				Receive a full line of input from the user's keyboard String Content of type
				encounter enter Key end function
		be careful:
			Don't define and jdk Type with the same name in,Same package class
			If in nextLine There are series on it next function,Need to deal with legacy enter problem
	Random
		Generate random number
		Guide Package:
			import java.util.Random
		establish Random Reference to type:
			Random  ran = new Random();
		Use function:
			ran.nextInt()
				Random generation int Integer in range
			ran.nextInt(integer n)
				random[0,n)integer
			ran.nextDouble() [0.0,1.0)
				Random generation double Decimal range
		Formula summary:
			Random integer
				[0,n)
					ran.nextInt(integer n)
				[0,n]
					ran.nextInt(n+1)
				[min,max)
					ran.nextInt(max-min)+min
				[min,max]
					ran.nextInt(max-min+1)+min
		be careful
			Pseudo random number

2, Operator
1. Arithmetic operator
(1)+
Positive sign (omitted)
Addition operation
String connector
Once the "" string appears on the left and right sides of +, + is used as the string connector to splice the data on the left and right sides into a string
(2)-
minus sign
Subtraction operation
(3)*
(4)/
(5)%
Remainder or modulo
When the first operand < the second operand, the resu lt is the first operand
(6)++
Self increasing
Self + 1
(7)–
Self subtraction
Self-1
(8) + + -- Law:
1. Only operate yourself
++The operands are preceded by their own + 1
– the operands are preceded by their own - 1
2. Affect other results
(assignment, expression, output)
+± - put in front of the operand, first + 1 or - 1, and then calculate
+± - put it after the operand, calculate first, and then + 1 or - 1
2. Assignment operator
Basic assignment operator =:
=Is the only right to left symbol in java
Lowest priority
Extended assignment operator (arithmetic operator + basic assignment operator):
+= -= *= /= %=
advantage
Fast execution efficiency
Automatic cast
be careful
During compilation, the compiler will automatically optimize the position that can be realized into an extended assignment operator
Comparison operator or relational operator
> < >= <=
== !=
Logical operator
&And
If both are true, the result is true
One that is false is false
|Or
One that is true is true
If both are false, it is false
! wrong
!true -> false
!false -> true
^XOR
The same is false, but the different is true
short circuit
If the value of the left operand can determine the result of the final expression, the second operand will not be executed
&&
If both are true, the result is true. If one is false, it is false
||
If one is true, it is true, and if both are false, it is false
be careful
The operand must be boolean. Short circuit operator is recommended for expressions with boolean value
Bitwise Operators
Conditional operator
Conditional expression? Value 1: value 2;
Execution process
Calculate the conditional expression and get the boolean result
Is true and the result is a value of 1
Is false and the result is a value of 2
Operand
Data on the left and right sides of the operator
expression
Operands connected by operators are called expressions as a whole
Unary operator or unary operator
There is only one operand
+- (sign) + + –
Binary operator or binocular operator
There are two operands
+ - * /
Ternary operator or ternary operator
There are three operands
Conditional operator

Topics: Java Back-end