JAVA Chapter II summary

Posted by cosmos33 on Sun, 19 Sep 2021 12:11:40 +0200

1, Which two data types does java contain? What is the value range and default value of each type of basic type?

Data types can be divided into basic types and composite types

Basic type:

Integer type: byte(8 bits), short(16), int (32), long (64)

Floating point type: float, double

Character type: char(16 bit unsigned integer, using Unicode character set)

boolean type: boolean (the value is true or false, and the bool ean type of C + + is boolean)

Composite data type:

class, interface, array

data type

Default value

Range of numbers

byte

0

-2^7~2^7-1

short

0

-2^15~2^15-1

int

0

-2^31~2^31-1

long

0L

-2^63~2^63-1

float0.0f-
double0.0d-
char \ u0000 (empty, ")-
booleanfalse-

2, Under what circumstances does Java have integer overflow? Please give examples and solutions.

Integer overflow is easy to occur when two numbers perform mathematical operations

For example, we know

public class IntegerExample {
    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);//2147483647
        System.out.println(Integer.MIN_VALUE);//-2147483648
    }
}

The following code has a problem

public static void main(String[] args) {
      int m = Integer.MAX_VALUE/2+1; //1073741824
      int n = Integer.MAX_VALUE/2+1;
      int overflow = m + n;
      System.out.println(overflow); //-2147483648
}

The following corresponding modifications can be made

public static void main(String[] args) {
      int m = Integer.MAX_VALUE/2+1; //1073741824
      int n = Integer.MAX_VALUE/2+1;
      long normal = (long) m + n;
      System.out.println(normal); //2147483648
}

3, What are the wrapper classes of Java basic types? What are the high-frequency data cache ranges? Please select a wrapper type to program and verify its data cache characteristics.

First of all, we make it clear that everything in java is an object, but the basic type is not an object, but a class

General data

type

Corresponding package

Loading class

char

Character

byte

Byte

short

Short

int

Integer

long

Long

float 

Float

double

Double

JDK document: Java Platform SE 8

Double and Float have no cache, and other types have high-frequency cache intervals.

The cache range of its high-frequency cache interval is:
Boolean: static value will be returned when static final is used
Byte: -128~127
Short: -128~127
Character: 0~127
Long: -128~127
Integer: -128~127
 

For example:

Integer i1 = 127;
Integer i2 = 127;
i1 == i2 ->true


Integer i1 = 128;
Integer i2 = 128;
i1 == i2 ->false

4, What is automatic packing and what is automatic unpacking? Please give an example.

To put it simply, boxing is to automatically convert the basic data type to the wrapper type; unpacking is to automatically convert the wrapper type to the basic data type.

Integer varInteger=100;

Equivalent to -- > Integer varInteger=Integer.valueOf(100); / / automatic packing

int varint=varInteger;

Equivalent to -- > int varint=varInteger.intValue(); / / automatic unpacking

5, What is the difference between int and Integer, and what is the mutual transformation between them?   Please learn the Integer class independently through the JDK document to test the main methods.

Java introduces the corresponding wrapper type for each basic data type. The wrapper class of int is Integer, which is introduced from Java 5

Automatic packing / unpacking mechanism is introduced, so that they can be converted to each other.

difference:
>>Integer is the wrapper class of int; int is the basic data type;
>>Integer variables must be instantiated before they can be used; int variables do not need;
>>Integer is actually a reference to the object, pointing to the integer object of this new; int is the directly stored data value;
>>The default value of integer is null; the default value of int is 0

contrast:

(1) Because the Integer variable is actually a reference to an Integer object, the two Integer variables generated by new are always unequal (because new generates two objects with different memory addresses).

Integer i = new Integer(100);
Integer j = new Integer(100);
System.out.print(i == j); //false


(2) When comparing Integer variables with int variables, as long as the values of the two variables are equal, the result is true (because when comparing wrapper class Integer with basic data type int, java will automatically unpack it as int and then compare it, which will actually become the comparison of two int variables)

