A good memory is better than a bad pen. 1-java basic data types and conversions

Source: Internet
Author: User


First, the basic data type

1 , Boolean (Boolean)

Boolean (Boolean): True/false, can not be replaced by 0 or not 0, cannot be converted between data.

2 , Integer type

The integer default is int , and a long type represents a range that is larger than the int table, 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

longl2=8888888888;// The value is outside the int range, error occurs

longl3=8888888888l;// correct

3 , floating-point types

floating-point type defaults to double, so the float type declaration needs to be f/f later, or with a cast character.

Float and double , float is a 4-byte precision; a double is a 8-byte precision

float f1= (float) 0.1;

Float f2=0.1f;

F1 is a eight-byte double type that is cast to a four-byte float type.

F2 itself is a four-byte float type.

4 , character type (char)

Each character occupies two bytes;

Second, the basic data type conversion

The data types are arranged by capacity size (number of table range size) from small to large:

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

1. principles to be followed during conversion

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

2 when a large-capacity type is converted to a small capacity type, the conversion must be strengthened;

3 ) byte, the Short,char does not convert to each other, and the three are first converted to the int type at the time of calculation;

4 The real number constant defaults to the double type, and the integer constant defaults to the int type;

5 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. Basic Data type Conversion sample

Int i1=223;

Int i2=446;

Double D1 = (I1+I2) *1.2// types with small capacity are automatically converted to large capacity types

Float f1 = (float) (I1+I2) *1.2// when a large-capacity type is converted to a small-capacity type, you need to add a cast character

Byte b1=1;

Byte b2=2;

byte b3= (byte) (B1+B2); Byte,Short, char is converted to int at the time of calculation, and the conversion character is enhanced when the bulk (int) is converted to a small-capacity type (byte)

A good memory is better than a bad pen. 1-java basic data types and conversions

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.