[Crazy JAVA] Java Foundation 03: Explanation of Data Types

Posted by ZachEdwards on Mon, 10 Jan 2022 18:14:52 +0100

Variables are requests for memory to store values. That is, when you create a variable, you need to request space in memory.

The memory management system allocates storage space for a variable based on its type, which can only be used to store that type of data.

Therefore, by defining different types of variables, you can store integers, decimals, or characters in memory.

Two main data types for Java:

  • Basic data types

  • Reference data type

Basic data types

The Java language provides eight basic types. There are six numeric types (four integers, two floats), one character type, and one Boolean type.

Integer type

byte:

  • The byte data type is an 8-bit, signed integer represented by a binary complement.

  • The minimum value is -128 (-2^7); The maximum value is 127 (2^7-1); The default value is 0;

  • The byte type is used in large arrays to save space, mainly instead of integers, because the byte variable takes up only a quarter of the space of the int type.

    Examples: byte a = 100, byte b = -50.

short:

  • The short data type is a 16-bit, signed integer in binary complement

  • The minimum value is -32768 (-2^15); The maximum value is 32767 (2^15 - 1); The default value is 0;

  • Short data types can also save space like byte. A short variable is half the space occupied by an int variable.

    Example: short s = 1000, short r = -20000.

int:

  • The int data type is a 32-bit signed integer represented by a binary complement.

  • The minimum value is -2,147,483,648 (-2^31); The maximum is 2,147,483,647 (2^31-1); The default value is 0;

  • Generally, integer variables default to int type;

    Examples: int a = 100000, int b = -200000.

long:

  • The long data type is a 64-bit signed integer represented by a binary complement.

  • The minimum value is -9,223,372,036,854,775,808 (-2^63); The maximum is 9,223,372,036,854,775,807 (2^63-1);

    The default value is 0L;

  • This type is mainly used on systems that require larger integers;

    Example: long a = 100000L, Long b = -200000L. "L" is theoretically case-insensitive, but if written as "l", it is easy to be confused with the number "1", which is not easy to distinguish. So it's best to capitalize.

float

float:

  • The float data type is a single-precision, 32-bit floating point number that meets the IEEE 754 standard.

  • float saves memory space when storing large floating-point arrays;

  • The default value is 0.0f;

  • Floating-point numbers cannot be used to represent precise values, such as currency;

    Example: float f1 = 234.5f.

double:

  • The double data type is a double-precision, 64-bit, floating-point number that meets the IEEE 754 standard.

  • The default type of floating point number is the double type.

  • The double type also does not represent an exact value, such as currency;

  • The default value is 0.0d;

    Example:

    double   d1  = 7D ;
    double   d2  = 7.; 
    double   d3  =  8.0; 
    double   d4  =  8.D; 
    double   d5  =  12.9867; 

    7 is an int literal quantity, while 7D, 7. And 8.0 are double literals.

Boolean

boolean:

  • The boolean data type represents one bit of information;

  • There are only two values: true and false;

  • This type only serves as a flag to record true/false situations;

  • The default value is false; Example: boolean one = true.

Character

char:

  • The char type is a single 16-bit Unicode character;

  • The minimum value is \u0000 (decimal equivalent is 0);

  • The maximum value is \uffff (that is, 65535);

  • Char data types can store any character; Example: char letter ='A';

Example

There is no need to force memory for ranges of basic types of numeric types, since their values are already defined as constants in the corresponding wrapper class. Take the following example:

