Getting to Know Java: Data Types, Variables, and Operators

Hello everyone, in this article, I will share with you about Java data types, variables and operators.

Data Types and Variables

type of data

In Java, data types are divided into basic data types and reference data types. Today we will focus on explaining basic data types.

The basic data types are divided into integer, floating point, character and Boolean types. Let's look at the following table.
insert image description here

integer type

byte type

The size of the byte type is 1 byte, and the range it can represent is -128~127. We can use a piece of code to know this range.

System.out.println(Byte.MIN_VALUE);
System.out.println(Byte.MAX_VALUE);

insert image description here

Here Byte is a wrapper class of byte type. We don't need to study it in detail now, as long as we know that it can be used in this way.

short type

The size of the short type is 2 bytes, which can represent -2^15 ~ 2^15-1, that is, -32768 ~ 32767

System.out.println(Short.MIN_VALUE);
System.out.println(Short.MAX_VALUE);

insert image description here

int type

The size of the int type is 4 bytes, which can represent
numbers in the range of -2^31 ~ 2^31-1, -2147483648 ~ 2147483647.

System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);

insert image description here

long type

The size of the long type is 8 bytes, which can represent
numbers in the range of -2^63 ~ 2^62-1, -9223372036854775808 ~ 9223372036854775807.

System.out.println(Long.MIN_VALUE);
        System.out.println(Long.MAX_VALUE);

insert image description here

You may ask here, everyone has learned C language, so why is there no long long type? And in the C language, why is the size of the int type sometimes 4 bytes and sometimes 8 bytes? How many bytes is the int type in Java?

There is no long long type in Java, because the long type in Java is 8 bytes, and the int type in Java is always 4 bytes, it will not appear that one will be 4 bytes, and the other will be 8 bytes.

floating point

The floating-point type in Java is the same as the floating-point type in C language. The floating-point type is divided into float type and double type. The size of the float type is 4 bytes, and the size of the double type is 8 bytes. The type also has a range of representation, but we generally don't pay attention to it.

character type

The character type char in Java is very different from the char type in C language, because in C language, the size of char type is 1 byte, while in Java the size of char type is 2 bytes, and Our Chinese characters are also two bytes, that is to say: in Java we can use char to store Chinese characters. For example:

char c = '好';

The range represented by the char type is: 0~65535, so why not -32768~32767? Because Unicode encoding is used in Java to represent Chinese characters.
Let's see it in code

System.out.println(Character.MIN_VALUE);
System.out.println(Character.MAX_VALUE);

insert image description here

Boolean type

The size of the Boolean type is indeterminate, and the range he represents is only false and true.

variable

What is a variable?
Variables refer to quantities that can be changed frequently in a Java program, and these variables are defined with data types.

The format of defining a variable is: data type variable name = initial value

When we define a variable, we should pay attention that the value of the variable cannot exceed the range that can be represented by the data type, for example: Byte a = 128; if this does not work, the compiler will report an error.

What we need to pay attention to when defining variables is that when we define long integer and float types, we can modify them a little. Long b = 100L, we add l or L after the number to distinguish between int and long types, float c = 1.0f, when defining float type data, we need to add f or F after the number, otherwise the compiler will put This data is regarded as a double type, and the compiler will report an error.

Let's focus on floating-point variables and Boolean variables.

floating point variable

int a = 3;
int b = 2;
System.out.println(a/b);

What is the output of this code? 1.5? Let's take a look at
insert image description here
this effect in both Java and C languages, that is, if the numbers on both sides of / are integers, the result must also be an integer, and its result is the result of divisibility, then we want to get 1.5 what can we do about it? We need to convert to division between floating point numbers.

double a = 30;
double b = 2.0;
System.out.println(a/b);

insert image description here

double a = 3.0;
int b = 2;
System.out.println(a/b);

insert image description here
It is also possible to write 1.5 in this way, because the int type will be automatically converted to the double type during calculation.

Boolean variable

Boolean variables only have two values ​​false and true, and in Java and C language is different, in C language we can write:

int i = 10;
while(i)
{
    
    
    System.out.println("我好帅");
    i--;
}

But we will report an error if we write this in Java, because there is no so-called 0 is false in Java, and non-zero is true. In Java, true can only be used to represent true, and false to represent false. We can only write like this:

int i = 10;
while(i>0)
{
    
    
    System.out.println("我好帅");
    i--;
}

type conversion

The type is not always the same, he will change, for example:

int a = 10;
Long b = 0L;
b = a;

implicit conversion

Here a will be automatically converted to long type, because b is a variable of type long. A variable of type long is larger than a variable of type int, so a will be automatically converted to type long, and then assigned to a. This conversion is called implicit conversion. , and there is another implicit conversion.

