★java Syntax (ii) —————————— data types and load Exchange

Source: Internet
Author: User

Integer type:

Model 1.byte: 8-bit (1-byte) range: -128~127

Usage: byte x = 35;

Model 2.short: 16-bit (2-byte) range: -32768~32767

Usage: short x = 999;

Model 3.int: 32-bit (4-byte) default integer type

Usage: int x = 8;

Type 4.long: 64 bits (8 bytes) followed by "L" or "L", in order to distinguish the number "1", we use "l"

Usage: long x = 89L;

Floating-point types:

Type 1.float: single-precision floating-point type, 32-bit (4-byte) followed by "F" or "F"

Usage: float x = 3.14F; float y = 5.667f;

Type 2.double: Double-precision floating-point type, 64-bit (8-byte) default floating-point type

Usage: Double x = 23.666;

Character type:

Char Type: single character, 2 bytes, single quote

Usage: char x = ' a ';

Boolean type:

Boolean: Logical type, only true and false two values

Usage: boolean x = true; Boolean y = false;

Conversions between data types:

1. Automatic conversion (implicit conversion): Compatible

Usage: byte→short→int→long→float→double

range from small to large for automatic conversion

2. Cast:

Usage: (data type to convert) variable name;

int h = (int) 1234L;

Example:

practice in the package class; Public classdata type Conversion { Public Static voidMain (string[] args) {intA = - ; intb =9; floatg, H; G= A/b; System. out. println ("A/ B ="+g); H= (float) A/b;//Forced ConversionsSystem. out. println ("A/ B ="+h); }}

When two integers are divided, the number after the decimal point is truncated, and the result of the operation remains an integer, which is required to be cast;

As long as one of the integers is converted to a decimal type;

★java Syntax (ii) —————————— data types and load Exchange

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.