Basic data types and conversions between Java basics

Source: Internet
Author: User



1. Boolean (Boolean): True/false, can not be replaced by 0 or not 0, the conversion between data is not possible.

2. Integer type

* * The integer type defaults to int, and a long type represents a range that is larger than the INT table number, so when a long type is declared, the constant is added with L (l).

such as: Long l1=12345678//value within the range of int, no error

Long l2=8888888888;//value is outside the int range, error occurs

Long l3=8888888888l; That's right

3. Floating-point type:

* Floating-point type is double by default, so the float type declaration needs to be f/f later or with a cast character.

float f1= (float) 0.1;

Float f2=0.1f;

The difference between the two: F1 is a eight-byte double type, cast to a four-byte float type.

The F2 itself is a four-byte float type.

4. Character type (char)

Each character occupies two bytes; The value is placed in the "";

Second, the basic data type conversion:

Each data type is arranged by size (size of table) from small to large:

Byte, Short, char--int--long--float--double

1. Principles to be followed when converting:

1) The type of small capacity is automatically converted to the type of capacity;

2) If the type with large capacity is converted to a small capacity type, the conversion character should be strengthened;

3) The Byte,short,char does not convert to each other, and the three are first converted to the int type when calculating;

4) The literal constant defaults to the double type, and the integer constant defaults to the int type;

* Why can the character type and the shaping, floating-point type of operation?

Answer: The character type is also a number inside the machine. When the calculation is performed, the asck code of the character is obtained before the calculation.

2. Conversion Example:

Int i1=123;

Int i2=456;

Double D1 = (I1+I2) *1.2//small size type automatically converted to a large capacity type

Float F1 = (float) (I1+I2) *1.2///large size type when converting to a small capacity type, you need to add the cast character

Byte b1=1;

Byte b2=2;

byte b3= (Byte) (B1+B2); Byte,short,char conversion is enhanced when the calculation is first converted to int, and large capacity (int) is converted to a small-capacity type (byte)


Basic data types and conversions between Java basics

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.