Basic Data Types and Operators of Java
- 2021-11-29 07:19:58
- OfStack
String type String
Splice character '+'
Escape character
Operator Addition, subtraction, multiplication and division
Modular operation
Incremental assignment operator
Self-increasing operators and self-built operators
Assignment operator
Judgment operator
Logical judge
Bitwise operator
Shift operation
Conditional operator
Operator priority
Summarize
Data type
Boolean type
There are also two types of bool in Java: true (true) and false (false)
Unlike C language, in C language, we can put 1 number in bool type, 0 is false and non-0 is true
int fun(){
return 1;
}
bool a = fun();
In Java, bool type has no specific value. If it is written according to the above C language, an error will occur. true is true, false is false, and there is no other value.
System.out.println(true);
System.out.println(false);
The output is as follows:
true
false
In the JVM specification, the bool type size is not specified, but in some books, the bool type is 1 byte size, and in some books, it is 1 bit size.
String type String
In Java, there is no pointer, but there is a separate string type String
String is also called reference type
String str = "abcde";
System.out.println(str);
Splice character '+'
System.out.println("hello"+"world");
Execution results
helloworld
You can splice two strings
But the following code executes differently
System.out.println("hello"+10+20);
System.out.println(10+20+"hello");
System.out.println("hello"+(10+20));
System.out.println(10+""+20+"hello");
The implementation results are as follows:
hello1020//10 and 20 are spliced into strings
30hello//10 and 20 make the sum first and become 30
hello30//Parentheses are added and become strings
1020hello
//Become a string because there is an empty string between 10 and 20 so that 10 and 20 are not added
Escape character
Escape character ''and C language 1
Will match the next word to another character
For example, we are familiar with\ n line break
If you want to print\ n, you can use double escape
System.out,println("\\n") ;
The output is as follows:
\n
Operator
Addition, subtraction, multiplication and division
+,-, *,/, are our most common operators. These operators have the same effect as you think, except that only 1 should pay attention to the '' sign, and the division sign should pay attention to the fact that the divisor cannot be 0. If decimal calculation is involved, it is recommended to use double, because 1/2 = 0 in computer language, because both 1 and 2 are shaping, and the result will only be shaping.
Modular operation
The '%' modular operation is used to find the remainder
int a = 5%2;
In the above code, the value of a is 1, because 5/2 = = 2 plus 1, so a is equal to 1.
Incremental assignment operator
Incremental assignment operators, +=, -=, *=, /=, and so on
Mixed operator 1 in C language
a+=3 < == > a = a+3;
And in Java, incremental assignment comes with cast.
short a = 2;
int s = (int)a+2;// Forced type conversion is required
int b = 0;
b+=a;// No, because the type has been automatically converted
Self-increasing operators and self-built operators
+ + and-, which are equivalent to a+=1 and a-=1;
Self-increasing operators are divided into pre-and post-operators
For example:
int a = 0;
int b = a++;
// Equivalent to int b = a;a = a+1;
int c = --a;
// Equivalent to a = a-1;int b = a;
Assignment operator
'=' is the assignment operator, which assigns the right value to the left
Judgment operator
Judgment operators have = =,! =, < =, > =, < , > .
Is an bool expression, and the result is of type bool
int a = 0;
boolean c = false;
c = a==0;//a==0 To be true, c = true;
c = a!=0;//a==0 As false, c = false;
c = a>0;//a>0 As false, c = false;
c = a<0;//a<0 As false, c = false;
c = a>=0;//a>=0 To be true, c = true;
c = a<=0;//a<=0 To be true, c = true;
Logical judge
Logical or
Expression 1 Expression 2 Expression 3 …
Judging from left to right, if one is true, stop judging and return to true, otherwise return to false.
& & Logic and
Expression 1 & & Expression 2 & & Expression 3...
Judging from left to right, if one is false, stop judging and return false, otherwise return false.
! Logical inversion
! Expression 1;
Returns false if Expression 1 is true, and true if Expression 1 is false.
Note: All expressions must be bool expressions! ! !
Bitwise operator
Bitwise and & Bitwise OR, Bitwise XOR ^, Bitwise reverse ~
Bitwise AND, a
&
b, for two numbers a and b, for each binary bit, if both are 1, the corresponding binary bit is also put 1, otherwise, it is put 0
1101
&
0011 = 0001
Bitwise OR, ab, for two numbers a and b, for every binary bit, if both are 0, the corresponding binary bit is also put 0, otherwise, it is put 0
1101
&
0011 = 1111
Bitwise XOR, a ^ b, for two numbers a and b, for every 1 binary bit, 0 if the same, or 1
1101
&
0011 = 1110
Bit-by-bit inverse, ~ a, for every binary bit, except the sign bit, that is, the first bit, all other bits change from 1 to 0, and 0 to 1.
~1101 = 1010
Shift operation
Left shift < < , move to the right > > And unsigned right shift > > >
Left shift operator, shifts its binary bit to the left by x units, and makes up 0 if it is insufficient
0101 0101 < < 2 (move two places to the left)
The result is 01 010 100
Right shift operator, shifts its binary bit to the right by x units, and the insufficient complementary sign bit
1101 0101 > > 2 (move two places to the right)
The result is 111101 01
Unsigned right shift, shift its binary bit to the right by X units, and make up 0 for the insufficient number, whether it is negative or positive
1101 0101 > > > 2 (unsigned right shift by two digits)
The result is 001101 01
PS: No unsigned left shift
Conditional operator
Conditional operators are 3-entry operators
Expression 1? Expression 2: Expression 3
Expressions 1, 2, 3 must be bool expressions
Returns Expression 2 if Expression 1 is true, and Expression 3 if Expression 1 is false.
Operator priority
Operators have priority, but most of the time you don't have to go into it. You can calculate the internal expression first through parentheses, and use parentheses flexibly to prevent the problems caused by operator priority.