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
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();
		Receive user keyboard input
			import java.util.Scanner;
		establish Scanner Reference to type:
			Scanner sc = new Scanner(;
		Use function:
				Receive user keyboard input int Content of type
				Receive user keyboard input byte Content of type
				Receive user keyboard input short Content of type
				Receive user keyboard input long Content of type
				Receive user keyboard input double Content of type
				Receive user keyboard input float Content of type
			Received from a valid character, encountered enter Key end function
				Receive user keyboard input String Content of type
				Receive from valid characters,Do not receive when encountering spaces, etc,encounter enter Key end function
				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
		Generate random number
		Guide Package:
			import java.util.Random
		establish Random Reference to type:
			Random  ran = new Random();
		Use function:
				Random generation int Integer in range
			ran.nextInt(integer n)
			ran.nextDouble() [0.0,1.0)
				Random generation double Decimal range
		Formula summary:
			Random integer
					ran.nextInt(integer n)
		be careful
			Pseudo random number

2, Operator
1. Arithmetic operator
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
minus sign
Subtraction operation
Remainder or modulo
When the first operand < the second operand, the resu lt is the first operand
Self increasing
Self + 1
Self subtraction
(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):
+= -= *= /= %=
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
If both are true, the result is true
One that is false is false
One that is true is true
If both are false, it is false
! wrong
!true -> false
!false -> true
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
Data on the left and right sides of the operator
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