Byte a = 10;
Byte b = 20;
Byte c = 0;
c = a+b;

For this code, the compiler will report an error, because the compiler has such a principle that it will upgrade the data less than 4 bytes to 4 bytes of data during calculation, that is to say, a and b will happen first The integer type is promoted, and then the calculation is performed, and then the value of the int type is assigned to the variable of the byte type, and an error will be reported, so we must pay attention to this implicit conversion.

explicit conversion

Explicit conversion means that we pass data in such a form
(type), for example: (int) 13.14, here we convert 13.14 mandatory type into int type, and data loss may occur during the process of mandatory type conversion, here Data after the decimal point will be lost.

Not all mandatory type conversions can be successful, and type conversions cannot occur between different types, for example:

boolean a = false;
int b = (int)a;

This kind of compiler will report an error, and the type conversion is unsuccessful.

operator

arithmetic operator

Arithmetic operators are actually very simple, here + - * / we will not introduce too much. In Java, the object of % can be a floating point number. For example:

double a = 6.5%3;

The result is 0.5

increment operator

+= -= *= /= %=
Incremental operator is actually equivalent to abbreviation, for example: a+=2; is equivalent to a = a+2; other incremental operators are the same, but the incremental operator we It should be noted that no implicit conversion occurs when we use the increment operator, for example:

Byte a = 10;
Byte b = 20;
a+=b;

This code will not report an error.

Increment/decrement operators

++   -- 
int a = 1;
int b = a++;
int c = 1;
int d = c+1;

After executing this code, the values ​​of b and d, and the values ​​of a and c are all different. Here a++ is the postfix ++, so the value of a is assigned to b first, and then ++, a The value itself is also ++, but d is the value after c+1, and the value of c is still 1. This is a side effect of self-increment or self-decrement, and we need to pay attention when using it.

The post ++, – is different from the pre ++, –, the pre ++, – is to perform +1 or -1 first, and then assign the value, but the post ++, – is to assign the value first and then + 1 or -1.

Relational operators (relatively simple, but not too much explanation)

== > < >= <= !=, == do not write =.

Logical Operators

&& || !

&&

Logical and, when both sides of && are true, the result is true, otherwise it is false.

int x = 1;
if(x == 2 && 1/0 == 0) {
    
    
    System.out.println("ok");
}else {
    
    
    System.out.println(no);
}

insert image description here

When we run this code, there will be no error, because when the expression on the left of && is false, the expression on the right will not be executed

||

The result is true when either of the expressions on either side of || is true. Likewise, when the expression on the left of || is true, the expression on the right will not be executed.

Logical negation means that if the expression is true, the result of logical negation is false, and when the expression is false, the result of logical negation is true.

bitwise operator

& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise Negative
Bitwise operators operate on the two's complement form of numbers.

& bitwise AND operator

It is 1 only if the corresponding binary values ​​are all 1, otherwise it is 0. For example: 15&20, 15 converted to binary is 1111, 20 converted to binary 10100
and then performed bitwise AND operation,
0 1 1 1 1 1
0 1 0 0 0
0 1 0 0
results in 4.

| bitwise OR operator

One of the two corresponding binary bits is 1.
15 | 20
0 1 1 1 1
1 0 1 0 0
1 1 1 1 1
is 31

^ bitwise XOR operator

Two corresponding binary numbers are not the same as 1, and the same is 0
15 ^ 20
0 1 1 1 1 1 0
1 0 0
1 1 0 1 1
The result is 27

~ bitwise inversion

If the binary bit is 0, turn it to 1, and if it is 1, turn it to 0
15
1 1 1 1
0 0 0 0
results in 0.

shift operator

"<<" binary bit left shift
">>" binary bit right shift
">>>" unsigned right
shift The operation object of the shift operator is also the binary complement form of the number

<< left shift operator


Binary shift one bit to the left, complement 0 on
the
right 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 The result is 30, it can also be said that the left shift operator is multiplied by 2^i, i is the
shift several.

>> right shift operator

The binary bit is shifted one bit to the right, the sign bit is complemented on the left, 1 is complemented for negative numbers, and 0 is complemented for integers
15>>1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 The result is 7
.
-15
original code: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 inverse code: 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0's complement: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 0 0 0 1
-15>>1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 Then convert this binary
to The original code, the result is -7
The ">>" right shift operator is equivalent to dividing by 2^i.

>>> unsigned right shift

The left side of unsigned right shift will always be filled with 0, so I won't introduce too much here.

Guess you like

Origin blog.csdn.net/m0_73888323/article/details/130117926