1.

If I hadn’t had a problem with the code yesterday, I probably wouldn’t have noticed the difference between Double and Double;

Problem 1: An int can be cast to double, but I accidentally wrote double as double in the project (the main problem is that I do not understand the use of double and double, so I confused). Cannot cast ‘int’ to ‘java.lang.Double’

Double is a Java class: java.lang.Double; And double is the base data type

Problem 2: Item check found that a double can be converted to double without error

        double smD = 2.22;
        System.out.println((Double)smD);
Copy the code

In fact, in JDK1.5 and beyond, Double is a wrapper for Double. If you convert a Double to a Double, then all of the methods in Double can be used for that data.

Automatic packing and automatic unpacking:

Double d =new Double(dou) can box the base data type of Double into a Double wrapper class Double dou =d.doubleValue() can unbox the base data type of DoubleCopy the code

2. What is the difference between Double and Double

  • Double is a class that creates objects and stores them in the heap;
  • Double is a basic data type that creates a reference and keeps it on a stack;
  • Double is an encapsulation of the Double type. There are many built-in methods to convert a String to a Double, and to obtain various attributes of the Double type (MAX_VALUE, SIZE, etc.).
  • Double basic method:

3. Java basic data types

3.1 Automatic type conversion

Definition: numbers indicate that a small range of data types can be automatically converted to a large range of data types;

Data overflow problem: When two ints are multiplied, the result is out of the int representation. Workaround: Generally convert the first data to a large range of data types and then perform operations with other data types;

Int count = 100000000; int price = 1999; long totalPrice = count * price; The output is negative; Int count = 100000000; int price = 1999; long totalPrice = (long) count * price;Copy the code

3.2 Forcing type Conversion

Force display to convert one data type to another, beware of data overflow:

int ii = 300;
byte b = (byte)ii;
Copy the code

300 is already out of the range of byte representations, so it is converted to a meaningless number.

3.3 Type Promotion

Type promotion means that in expressions of different data types, types are automatically promoted to data types that represent a large range of values

long count = 100000000; int price = 1999; long totalPrice = price * count; Price is int, count is long, and the operation result is long. The operation result is normal and no overflow occurs.Copy the code