Java Data Type Range

Source: Internet
Author: User

Data type Range Testing

First, the problem of compiling numerical constants

Constant numbers in Java are compiled by default in int type

Such as:

Long // 10 Guests Long // The default data is int, 11-bit super int range, compiled however, the number has an underscore reminder under Eclipse

If the number used exceeds the int type, here are a few workarounds

1. Add L or L after integer

Long // add L or L to indicate a long compilation

2. Written as floating-point number (double indicates a wider range)

In Java, the direct write integer constant, is the default int type, the integer after plus L can be converted to long.

Directly write floating-point constants, which are defaulted to double, and an integer followed by F can be converted to float.

Double // writes numeric constants in double form, which can be compiled over

Second, the processing method of unsigned int

Java does not have a unsigned int type, the author uses a shift method to represent the maximum value of an unsigned integer

Private Static Final Long Unsigned_int_max = (1L << 32)-1;

By the way, you can use this method to define a constant character instead of a macro definition in C.

Introduction to basic data types

The basic data types are mainly:
BYTE, short, int, long, float, double, char, Boolean
Can be divided into three categories:

    1. Numeric types: Byte, short, int, long, float, double
    2. Character type: Char
    3. Boolean Type: Boolean
Byte
    1. BYTE is a 8-bit data type, occupies 1 bytes (8bit), the default value is 0, its value range is ( -2^7) ~ (2^7-1), that is, 128 ~ 127, so the maximum amount of storage data is 255;
    2. Byte is generally used in large arrays in place of integers, because the byte variable occupies only 1/4 of the space of Int.
    3. Byte Use example: Byte a = 10,byte=-10. You need to pay attention to the value range when using the byte data type!!!
Short
    1. Short is a 16-bit data type, occupies 2 bytes, the default value is 0, its value range is ( -2^15) ~ (2^15-1), that is-32768 ~ 32767, so the maximum data storage is 65536;
    2. Although short is 1/2 of the space occupied by int variables, it is seldom used in practice. Space can also be saved in large arrays.
    3. Short use example: Short A=100,short b=-200;
Int
    1. int is a 32-bit data type, occupies 4 bytes, the default value is 0, its value range is ( -2^31) ~ (2^31-1), that is-2147483648 ~ 2147483647, so the maximum data storage is 2^32-1;
    2. int is the data type is integral type, is one of the most data types we use in the project;
    3. int use example: int a=1000,int b=-2000;
Long
    1. Long is a 64-bit data type, occupies 8 bytes, the default value is 0L, its value range is ( -2^63) ~ (2^63-1), that is-9223372036854775808 ~ 9223372036854775808, so the maximum data storage is 2^ 64;
    2. Long is one of the most common data types that we use in our projects. It is best to take uppercase L at the end of a value when using Long data.
    3. Long use example: Long A=1000l,long b=-2000l;
Float
    1. Float is a 32-bit data type, occupies 4 bytes, the default value is 0, its value range is 3.4e-45 ~ 1.4e38;
    2. Float is a single-precision data type and must be preceded by a number F or F when assigned directly.
    3. Float Use example: float a=10.25f, float b=-20.35f;
Double
    1. The double is a 64-bit data type, occupies 8 bytes, the default value is 0, and its value range is between 4.9e-324 ~ 1.8e308;
    2. A double is a data type that doubles, preferably with a D or D when directly assigning a value.
    3. Double Use example: Double a=10.123d, double b= -10.25644d;
Boolean
    1. Boolean is a Boolean type that occupies 1 bytes, only two values, false and True, and the default value is False.
    2. Boolean can only use a flag to record true or false, which is used in combination with the general and if.
    3. Boolean Use Example: Boolean A=true,boolean b=false;
Char
    1. Char is a character type, occupies 2 bytes, the default value is empty, and the value range is 0~65535, which is \u0000 ~ \uffff.
    2. The char data type can store any character.
    3. Char Use example: Char A=1,char b= ' a ';

The level of the numeric type is from low to high, respectively:
Byte,char,short (these three-lateral)-->int-->float-->long-->double
It is the automatic type conversion, which is automatically converted by the system, from low to high level.

For example, convert data of type int to float type data.
Example:

int i=10;float j=i;System.out.println("i:"+i+",j:"+j);

Results:

i:10,j:10.0

If the high level is turned to low, then a cast is required, that is, coercion of type conversions.
For example, convert data of type int to data of type byte.
Example:

int i=127;int j=128;byte bye=(byte)i;byte bye2=(byte)j;System.out.println("i:"+i+",bye:"+bye);System.out.println("j:"+j+",bye2:"+bye2);

Results:

i:127,bye:127j:128,bye2:-128

Description: Because the byte type is 8 bits and the maximum value is 127, when an int is cast to a byte type, a value of 128 causes an overflow. So when you do a forced type conversion, pay attention to the range of values!

It is necessary to pay attention to the accuracy of the data when casting, otherwise the data can be lost accurately.
For example, a double type of data is converted to float type data.
Example:

double d=10.1111115;float f=(float) d;System.out.println("f:"+f+",d:"+d);

Results:

f:10.111112,d:10.1111115

After knowing the level of the data type, what is the final data type if you are calculating with different data types?
Here we use these several to test:
1.byte type of data plus short type of data;
Data of type 2.short plus int type data;
3.int type of data plus a long type of data;

Here we use this method to get the final data type

public static String getType(Object o){         return o.getClass().toString();     

Code:

Short a=1; byte b=2; int C =3; Long d =4; System. Out. println (GetType (a)); System.out. println (GetType (b)); System.out. println (GetType (c)); System.out. println (GetType (d)); System.out. println (GetType (a+b)); System.out. println (GetType (a+c)); System.out. println (GetType (c+d));            

Results:

ClassJava.Lang.ShortClassJava.lang.byte class java. Lang.integerclass java. lang.long class java. Lang.integerclass java. lang.integer class java. Lang.long            

Is it a little surprised to see the result? So what is it for?
When the data of different numeric types is added here, it is automatically converted to the highest level.
So why is the data of the short type plus the byte type the int type?
Because in the Java world, if the operation is smaller than the type int, Java compiles them uniformly and strongly into the int type.
If the above is not well understood, then here we will do an experimental test, through the results to verify.
Example:

short a=1,b=2;byte c=3,d=4;System.out.println(getType(a+b));System.out.println(getType(c+d));

Results:

class java.lang.Integerclass java.lang.Integer

So here we can conclude that, in a numeric type, if the level is less than int, the final data type is automatically converted to int, and if it is higher than int, the final data result will take the highest one!

Java Data Type Range

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.