Java Data types

Source: Internet
Author: User

a primitive type, or a built-in type, is a special type that differs from a class in Java. They are the most frequently used types in our programming, so they are always in the interview question, and in this article we will review the basic types of knowledge from several aspects of the interview.

There are eight basic types, each of which has a corresponding wrapper class. For more information about them, see the following table:

the base types can be divided into three categories, char types, Boolean type Boolean, and numeric type Byte, short, int, long, float, double. Numeric types can also be divided into integer types, byte, short, int, long, and floating-point number type float, double. There are no unsigned numeric types in Java, and their range is fixed and will not change with the machine hardware environment or the operating system. In fact, there is another primitive type Void in Java, which also has a corresponding wrapper class java.lang.Void, but we cannot manipulate them directly. We do not need to force memory for the range of values of the base types of numeric types, because their values are already defined in the corresponding wrapper class as constants. Take a look at the following example:
Java Code
Public class Primitivetypetest {
Public static void Main (string[] args) {
//Byte
System.out.println ("base type: Byte bits number:" + byte.size);
System.out.println ("Packing class: Java.lang.Byte");
System.out.println ("Minimum value: byte.min_value=" + byte.min_value);
System.out.println ("Maximum value: byte.max_value=" + byte.max_value);
System.out.println ();
  
// Short
System.out.println ("Basic type: Short bits number:" + short.size);
System.out.println ("Packing class: Java.lang.Short");
System.out.println ("Minimum value: short.min_value=" + short.min_value);
System.out.println ("Maximum value: short.max_value=" + short.max_value);
System.out.println ();
  
//int
System.out.println ("base type: int bits number:" + integer.size);
System.out.println ("Packing class: Java.lang.Integer");
System.out.println ("Minimum value: integer.min_value=" + integer.min_value);
System.out.println ("Maximum value: integer.max_value=" + integer.max_value);
System.out.println ();
  
//Long
System.out.println ("Basic type: Long bits number:" + long.size);
System.out.println ("Packing class: Java.lang.Long");
System.out.println ("Minimum value: long.min_value=" + long.min_value);
System.out.println ("Maximum value: long.max_value=" + long.max_value);
System.out.println ();
  
//Float
System.out.println ("Basic type: float bits number:" + float.size);
System.out.println ("Packing class: Java.lang.Float");
System.out.println ("Minimum value: float.min_value=" + float.min_value);
System.out.println ("Maximum value: float.max_value=" + float.max_value);
System.out.println ();
  
//Double
System.out.println ("Basic type: Double bits number:" + double.size);
System.out.println ("Packing class: Java.lang.Double");
System.out.println ("Minimum value: double.min_value=" + double.min_value);
System.out.println ("Maximum value: double.max_value=" + double.max_value);
System.out.println ();
  
//Char
System.out.println ("base type: char bits number:" + character.size);
System.out.println ("Packing class: Java.lang.Character");
//Output the Character.min_value to the console in numerical form rather than as a character
System.out.println ("min: character.min_value="
+ (int) character.min_value);
//Output the Character.max_value to the console in numerical form rather than as a character
System.out.println ("Maximum value: character.max_value=" )
+ (int) character.max_value);
    }  
}  

Operation Result:

1. Basic type: Byte bits number: 8
2. Packing type: Java.lang.Byte
3, Minimum: byte.min_value=-128
4, Maximum value: byte.max_value=127
5.
6. Basic type: Short bits number:
7. Packing Type: Java.lang.Short
8, Minimum: short.min_value=-32768
9, Maximum value: short.max_value=32767
10.
11, basic type: int bits number:
12. Packing Type: Java.lang.Integer
13, Minimum: integer.min_value=-2147483648
14, Maximum value: integer.max_value=2147483647
15.
16, Basic type: Long bits number:
17. Packing Type: Java.lang.Long
18, Minimum: long.min_value=-9223372036854775808
19, Maximum value: long.max_value=9223372036854775807
20.
21. Basic type: Float bits number:
22. Packing Type: java.lang.Float
23, Minimum: float.min_value=1.4e-45
24, Maximum value: Float.max_value=3.4028235e38
25.
26. Basic type: Double bits number:
27. Packing Type: java.lang.Double
28, Minimum: double.min_value=4.9e-324
29, Maximum value: double.max_value=1.7976931348623157e308
30.
31. Basic type: char bits number:
32. Packing Type: Java.lang.Character
33, Minimum: character.min_value=0
34, Maximum value: character.max_value=65535

The minimum and maximum values for float and double are output in the form of scientific notation, and the trailing "e+ number" indicates how many times the number before E is multiplied by 10. For example, 3.14E3 is 3.14x1000=3140,3.14e-3 is 3.14/1000=0.00314.

When you compare the results with the above table, you will find that the minimum value of float, Double Two types is not the same as the value of Float.min_value and Double.min_value, which is why? In fact, Float.min_value and Double.min_value refer to the smallest positive numbers that can be represented by the float and double types respectively. That is, there is a case where the value between 0 and ±float.min_value, the Float type cannot be represented, and the value between 0 and ±double.min_value Double type cannot be represented. This is not surprising because the values in these ranges exceed their accuracy range.

The base types are stored in the stack, so they are accessed faster than the instance object of the corresponding wrapper class stored in the heap. Starting with Java5.0 (1.5), the Java Virtual machine (Java VM) can perform the basic types and the automatic conversion between their corresponding wrapper classes. So we use their wrapper classes in terms of assignment, parameter passing, and math, like using basic types, but that doesn't mean that you can call their wrapper class with a basic type to have a method. In addition, the wrapper classes for all basic types (including void) use final adornments, so we cannot inherit them to extend new classes or to override any of their methods.

Reference http://www.cnblogs.com/kelin1314/archive/2010/09/11/1824145.html

Java Data types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.