java data types and operators

1. Data type:
Java language is a strongly typed language. For each type of data, a clear specific data type is defined, and memory space of different sizes is allocated in memory
1.1 Classification of data types:
basic data types
Reference data types: later The following basic data types will be introduced
: four types and eight types.
Integer type
byte occupies one byte -128----127
short occupies two words -2^15----2^15-1
int occupies four bytes -2^31----2^31-1
long occupies eight bytes -2^63----2^63-1
float
occupies four bytes -3.403E38----3.403E38
double occupies eight bytes -1.798E308----1.798E308
character type
char occupies two bytes 0----65535
boolean type
boolean
Note: pay attention to float type when assigning, when long type
integer is assigned to long, the back of the data When adding L
decimal to float value, add f after the data
as follows:
long x = 8888888888L;
float f = 12.3F;
the default data type of integer is int
The default data type of decimal is double

1.2 Scope
of a variable: It is the effective scope of use of the variable. In
this way, variables are divided into:
local variables
Global variables: variables defined outside the method in the class
Local variables: variables defined in or on the method

1.3 Data type conversion:
Divided into:
Implicit conversion
Forced conversion: Forced to convert
a data type with a large range to data with a small range Implicit conversion: The data type with a small value range is operated with a data type with a large value range. First promote the small data type to a large one, and then operate, the result of the operation is the latter

Implicit conversion: The
code is as follows to see if there is any problem:
int x = 3;
byte b = 4;
x = x + b;
System.out.println(x); The
code is explained as follows:
define a variable as int type, assign 3 to
define A variable is of type byte, assign 4
to calculate the result of adding the two, and then assign the value to int type x
. Because of the implicit conversion, variable b will be promoted to int type first, so the result of the operation is 7 (int type), which is assigned to Int type x, no problem,
and then look at the following code if there is any problem:
int x = 3;
byte b = 4;
b = x + b;
System.out.println(b);
The result of the operation is 7 (int type), Assignment to x of type byte cannot be assigned. There is a problem. Only data types with a small range can be assigned to data types with a large range.
The code compilation upstairs will report an error

Force conversion:
Format: (data type) data
int x = 3;
byte b = 4;
b = (byte) x + b;
System.out.println(b);
Note:
If the data exceeds the assigned data type The value range, the result obtained will be wrong

The following interview questions:
byte b1 = 3;
byte b2 = 4;
byte b3 = b1 + b2;
byte b4 = 3 + 4;
is there any problem in defining b3 and b4?

During the operation of variables, as long as the operation is between data types (including int) smaller than the int type, the operation will be promoted to int (because the default data type of integer is int), so the first calculation result Int type cannot be assigned to byte, it is
wrong

The second is the operation of constants. The java compiler has a constant optimization mechanism, that is, it calculates the result of 3+4 at the time of compilation. Obviously, 7 is within the range of the byte type, so it is no problem.

1.4 Data range comparison:
When performing mixed operations, byte, short, and char will not be converted to each other, and they will be automatically promoted to int type. For other types of mixed operations, small data types are promoted to large
range comparisons:
byte, short ,char < int < long < float < double

Take a look at the following code to write the result:
float f = 12.3f;
long x = 12345;
f = x;//Implicit conversion from long to float
System.out.println(f);//Output result: 12345.0

float f = 12.3f;
long x = 12345;
x = (long)f;//Force conversion
System.out.println(x);//Output result: 12

1.5 The char type participates in the operation:
The char type can participate in the +- operation. You can find the value corresponding to the char type in the ASCII code table, and then perform the operation
to see the following code:
System.out.println("hello"+'a' +1);//Result: helloa1, which belongs to the operation of strings and other data types, the result is the number of characters
System.out.println('a'+1+"hello");//Result: 98hello, a corresponds to 97, result in order

2. Operators and common interview questions
2.1 Look at the results of code writing:
System.out.println(10 / 3);
//The result of integer division can only be an integer
System.out.println(10 / 3.0);
//If you want to get Decimal, turn one of the numbers into a decimal, and the other number will be automatically type promoted
System.out.println(-5 % 2);
// The sign of the result of the % operator is only related to the left side, not the right side
System.out.println(13 % -5);
result:
3
3.3333333333333335
-1
3

2.2 The usage of the arithmetic operators ++ and --:
used alone:
​​int a = 3;
a++;//a = a + 1;
System.out.println(a);
Putting it in front of the operand is as

involved as the back effect Operation:
Look at the following code:
int a = 3;
int b;
b = a++;//When ++ is behind the variable, it will first take out the value in the variable for assignment, and then add 1
System.out by itself. println("a = " + a);
System.out.println("b = " + b);
Operation result:
a = 4
b = 3

Look at the following code:
int a = 3;
int b;
b = ++a;//When ++ is in front of the variable, it will add 1 to itself, and then assign the result to
System.out.println("a = " + a);
System.out.println("b = " + b);
Operation result:
a = 4
b = 4

2.3 Interview questions:
byte b = 10;
b++;
b = b + 1;
ask which sentence will give an error and why?
b++; is actually equivalent to b = (byte)(b + 1), no problem
is actually similar to b+= 1; This shorthand operator will perform forced type conversion internally, so it is no problem
b = b + 1; will report an error, which has been analyzed above

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325365838&siteId=291194637