Java basic types and operations

Posted by borris_uk on Sun, 30 Jan 2022 17:27:13 +0100

1. What basic data types does Java provide?

Java has improved eight original data types: byte, short, int, long, float, double, char and boolean

The basic data type is not an object, but a basic data type. These data type variables will be allocated memory space on the stack immediately after they are declared

 

The eight basic types can be divided into the following four categories:

  • Int related: short(2 bytes), int(4 bytes), long(8 bytes)
  • Float related: float(4 bytes), double(8 bytes)
  • boolean correlation: boolean(1 byte), byte(1 byte)
  • Char correlation: char(2 bytes) (unicode characters)

In addition, Java also provides encapsulation classes for these original data types (Character type, Boolean type, numeric type Byte, Short

Integer,Long,Float,Double)

 

Differences between package type and original type:

  • The original type passing parameter is passed by value, and the encapsulated type passing parameter is passed by reference
  • When a class is instantiated, the default value of the encapsulation type is null, and the default value of the original type is related to their type (for example, the default value of int is 0)

 

Accuracy loss:

When assigning a high-precision variable to a low-precision variable, it will cause a loss of precision. For example, assigning a value of type double to a variable of type float

eg: float f = 0.93; This will cause precision loss. The correct method is to use forced type conversion, eg: float f = (float)0.93;

 

 

2. What is an immutable class?

Immutable class:

Immutable class means that when an instance of this class is created, it is not allowed to modify its value. Immutable class is similar to constant, that is, it is only allowed to be read by other programs,

However, other programs are not allowed to modify

 

Common immutable classes:

Encapsulation classes of basic types (Integer, Float, etc.), String

 

String class is immutable, so why is the following code feasible?

1 String s = "Hello";
2 s += " World";
3 System.out.println(s);

Explanation:

In the second line of code, only the reference of s is modified. s points to "Hello World" instead of "hello",

The original "Hello" still exists in memory and has not been changed

 

  

3. What are the differences between value passing and reference passing?

There are two ways of parameter passing in Java language: Value Passing and reference passing

Value transfer:

In the method call, the argument passes the value to the formal parameter. The formal parameter just initializes a temporary storage unit with the value of the argument, so the form participates

Although the value of the argument is the same, it has different storage units. The change of the formal parameter will not affect the change of the argument

Reference passing:

In a method call, what is passed is an object (which can also be regarded as the address of the object), which means that the objects of formal parameters and arguments point to the same storage unit,

Therefore, the modification of the formal parameter will affect the value of the argument

In Java, raw data types are passed by value when passing parameters, while ordinary objects are passed by reference when passing parameters

Examples are as follows:

 1 public class test {
 2     public static void testPassParameter(StringBuffer ss1, int n){
 3         ss1.append(" World");     // quote
 4         n = 8;                    // value
 5     }
 6     
 7     public static void main(String[] args) {
 8         int i = 1;
 9         StringBuffer s1 = new StringBuffer("Hello");
10         testPassParameter(s1, i);
11         System.out.println(s1);        // Hello World
12         System.out.println(i);         // 1
13     }
14 }

 

The eight basic data types in Java are passed by value, and all other types are passed by reference. Due to the packaging types of the eight basic data types in Java

They are immutable types, which makes it more difficult to understand passing by reference. See the following examples for details:

 1 public class test {
 2     public static void changeStringBuffer(StringBuffer ss1, StringBuffer ss2) {
 3         ss1.append(" World");
 4         ss2 = ss1;
 5     }
 6 
 7     public static void main(String[] args) {
 8         Integer a = new Integer(1);
 9         Integer b = a;
10         b = new Integer(b.intValue() + 1);
11         System.out.println(a + " " + b);    // 1 2 line 10 actually creates a new Integer object
12         StringBuffer s1 = new StringBuffer("Hello");
13         StringBuffer s2 = new StringBuffer("Hello");
14         changeStringBuffer(s1, s2);
15         System.out.println(s1 + " " + s2);    // Hello World Hello
16     }
17 }

As can be seen from the above example, pass by reference. In fact, when the content corresponding to the address in the formal parameter is modified, the actual parameter will change accordingly

When the formal parameter address is directly changed, the actual parameter will not change, which shows that passing by reference is actually a kind of passing by value (passing the address)

In other words, reference passing will not change the address pointed to by the actual parameter, but can change the content of the address pointed to by the actual parameter

 

 

4. What are the rules for the conversion of different data types?

Background description:

In Java, when the data types of the two variables involved in the operation are different, implicit data conversion is required. The rule is to convert from low precision to high precision,

That is, the priority meets: byte < short < char < long < float < double. For example, when values of different data types are calculated, the data of short type

