1. Basic data types

Java is a strongly typed language. This means that you must declare a type for each variable.

There are eight primitive types in Java. There are four integral types, two floating point types, a char character type for Unicode encoded character units, and a Boolean type for true values.

The data type The default value
byte 0
short 0
int 0
long 0L
float 0.0 f
double 0.0 d
char ‘u0000’
String (or any object) null
boolean false

1.1 the integer

Integers are used to represent values that have no fractional part and are allowed to be negative. Java provides four types of integers:

type Storage requirements The default value Value range
int 4 bytes 0 – 2147483648 ~ 2147483648(Just over 2 billion)
short 2 – 0 -32768 (-2^15) ~ 32768 (-2^15)
long 8 bytes 0 – 9223372036854775808-2 ^ (63) to 9223372036854775808 (63) – 2 ^
byte 1 byte 0 -128 (-2^7) ~ 127 (2^7-1)

1.2 floating-point

Floating point types are used to represent values that have a fractional part. There are two floating point types in Java, float and double, where float has accuracy of 8 significant digits and double has accuracy of 17 significant digits.

type Storage requirements The default value Value range
float 4 bytes 0.0 f 10 to the minus 38 to 10 to the 38
double 2 – 0.0 d 10 to the minus 308 to 10 to the 308
  • Float is accurate to 8 significant digits and double is accurate to 17 significant digits.
  • Floating point values are not suitable for financial calculations that cannot accept rounding errors. The main reason is that floating-point values are represented in binary systems, where 1/10 of a fraction cannot be accurately represented.
float f1 = 234.5 f // The f is only used to distinguish double and does not represent any numeric meaning
double d1 = 123.4
Copy the code

1.3 the type char

The char type is a single 16-bit Unicode character;

1.4 a Boolean type

Boolean types have two values: false and true, which determine logical conditions.

1.5 Conversion between numeric types

The following figure shows the legal conversions between numeric types. There are six solid arrows in the figure, which represent transformations without information loss; There are three dummy arrows that represent conversions with possible loss of accuracy.

In Java, it is possible to convert a double to an int, but at the risk of losing some information. In this case **(narrow casting)**, a cast is required to do this operation.

2, variables,

In the Java language, all variables must be declared before they can be used. Each variable has a type. When declaring a variable, its class type precedes its name.

The basic format for declaring variables is as follows:

int a, b, c;         // Declare three int integers: a, b, and c
int d = 3, e = 4, f = 5; // Declare three integers and assign initial values
byte z = 22;         // Declare and initialize z
String s = "runoob";  // Declare and initialize the string s
double pi = 3.14159; // declare the double precision floating point variable PI
char x = 'x';        // Declare that the value of variable x is the character 'x'.
Copy the code

The Java language supports the following types of variables:

  • Class variable: a variable that is independent of a method and is modified static.
  • Instance variable: A variable independent of a method, but without the static modifier.
  • Local variables: variables in a method of a class.
public class Variable{
    static int allClicks=0;    / / class variables
    String str="hello world";  // Instance variables
    public void method(a){
        int i =0;  // Local variables}}Copy the code

In Java, the keyword final is used to indicate constants. The keyword final indicates that the variable can only be assigned once. Once assigned, it cannot be changed.

3. Operators

3.1 Arithmetic operators

Arithmetic operators are used in mathematical expressions in the same way they are used in mathematics.

The operator describe example
+ Addition – The values on both sides of the addition operator A plus B is equal to 30
Subtraction – the left operand minus the right operand A minus B is equal to minus 10
* The values on both sides of the multiplication-multiplication operator A times B is equal to 200
/ Division – Left operand divided by right operand B/A is equal to 2
% Mod – The remainder of the left operand divided by the right operand B % is equal to 0
++ Increment: Increases the value of the operand by 1 B++ or ++B equals 21 (see below for the difference)
Decrement: Decreases the value of the operand by 1 B– or –B is equal to 19.

3.2 Relational operators

Relational operators generate Boolean results that evaluate relationships between the values of the operands.

The operator describe example
= = Checks if the values of the two operands are equal, and if they are equal the condition is true. False (A == B).
! = Checks if the values of the two operands are equal, and the condition is true if the values are not. (A ! = B) true.
> Checks whether the value of the left-hand operand is greater than the value of the right-hand operand, if so, the condition is true. (A> B)
< Check whether the value of the left-hand operand is less than the value of the right-hand operand, if so, the condition is true. A <B is true.
> = Checks whether the value of the left-hand operand is greater than or equal to the value of the right-hand operand, if so, the condition is true. (A> = B)
< = Checks whether the value of the left-hand operand is less than or equal to the value of the right-hand operand, if so, the condition is true. (A <= B) is true.

= =,! = and equal

The relational operators == and! Equals () equals () equals () equals () equals () equals () equals () equals () equals () equals () equals () equals ()) equals () equals () equals ()) equals () equals () equals ()) equals ()

