Java learns a little bit about data types every day

Keywords: Java

Special Statement: The following contents are from videos or books of different online courses, which are arranged and merged. If there are any mistakes in sorting them out, we should be grateful for forgetting to point them out in time and making progress together.

Data Types for Java Introduction

Java data types fall into two categories

1. Basic data types

  • Numeric type (two types):

_Integer types: byte, short, int, long (from small to large order)

_Range of values:

     -byte:

    public static void main(String[] args) {  
    byte x = Byte.MIN_VALUE;
    byte y = Byte.MAX_VALUE;
    System.out.println("The range of byte type between "+x+" and "+ y);  

    Output: The range of byte type between -128 and 127  
 }

     - short:

    public static void main(String[] args) {  
    short x = Short.MAX_VALUE;
    short y = Short.MIN_VALUE;
    System.out.println("The range of short type between "+x+" and "+ y);  

    output: The range of short type between -32768 and 32767 
}

     - int:

    public static void main(String[] args) {    
    int x = Integer.MAX_VALUE;
    int y = Integer.MIN_VALUE;
    System.out.println("The range of int type between "+x+" and "+ y);  

    output: The range of int type between -2147483648 and 2147483647
}

     - long:

    public static void main(String[] args) {
    long x = Long.MAX_VALUE;
    long y = Long.MIN_VALUE;
    System.out.println("The range of long type between "+x+" and "+ y);  

    out put: The range of long type between -9223372036854775808 and 9223372036854775807

}

Floating point type: float,double (from small to large)
_Range of values:

     -float:

    public static void main(String[] args) {
    float x = Float.MAX_VALUE;
    float y = Float.MIN_VALUE;  
    System.out.println("The range of float type between "+x+" and "+y);  

    output:The range of float type between -3.4028235E38 and 1.4E-45

}

     -double:

    public static void main(String[] args) {
    double x = Double.MAX_VALUE;
    double y = Double.MIN_VALUE;
    System.out.println("The range of double type between "+x+" and "+y);  

    output: The range of double type between -1.7976931348623157E308 and 4.9E-324

}
  • Character type: char
    When assigning character variables, we use single quotation marks instead of double quotation marks, otherwise there will be an error.
    For example:

    public static void main(String[] args) {
    char x = 'a';
    System.out.println("The value of a is "+x);
    
    output: The value of x is a
    }
    
    
    
    public static void main(String[] args) {
    char x = "b";  
    System.out.println("The value of a is "+b);  
    
    output:  
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Type mismatch: cannot convert from String to char
    b cannot be resolved to a variable.
    }
    
  • boolean type: boolean
    The assignment results of this type are TRUE and False only.
    For example:

    public static void main(String[] args) {
    boolean x = true;
    boolean y = false;
    System.out.println(x+" and "+y);  
    
    output: true and false  
    }  
    

Special attention should be paid to:

_1. How to select the right data type?

_a). In line with the actual situation
For example, when recording students'height, we should choose float type, that is, decimal type, not integer type.

_b). Consider beforehand the size and scope of action that needs to be assigned.
_

    public static void main(String[] args) {
    short x = 20000000;
    System.out.println(x);  

    output: 
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Type mismatch: cannot convert from int to short

}   

_2. Data Accuracy?
For example, when the decimal 3.14 is assigned to an int variable, we will lose accuracy, because int is an integer type and only records three lost decimal parts. If low-precision values such as integer 4 are assigned to
_double will not cause accuracy loss.

_3. float assignment problem?
For example, float assignment needs to add "f" at the end to indicate that the type is float, otherwise the system will default to double type.

_4. Computing problems between different data types?
For example, if the value of int type is divided by the value of double type 2.5, the result is 4.0 which is consistent with that of double type.

_5. Computing problems in output statements?
For example:

    public static void main(String[] args) {
    int x = 10;
    int y =20;
    System.out.println("This is wrong: "+x+y);
    output: 1020
    System.out.println("This is correct"+(x+y));
    output:30

}  

We can see that the first output result is converted to string format, so when you need to calculate in the output statement, you must add brackets.

_6. Data can be mandatory conversion
For example:

    public static void main(String[] args) {

    int x = 10;
    float y = (float) x;
    System.out.println(y);  

    output: 10.0

}  

2. Reference data types

  • Class:
  • Interface:
  • Array:

Unfinished...
The update time is Monday, Wednesday, Friday and Sunday.

Posted by sb on Thu, 21 Mar 2019 13:51:52 -0700