Summary of Java data types

Source: Internet
Author: User

Summary of Java data Types

The Java language provides eight basic types. Six types of numbers (four integers, two floating-point types), one character type, and one Boolean type.
1, integers: including int,short,byte,long
2. Float Type: float,double
3, Character: Char
4, Boolean: Boolean

td>-----
basic type size minimum Max
boolean----- ------
char 16-bit Unicode 0 unicode 2^16-1
byte 8-bit -128 +127
Shor T 16-bit -2^15 +2^15-1
int 32-bit -2^31 +2^31-1
long 64-bit -2^63 +2^63-1
float 32-bit IEEE754 IEEE754
double 64-bit ieee754 IEEE754
void      


Even if you know the IEEE floating-point numbers, you may never have handled this kind of data individually. I'll show you how to handle IEEE floating-point numbers one by one, and the next time your Java project needs to create a floating-point number from a bit sequence, or create a bit sequence from a floating-point number, you know that the float class can do the job.

IEEE floating-point number introduction

The IEEE 754 floating-point single-precision number format defines a bit layout for storing floating-point numbers. In the empty bit layout, a bit is left as the sign bit, eight bits are left as exponent, and 23 bits are left as mantissa. These bits are arranged by the most important bits to the least important bits, as shown in the following example:



31 0
| |
Seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm

which

S = sign Bit
E = exponent
M = Mantissa

You can purchase a full explanation of IEEE floating-point numbers from the IEEE site. (If you search for IEEE 754 online, you should be able to find the free version.) )

The contribution of Float to the Java community

The following example program uses the two methods of float to turn a floating-point number into bits, and then turns those bits into a floating-point number.

Use the float.floattointbits (float f) method to turn a floating-point number into a sequence of bits. This method returns a 32-bit integer that represents the IEEE 754-bit arrangement of floating-point numbers that you provide as parameters.

You can use the float.intbitstofloat (int bits) method to convert in reverse direction. This method takes an incoming integer as an argument and converts the bits into an IEEE floating-point number.

The following is an example program:

public class Bitstip {
public static void Main (String args[]) {
float f = float.parsefloat (Args[0]);
int bits = float.floattointbits (f);

System.out.println ("bits:" + bits);
System.out.println ("Back to float:" + float.intbitstofloat (bits));
}
}

If you've ever had to do this kind of conversion manually, you'll appreciate both of these simple ways to save your job. If you are working with 64-digit numbers, you can choose to use the double wrapper (wrapper) class. This class provides the same method for handling IEEE 754 double-precision floating-point numbers.

It's a good idea to read Javadoc and look at the parts of the conversion between floating-point numbers and bit sequences in order to fully understand what these methods can do for you.

There are two types of data in Java:

(1) Basic data type, this is the first Java data type that Dahne IT training teacher to explain to you.

The eight basic data types for 1.Java are:

Integral type

byte byte 1 byte 8bit the maximum amount of storage data is 255, the data range stored is between -128~127

Short 2-byte 16bit maximum data storage is 65536, data range is -32768~32767

int integer 4 byte 32bit maximum data storage capacity is 2 of 32 minus 1, the data range is negative 2 of 31 times to positive 2 square minus 31

Long integer 8 bytes 64bit maximum data storage capacity is 2 of 64 minus 1, the data range is negative 2 of 63 to positive 2 square minus 63

Floating point Type

Float single Precision 4 byte 32bit data range in 3.4e-45~1.4e38, direct assignment must be preceded by a number F or f

Double dual precision 8 byte 64bit data range in 4.9e-324~1.8e308, you can add D or D when you assign a value or do not add

Character type

char character type with single quotation mark

Boolean type

Boolean Boolean true/false (two results only true)

Knowledge description of the data domain:

1. A bit is the smallest unit of data stored in the computer's postgraduate examination. That is, 1 bits, which can only be 0 or 1.

2. A byte is made up of eight bits, which is eight bits, which can represent 256 integer values, and because Byte is signed in Java (that is, positive and negative), the range bit -128~127.

3. A char consists of two bytes in Java, which is used to represent a character, Unicode encoding, that is an unsigned type.

4.int and float are made up of 4 bytes in Java.

5. Each long or double variable occupies eight bytes of storage space.

6.1t=1024g,1g=1024m,1m=1024k,1k=1024b

Be careful:

1. Integers in the program are int by default, that is, a specific integer value that is passed directly to the method invocation, which by default is int.

2. The type of small range can be automatically converted into a large range. A large range cannot be converted directly into a small range, but can be implemented by forced conversion.

eg
BYTE a=10;

Int
I=a;

(The program is working properly, the output of I is 10)

Eg:int a=10;

Byte
I= (Byte) A;

(The program is working properly, the output of I is 10)

If the value of a is outside the upper or lower bounds of the byte type, the value can be output, but the data of the value will change, changing the following rule:

Suppose a is an int and B is a byte type (other things can be analogous!). ~

If a is greater than the upper value of byte, it should subtract its range n times until the final value falls between -128~127, for example: int a=1000;

Byte
B= (Byte) a;//1000-256-256-256-256=-24

(The output of B is-24)

3. Floating-point conversion to integer is not rounding, but directly truncating the fractional part. Such as:

Double
b=12.5;

Int
i= (int) 12.5;

(The output value is 12 instead of 13!) )

4. Scientific representation of floating-point numbers:

For example: 1.2e+12f (indicates 1.2 times 10 of 12 power)

1e-21f (indicates 1 times 10-21 power)

(2) Object data type (Application data type, class type)

All with Class,interface,abstract
class defined by class;

String is a class defined by Java.

String data is stored in a way that is consistent with Java's basic data types.

How string is defined

String
str= "ABC";

String
Str-new String ();


Welcome to scan QR Code, follow the public number



Summary of 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.