Java (2) data type conversions, variables, and constants

Source: Internet
Author: User

Data type

1, Integer int (4 bytes), short integer (2 bytes), Long (8 bytes), Byte type byte (1 bytes).

2, character char (2 bytes)

3. Float single-precision float (4 bytes) Double-precision Doubles (8 bytes)

4, bool Boolean (1bit/1 bytes)

1 byte = 8 bit

Conversion of basic types

1, the basic types in Java can be converted to each other,theboolean type is more special cannot be converted to other types.

2. conversions are divided into default conversions and casts:

Default conversions: Types with small capacity are converted by default to large-capacity types

Byte (1)-->short (2)->int (4)-->long (8)-->float (4)-->double (8)

CHAR (2)--

all floating-point types (float,double) are uniformly considered to be larger than the integer size and do not look at the number of bytes.

Char and short are at the same level

Calculations between Byte,short, andchar do not convert to each other, first convert to int

Byte B1 = 10;

Short S1 = 20;

Int i1 = B1 +s1;

Java Program Sub-compilation period and run time

The compile phase just knows that the result of B1 + s1 is the int type.

int type cannot be assigned directly to byte type

byte B2 = B1 +s2;

can be done by

Byte B3 = 100;

Cast:

converting large-capacity types to small-capacity types requires casting

Note: You can assign an integer value directly to byte,short,Char , as long as the range is not over

in many types of mixed operations, the first is to convert all the data into a large capacity, and then compute

Double d = 5.998;

int i = (int) d;

Byte B = (byte) 5.998;

Note: When you force a type conversion to convert a large space-occupying type to a space-occupying type, the interception method is not rounded. Therefore, when you try to cast a type to another type, you sometimes lose some precision, even the values are completely different.

Variable:

about variables in the Java language

1. How to define variables in Java, syntax:

Data Type variable name;

2, How to assign values to variables, syntax:

variable Name = value;

3. What is the nature of the variable?

A variable is a piece of space in memory that has "type", "Name", "value"

For example:

Int A;//create a space in memory, the type of space is shaped, give the space name A

Assign Value

A = 100;

to access that piece of content through the variable name

SYSTEM.OUT.PRINTLN (100);

Repeat Access

SYSTEM.OUT.PRINTLN (100);

SYSTEM.OUT.PRINTLN (100);

modified to: System.out.println (a);

Deep variables:

request a space of type int, name i, and assign a value of

Int i = ten;

System.out.println (i);

re-assign value

i = 100;

System.out.println (i);

variables can be re-assigned, but cannot be re-declared

variable names cannot be duplicated in the same domain.

int i =200; //Error

are not declared in the same region. (in the new method)

-------

a variable in the Java language must be declared before it is assigned a value before it can be used

int A;

A = 10;

System.out.println (a);

declare three variable i1,i2,i3, type is int ,i1,i2 Two variable is not assigned, only The i3 variable is assigned a value.

Int i1,i2,i3 = 456;

Error

System.out.println (I1); I1 cannot be accessed without assignment.

SYSTEM.OUT.PRINTLN (i3);

Java (2) data type conversions, variables, and constants

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.