Learn Java next day (bottom) variables and data type conversions

Source: Internet
Author: User


Variable

variable : The amount that can be changed

three elements of a variable : Variable type variable name value

declaration of a variable :

    • int x; Declaration ending with a semicolon is a complete statement

    • Rules for naming variables: numbers, letters, underscores, and $ (except for Chinese and Chinese symbols) that begin with a letter, and cannot have spaces in the middle, although not in Chinese

    • Problem, but prone to errors, case sensitive, you can use some symbols, but do not use $,$ generally used in the compiler, but also cannot use Java reserved characters as the variable name

    • One row can declare multiple variables int x,a,y;

    • Assignment of variables:

    • Declaring a variable requires explicit initialization of the variable assignment. Note that variables must be assigned to be used.

    • Variable name = value; for example: x = 1;

    • An assignment can be written on one line with a declaration, for example: int x = 1;

    • Variables can eat more change values during use

Considerations for Variables :

    • Variables in Java must first be declared before use must be assigned a value

    • Java can make variable declarations from anywhere

    • The declaration of variables in Java is as close to the first use as possible, which is a good way to become canonical

Extension Section :

Note: In the process of running the program, the value in the space is changed, and this memory space is called a variable. For ease of operation, a name is given to the space, called the variable name, and the value in the memory space is the value of the variable. Therefore, the application of memory space, the variable does not necessarily have a value, to think that the variable has a value, you must put the value.

When there is no assignment, the variable will have the default initial value.

Byte 0
Short 0
Int 0
Long 0L
Float 0.0F
Double 0.0
Char ' \u0000 '
Boolean Flase
Reference type Null (non-referencing object)

data type conversion;

First, the Boolean type cannot be converted to another type

Beware of memory overflow issues

The type conversion is divided into the following situations;

1, automatic data type conversion

Classic examples:

byte a = 1000; Error 1000 range greater than byte

FLOAT a1 = 1.5; Error 1.5 is double cannot assign value to float

BYTE A2 = 100; correct JVM during compilation, for a value that is the default int type, if it is assigned to a data type that is smaller than the Int range K (short byte char) and so on, the JVM will judge if the value is less than K will automatically return the int type to type K. If the range of values is greater than the type K, an error will be taken. Because K doesn't fit, and you're not forced to convert.

When the value of int is in type K, then the JVM will perform an implicit numeric conversion at this time.

In other cases, a type with a small range of numeric values is converted to a type with a large range of numeric values, and the JVM automatically promotes the numeric type during compilation, in which the accuracy should be at least unchanged (float--double accuracy elevation)

The conversion between the short type data and the char type data can only be coerced, because while they are using the same space, the range of values is different. (short nearly plus or minus 32,000 char is 0~65535).

The calculations between Byte,short,char do not convert to each other, but instead transfer to the INT type. Even if the operation of the same type is first converted to an int in the calculation, then copying to the Byte,short,char requires a forced type conversion.

2. Implicit data-type conversions

Automatic type conversion, automatically completed by the system, from a small storage range to a large storage range of automatic conversion, byte-short,char-int-long-float-double

3. Explicit data type conversions (forcing data type conversions)

With a large range cast to a type with a small range, the precision may be lost. Manual conversions are required.

During conversion, for example, converting int to byte forces the selection to be eight bits. The result is an exception

4. Mathematical operation data type conversion and the possible required casts

In mathematical operations, the data type is converted to a large range of data types. The result needs to be cast to the desired data type.

Cast method:

In numeric meet plus coercion type conversion character

Double d = 34.2

int x = (int) d

Floating-point casts are cast to integers, and decimal digits are shed.


Learn Java next day (bottom) variables and data type 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.