public class PrimitiveTypeTest {  
    public static void main(String[] args) {  
        // byte  
        System.out.println("Basic types: byte Binary digits:" + Byte.SIZE);  
        System.out.println("Packaging Class: java.lang.Byte");  
        System.out.println("Minimum value: Byte.MIN_VALUE=" + Byte.MIN_VALUE);  
        System.out.println("Maximum: Byte.MAX_VALUE=" + Byte.MAX_VALUE);  
        System.out.println();  
  
        // short  
        System.out.println("Basic types: short Binary digits:" + Short.SIZE);  
        System.out.println("Packaging Class: java.lang.Short");  
        System.out.println("Minimum value: Short.MIN_VALUE=" + Short.MIN_VALUE);  
        System.out.println("Maximum: Short.MAX_VALUE=" + Short.MAX_VALUE);  
        System.out.println();  
  
        // int  
        System.out.println("Basic types: int Binary digits:" + Integer.SIZE);  
        System.out.println("Packaging Class: java.lang.Integer");  
        System.out.println("Minimum value: Integer.MIN_VALUE=" + Integer.MIN_VALUE);  
        System.out.println("Maximum: Integer.MAX_VALUE=" + Integer.MAX_VALUE);  
        System.out.println();  
  
        // long  
        System.out.println("Basic types: long Binary digits:" + Long.SIZE);  
        System.out.println("Packaging Class: java.lang.Long");  
        System.out.println("Minimum value: Long.MIN_VALUE=" + Long.MIN_VALUE);  
        System.out.println("Maximum: Long.MAX_VALUE=" + Long.MAX_VALUE);  
        System.out.println();  
  
        // float  
        System.out.println("Basic types: float Binary digits:" + Float.SIZE);  
        System.out.println("Packaging Class: java.lang.Float");  
        System.out.println("Minimum value: Float.MIN_VALUE=" + Float.MIN_VALUE);  
        System.out.println("Maximum: Float.MAX_VALUE=" + Float.MAX_VALUE);  
        System.out.println();  
  
        // double  
        System.out.println("Basic types: double Binary digits:" + Double.SIZE);  
        System.out.println("Packaging Class: java.lang.Double");  
        System.out.println("Minimum value: Double.MIN_VALUE=" + Double.MIN_VALUE);  
        System.out.println("Maximum: Double.MAX_VALUE=" + Double.MAX_VALUE);  
        System.out.println();  
  
        // char  
        System.out.println("Basic types: char Binary digits:" + Character.SIZE);  
        System.out.println("Packaging Class: java.lang.Character");  
        // Character as numeric rather than Character.MIN_VALUE output to console
        System.out.println("Minimum value: Character.MIN_VALUE="  
                + (int) Character.MIN_VALUE);  
        // Character as numeric rather than Character.MAX_VALUE output to console
        System.out.println("Maximum: Character.MAX_VALUE="  
                + (int) Character.MAX_VALUE);  
    }  
Basic types: byte Binary digits: 8
 Packaging Class: java.lang.Byte
 Minimum value: Byte.MIN_VALUE=-128
 Maximum: Byte.MAX_VALUE=127
​
Basic types: short Binary digits: 16
 Packaging Class: java.lang.Short
 Minimum value: Short.MIN_VALUE=-32768
 Maximum: Short.MAX_VALUE=32767
​
Basic types: int Binary digits: 32
 Packaging Class: java.lang.Integer
 Minimum value: Integer.MIN_VALUE=-2147483648
 Maximum: Integer.MAX_VALUE=2147483647
​
Basic types: long Binary digits: 64
 Packaging Class: java.lang.Long
 Minimum value: Long.MIN_VALUE=-9223372036854775808
 Maximum: Long.MAX_VALUE=9223372036854775807
​
Basic types: float Binary digits: 32
 Packaging Class: java.lang.Float
 Minimum value: Float.MIN_VALUE=1.4E-45
 Maximum: Float.MAX_VALUE=3.4028235E38
​
Basic types: double Binary digits: 64
 Packaging Class: java.lang.Double
 Minimum value: Double.MIN_VALUE=4.9E-324
 Maximum: Double.MAX_VALUE=1.7976931348623157E308
​
Basic types: char Binary digits: 16
 Packaging Class: java.lang.Character
 Minimum value: Character.MIN_VALUE=0
 Maximum: Character.MAX_VALUE=65535

The minimum and maximum values of Float and Double are output in scientific notation, and the "E+number" at the end indicates how many times the number before E is multiplied by 10. For example, 3.14E3 is 3.14 × 10 3 =3140, 3.14E-3 is 3.14 x 10-3 =0.00314.

In fact, there is another basic type of void in JAVA, which also has a corresponding wrapper class, java.lang.Void, but we can't manipulate them directly.

Type Default

The following table lists default values for each type of Java:

data typeDefault value
byte0
short0
int0
long0L
float0.0f
double0.0d
char'u0000'
String (or any object)null
booleanfalse

Example

public class Test {
    static boolean bool;
    static byte by;
    static char ch;
    static double d;
    static float f;
    static int i;
    static long l;
    static short sh;
    static String str;
 
    public static void main(String[] args) {
        System.out.println("Bool :" + bool);
        System.out.println("Byte :" + by);
        System.out.println("Character:" + ch);
        System.out.println("Double :" + d);
        System.out.println("Float :" + f);
        System.out.println("Integer :" + i);
        System.out.println("Long :" + l);
        System.out.println("Short :" + sh);
        System.out.println("String :" + str);
    }
}

The output of the instance is:

Bool     :false
Byte     :0
Character:
Double   :0.0
Float    :0.0
Integer  :0
Long     :0
Short    :0
String   :null

reference type

  • In Java, variables of reference type are very similar to C/C++ pointers. A reference type points to an object, and a variable that points to an object is a reference variable. These variables are declared with a specific type, such as Employee, Puppy, and so on. Once a variable is declared, the type cannot be changed.

  • Objects and arrays are reference data types.

  • The default value for all reference types is null.

  • A reference variable can be used to reference any compatible type.

  • Example: Site site = new Site("Runoob").

Strongly typed language, weakly typed language

1. Strongly typed languages

Strongly typed languages, also known as Strong Type Definition Languages, are languages that always enforce type definitions, requiring the use of variables to be strictly defined and all variables to be defined before being used.

Once a variable is defined as a type, it will always be that data type unless cast. Strongly typed languages include Java,. net, Python, C++, etc.

For example, an integer is defined and cannot be converted to a string without a forced type conversion.

2. Weakly typed languages

Weak-type language is a language defined by a weak type. A variable is defined by a type, which can be automatically converted according to environmental changes without explicit coercion. Weak-type languages include vb, PHP, javascript, and so on.

A simple understanding is a language in which variable types can be ignored. For example, VBScript is defined as a weak type, in VBScript you can connect the string'12'and integer 3 to get the string'123', and then you can think of it as integer 123 without display conversion. In fact, their types have not changed. VB just automatically adds a clong() or (int)() conversion function to these variables after it determines that an expression contains different types of variables. This is really due to the intelligence of the VB compiler, which is not the strength or weakness of the VB language itself.

For instance:

var A =5;
var B = "5";
SumResult = A +B;
MinResult = A -B;

SumResult's answer is not 10, but 55, which converts the type of A into a string and stitches it together.

MinResult's answer is 0, which converts the type of B into a number and subtracts it.

3. Differences between Strong and Weak Languages

Whether it is a strongly or weakly typed language, the basis of discrimination is whether a linguistic type change will occur implicitly. Strongly typed languages are slightly slower than weakly typed languages, but the strictness of strongly typed defining languages can avoid unnecessary errors.