1, the introduction

The Java language provides eight basic data types. These are byte, short, int, long, float, double, Boolean, char.

String is an object, not a basic data type

The eight basic data can be divided into different types

  • Four integer types: byte, short, int, and long
  • Two floating-point types: float, double
  • The value contains 1 character type: char
  • 1 Boolean: Boolean
Basic data types The default value Size (bytes) Value range The sample
byte 0 1 7-1-2 ^ 7-2 ^ byte a=10;
boolean false 1 true\false boolean b=true;
char ‘\u0000’ 2 0-2 ^ 16-1 char c=’c’;
short 0 2 15-2-2 ^ ^ 15-1 short d=10;
int 0 4 – 2 ^ 31-2 ^ 31-1 int e=10;
long 0 8 63-2-2 ^ ^ 63-1 long f=10L;
float 0.0 f 4 – 2 ^ 31-2 ^ 31-1 Float g = 10.0 F;
double 0.0 d 8 63-2-2 ^ ^ 63-1 Double h = 10.0;

The basic data types “==” are all values.

Boolean The Java Virtual Machine Specification specifies 4 bytes, but depends on whether the virtual machine implementation follows the specification, so 1 byte or 4 bytes is possible.

2. Unpacking and packing problems

  • Unpacking and packing

    Boxing automatically converts the base data type to wrapper type; Use the integer.valueof method. Unpacking automatically converts wrapper types to primitive data types; Use the integer.intValue method.

Integer total = 99; 
// The system performs automatic boxing for us when executing the above code
Integer total = Integer.valueOf(99);

int totalprim = total; 
// Unpack the box automatically
int totalprim = total.intValue();
Copy the code
  • Range of problems

    Extend it with a question that is often asked in an interview:

Integer i = 400; 
Integer j = 400; 
System.out.println(i==j);  //false
Integer o = 12; 
Integer k = 12;
System.out.println(o==12);  //true
Copy the code

ValueOf creates data using Integer. ValueOf creates data using Integer. ValueOf creates data using Integer.

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

If the value ranges from -128 to 127, it returns the instance from the cache. Otherwise, new an Integer object. New Integer is a loading process. The loading process creates the corresponding object, which consumes memory. Therefore, the loading process increases memory consumption and affects performance. So the final result is false because the two objects I and J have different memory addresses.

  • == value comparison problem
int a =200;
Integer b = new Integer(200);
Integer c = 200;
System.out.println(a==b); //true
System.out.println(a==c); //true
System.out.println(b==c); //false
Copy the code

This is often asked in interviews, and is often used in code.

If a==b, a==c, then the Integer calls value.intValue() to unpack the base data type. If there is a base data type, only the value is compared

If b== C, these two data types are never equal. Unpacking and unpacking are only for comparison of basic data types. Integer is not a basic data type.

Conclusion:

In any case, Integer and new Integer are not equal. They don’t go through the unpacking process because they store memory in different locations.

Both integers are non-new integers, true if the number is between -128 and 127, false otherwise.

If both new integers are new, then false.

Int == new Integer(); int == new Integer(); int = new Integer();

3, int and Integer

  • Integer inherits the Object class. Integer is an Object type with its own attributes and methods. Integer is a wrapper class for int. Int is a Java basic data type.
  • Integer The default value is null, and int the default value is 0.
  • Int can be directly operated on, but Integer cannot be directly operated on. Integer can only be operated on when unpacked and converted to int.

4. Default problems

In Java:

  • The default type of an integer is int.
  • The default float type is double, otherwise f and d must be followed

Floating-point numbers without f are double by default, and float is an error. There are two ways to solve this problem:

  • Closing withf
  • Strong turns using (float)

Integer.parseint () and integer.valueof ()

ParseInt () and valueOf() are both methods of Integer objects. The input parameter is a String.

  • parseInt
public static int parseInt(String s) throws NumberFormatException
Copy the code

Converts a string argument as a signed decimal integer. If the conversion is not possible, a NumberFormatException is thrown.

  • valueOf
public static Integer valueOf(String s) throws NumberFormatException
Copy the code

Returns a new Integer object initialized to the specified String value, or a NumberFormatException thrown if it cannot be converted.

String str = "And 12";
int  num = Integer.parseInt(str);
System.out.println(num); / / - 12

Integer  num2 = Integer.valueOf(str);
System.out.println(num2); / / - 12

int num3 =Integer.parseInt("HaC"); //java.lang.NumberFormatException
Copy the code

Int and Integer conversion:

int a=A.intValue();
Integer A=Integer.valueOf(a);
Copy the code

Integer classes: The implementation of valueOf methods for Integer, Short, Byte, Character, and Long is similar. Double: The implementation of the valueOf methods for Double and Float is similar. A different object is returned each time.

6. Accuracy loss

byte a = 5;
int b = 2;
float c  = 6f;
double d =0.03;
double d2 =300.03;
System.out.println(a/b); // 2 a will be converted to int
System.out.println(b/c); // 0.33333334 b is converted to float
System.out.println(a+d+d2); // 301.05999999999995 A will be converted to double, but the result should be 301.06
Copy the code

Different types of data are converted to high-precision data types during operation.

In fact, this kind of precision loss often occurs in the calculation of double values, especially when the decimal point is involved, which can cause errors in the program. Because computers operate in binary, and computers can have accuracy problems in the binary representation of decimals.

So we can use the java.math.BigDecimal class when we are working with high-precision data

7. String and integer concatenation problems

String a = "1";
int b = 1;
int c = 2;
System.out.println(a + b + c); / / 112
System.out.println(b + c + a); / / 31
Copy the code

A +b + C from left to right, a+b gives 12, which is a String, and then concatenates C, which is a String

B + c + a from left to right, b + C gives 3, which is an int, and then concatenates a to make a String.

Leave a few questions

1) Let’s set the following two assignment statements:
a = Integer.parseInt("1024");
b = Integer.valueOf("1024").intValue();
Copy the code

The following statement is correct.

A, A is an integer variable, and b is an integer object.

B, a is an integer object, and B is an integer variable.

C, A, and B are integers and they have the same value.

D, A and B are both integer variables and they have the same value.

D) intValue() is the basic data type that converts an Integer object type to an int;

2) What type is the result of the expression (short)10/10.2*2?

A short

B int

C double

D float

The answer is C. In Java, if you don’t declare it after a number, floating-point numbers default to double.

(short)10/10.2*2 (short) (10/10.2 *2); (short)10/10.2*2 (short) (10/10.2 *2); (short) (10/10.2 *2); (short) (10/10.2 *2); The latter is to force the value short after the calculation.


That’s the basic introduction to Java data types.