Integer i = new Integer(100);
int j = 100;
System.out.print(i == j); //true


(3) When comparing the non new generated Integer variable with the variable generated by new Integer(), the result is false. Because the non new generated Integer variable points to the Integer object stored in the cache array in the static constant pool, and the variable generated by new Integer() points to the new object in the heap. Their object references (addresses) in memory are different.

Integer i = new Integer(100);
Integer j = 100;
System.out.print(i == j); //false


(4) For two Integer objects not generated by new, if the values of the two variables are between - 128 and 127, the comparison result is true. If the values of the two variables are not in this interval, the comparison result is false

Integer i = 100;
Integer j = 100;
System.out.print(i == j); //true

Integer i = 128;
Integer j = 128;
System.out.print(i == j); //false


The reason for article 4: when java compiles Integer i = 100, it will be translated into Integer i = Integer.valueOf(100) . in the java API, valueOf of Integer type is defined as follows. Numbers between - 128 and 127 will be cached. When Integer i = 127, the Integer object 127 will be cached. When Integer j = 127 is written next time, it will be directly fetched from the cache, so there will be no new.

public static Integer valueOf(int i){
    assert IntegerCache.high >= 127;
    if (i >= IntegerCache.low && i <= IntegerCache.high){
        return IntegerCache.cache[i + (-IntegerCache.low)];
    }
    return new Integer(i);
}


reference resources: (13 messages) what's the difference between int and Integer in Java foundation _chenliguan's blog - CSDN blog _intinteger

6, What is the difference between logical operator & and & & and what is the difference between logical operator & and bitwise operator? Please give ex amp les respectively

operatoroperationgive an exampleOperation rules
&Andx&yx. When y is true, the result is true
&&Conditions andx&&yx. When y is true, the result is true

difference:

1. & when performing an operation, the expressions on the left and right sides of the operator are first executed by the operation, and then the results of the two expressions are operated

2. & & during operation, if the operand obtained from the expression on the left can determine the operation result, the expression on the right will not be operated, that is, short circuit effect

For example:

public class LogicAnd {
       public static void main(String[] args){   
           int out = 10;
           boolean b1 = false;
           if ((b1 == true) && (out += 10) ==20){
               System.out.println("equal, out="+out);
           } else {
              System.out.println("Wait, out="+out);
           }
       }
}

out=10

public class LogicAnd {
       public static void main(String[] args){   
           int out = 10;
           boolean b1 = false;
           if ((b1 == true) & (out += 10) ==20){
               System.out.println("equal, out="+out);
           } else {
              System.out.println("Wait, out="+out);
           }
       }
}

out=20

7, What statements can be used in the Java language to jump out of multiple loops? Please give examples

1.break:

break; (jump out of this layer loop)

break lab;   (jump out of the outer loop of multiple loops)

Where: lab is a user-defined label.

The break lab statement is used in the loop statement. The lab label must be written in front of the outer loop entry statement to make the program flow exit the outer loop specified by the label.    

 

lab:
            for(int i =0; i<2; i++) {
                 for(int j=0; j<10; j++) {
                      if (j >1)  { 
                         break lab; 
                      }
                     System.out.println("break");
                 }
            }

Output result: break break

2.continue:

Continue; (jump out of this cycle of this layer and continue the next cycle of this layer)

continue lab; (jump out of this cycle of the outer layer and continue the next cycle of the outer layer)

lab is a user-defined label.

When there are nested multi-layer loops in the program, the labeled continue lab statement can be used to jump from the inner loop to the outer loop. At this time, a label should be added before the entry statement of the outer loop. For example:

        lab:   for(int i =0; i<2; i++) {
                      for(int j=0; j<10; j++) {
                            if (j >1)  { 
                                 continue lab; 
                            }
                           System.out.println("continue");
                      }
                     System.out.println("************");
                 }

Output result: continue continue continue

Topics: Java