3.3 Logical operators

The operator describe example
&& Called logic and operators. The condition is true if and only if both operands are true. (A && B) is false.
| | Called logic or operators. If either of the operands is true, the condition is true. (A | | B) is true.
! This is called a logical non-operator. Used to reverse the logical state of operands. If the condition is true, the logical non-operator will get false. ! (A && B) is true.

3.4 bit operators

Java defines bitwise operators for integers (int), long (int), short (char), and byte (byte).

The operator describe example
& If both corresponding bits are 1, the result is 1; otherwise, it is 0 A&B, you get 12, which is 0000, 1100
| If both corresponding bits are 0, the result is 0; otherwise, it is 1 (A | B) received 61, or 0011 1101
^ The result is 0 if the corresponding bit values are the same, 1 otherwise A to the B is 49, which is 0011, 0001
~ The bitwise inverse operator flips each bit of the operand, that is, 0 becomes 1, and 1 becomes 0. (A) is minus 61, which is 1100, 0011
<< Bitwise left shift operator. The left operand moves the right operand by bits to the left. A << 2 is 240, which is 1111 0000
>> Bitwise right shift operator. The left operand moves right by bit to the number of digits specified by the right operand. A >> 2 gets 15 which is 1111
>>> Bitwise right shift zeroing operator. The value of the left-hand operand moves to the right by the number of digits specified by the right-hand operand, and the resulting space is filled with zeros. A>>>2 gets 15 which is 0000 1111

3.5 Assignment operators

Here are the assignment operators supported by the Java language:

The operator describe example
= Simple assignment operator that assigns the value of the right-hand operand to the left-hand operand C = A + B will assign the value of A + B to C
+ = The addition and assignment operator assigns the left-hand operand to the left-hand operand by adding the left-hand and right-hand operand C plus A is the same thing as C is equal to C plus A
– = The subtraction and assignment operator, which assigns the subtraction from the left-hand operand to the left-hand operand C minus A is the same thing as C minus A
* = The multiplication and assignment operator assigns the left-hand operand to the left-hand operand by multiplying the left-hand and right-hand operand C times A is the same thing as C times A
/ = The division and assignment operator assigns the left-hand operand to the left-hand operand by dividing the right-hand operand C / = A, C and A are of the same type is equivalent to C = C/A
(%) = The modulo and assignment operator, which modulo the left-hand and right-hand operands to the left-hand operand C % = A is the same thing as C = C % A
< < = The left shift assignment operator C << = 2 is equivalent to C = C << 2
> > = The right shift assignment operator C >> = 2 is equivalent to C = C >> 2
& = Bitwise and assignment operators C squared is the same thing as C squared
^ = Bitwise xor assignment operator C ^ 2 is the same thing as C ^ 2
| = Bitwise or assignment operators C | = 2 is equivalent to the C = C | 2

3.6 Other Operators

Type conversion operator

The syntax for casting is to give the target type you want to cast in parentheses, followed by the name of the variable you want to cast.

double x = 9.998
int n = (int)x
Copy the code

Any primitive data type can be converted to any other primitive data type except Boolean.

The conditional operator (? 🙂

The instanceof operator

Conditional operators are also known as ternary operators. This operator has three operands and needs to determine the value of a Boolean expression.
b = (a == 1)?20 : 30;
// the instanceof operator is used to manipulate an instanceof an object and check whether the object is of a specific type (class type or interface type).
String name = "James";
boolean result = name instanceof String; // Since name is a String, true is returned
Copy the code

3.7 Precedence of operators

The operator with the highest priority in the following table is at the top of the table and the operator with the lowest priority is at the bottom of the table.

category The operator Correlation between
The suffix () []. (dot operator) Left to right
One yuan expr++ expr– From left to right
One yuan ++expr –expr + – ~! From right to left
multiplicative * / % Left to right
additive + – Left to right
shift >> >>> << Left to right
Relationship between > > < < = = Left to right
equal = =! = Left to right
Bitwise and & Left to right
The bitwise exclusive or ^ Left to right
Bitwise or | Left to right
Logic and && Left to right
Logic or | | Left to right
conditions ? : From right to left
The assignment = + = = * = / = % = > > < < = = & = ^ = | = From right to left
The comma . Left to right

Reference:

Rookie tutorial www.runoob.com/java

Java Core Technologies – Volume 1