It can be automatically converted to int type, int type can be automatically converted to float type, etc. on the contrary, it needs to be converted through strong type

 

Automatic type conversion:

Low level data types can be automatically converted to high-level data types. The conversion rules are as follows:

Precautions for automatic conversion:

  • char type conversion will be automatically converted to its corresponding ASCII code
  • Data of byte, char and short types will be automatically converted to int type when participating in the operation, but type conversion will not occur when using + = operation
  • boolean type cannot be converted to other data types

 

Cast type:

When it is necessary to convert from high-level data type to low-level data type, forced type conversion is required. The forced type conversion is as follows:

  • byte =>char
  • char =>byte
  • short =>byte,char
  • int =>byte,short,char
  • long =>byte,short,char,int
  • float =>byte,short,char,int,long
  • double =>byte,short,char,int,long,float

In addition, it should be noted that casting may lose precision (high precision to low precision)

 

 

5. What are the considerations for casts?

+ = in Java is the operation method specified in Java, which will be specially processed by the java compiler, so the statement short s1 = 1; s1 += 1; Can compile

 

 

6. What is the operator priority?

 

 

7. What are the functions of the round, ceil and floor methods in Math class?

  • round: logarithmic rounding
  • ceil: round up logarithm
  • floor: round down the logarithm

Here is the test code:

 1 public class test {
 2     public static void main(String[] args) {
 3         float m = 6.4f;
 4         float n = -6.4f;
 5         System.out.println(Math.round(m));        // 6
 6         System.out.println(Math.round(n));        // -6
 7         System.out.println("===============");
 8         System.out.println(Math.ceil(m));        //7.0
 9         System.out.println(Math.ceil(n));        // -6.0
10         System.out.println("===============");
11         System.out.println(Math.floor(m));        // 6.0
12         System.out.println(Math.floor(n));        // -7.0
13         System.out.println("===============");
14     }
15 }

 

 

8. What's the difference between + + i and i + +?

During the execution of java, i + + and + + i directly operate i=i+1 on i,

However, the difference is that i + + gets a copy of the previous value of i without addition, while + + i directly gets the calculated value

For example, a = i + +; Is to assign the value of i to a and then the value of i + 1, while a=++i is to assign the value of i + 1 and then the value of i after + 1 to a

Example code:

 1 public class test {
 2     public static void main(String[] args) {
 3         int i = 1;
 4         System.out.println((i++)+(i++));        // 3
 5         System.out.println(i);                    // 3
 6         System.out.println((i++)+(++i));        // 8
 7         System.out.println(i);                    // 5    
 8         System.out.println((i++)+(i++)+(i++));    // 18
 9         System.out.println(i);                    // 8
10     }
11 }

 

 

9. Move right operation of unsigned number

Java provides two kinds of shift right Operators: > > and > > >, > > is called signed shift right operator and > > > is called unsigned shift right operator

The difference between the two:

  • >>When moving to the right, if the number involved in the operation is positive, 0 will be supplemented in the high position, and if it is negative, 1 will be supplemented in the high position
  • >>>The difference is that whether the number involved in the operation is positive or negative, it will be supplemented with 0 in the high order

 

Extension - what are the similarities and differences between < < operator and > > operator?

< < operator ID shifts to the left. Shifting n bits to the left represents the original value multiplied by 2 to the nth power, which is often used instead of multiplication

For example, a number m multiplied by 16 can be expressed as shifting the number to the left by 4 bits (m < < 4)

Because the CPU directly supports bit operation, bit operation is more efficient than multiplication operation

Different from the shift right operation, the shift left operation has no difference between signed and unsigned. When shifting left, the high bit is removed and the low bit is supplemented with 0

 

 

10. Can a Chinese character be stored in a char variable?

In Java, unicode encoding is used by default, that is, each character (char) occupies two bytes, so char can store Chinese characters

String is composed of char, but it is stored in a more flexible way, that is, English occupies one byte, while Chinese generally occupies two bytes

Example code:

 1 public class test {
 2     public static void getLen(String s){
 3         System.out.println(s + "The length of the is: " + s.length() + " The number of bytes occupied is: " + s.getBytes().length);
 4     }
 5     
 6     public static void main(String[] args) {
 7         String s1 = "hello";
 8         String s2 = "Hello";
 9         getLen(s1);
10         getLen(s2);
11     }
12 }

Note: in Java, the number of bytes occupied by Chinese characters depends on the character encoding method. Generally, when ISO8859-1 encoding method is adopted,

A Chinese character, like an English character, only occupies one byte; When GB2312 or GBK encoding mode is adopted, one Chinese character accounts for 2 bytes;

When UTF-8 encoding is adopted, a Chinese character will occupy 3 bytes