Java default types, type conversions, constants and variable notes

Tech guy

  • java
  • storage
  • The data type

Abstract: Default type: in Java, integer default type is int, decimal default type is double, single character default type is char, true and false default type is Boolean, String default type is String. Variable and constant type conversions: Because there is a default type, conversion is required whenever another type is needed. Conversion from small to large is automatic, and conversion from large to small is mandatory.

Default type:

In Java, integers are int, decimals are double, single characters are char, true and false are Boolean, and strings are String by default.

 

Variable and constant type conversions:

Because there is a default type, casting is required when other types are needed. Converting from a small type to a large type is automatic, and converting from a large type to a small type requires casting. Example: int I = 237196223; Short s=(int)237196223; short s=(int)237196223; .

 

Application rules for variable names:

Apply for a data type that is contiguous in memory; The variable name is stored in the named area, and it points to the memory address number, which is also a representative identifier of the data value. It is like a tag number. If a variable needs to be modified, it can be used to call the operation with its tag name.

Variable names can be applied with an underscore and start with a letter and end with a number; It cannot start with a digit or a special character. Developers can use variable names to value, store, modify, call, and so on.

 

Automatic type conversion:

In Java, small data types can be automatically converted to large data types, just as a small glass of water can be poured into a large glass without fear of overflow. For example, byte b=127; short s=b; Byte types are smaller than short types and can be converted automatically without loss of precision.

 

Cast:

Large data types need to be cast to small data types. Because the water in the big cup has to be poured into the small cup and the water will overflow, and of course the accuracy will be lost. For example, short s=(byte)128; The output is -128, because the cast loses precision.

A cast is performed in binary. For example, an int is 32 bits and a short is 16 bits. A cast from an int to a short intercepts the following 16 bits.

 

Variables and constants:

A constant can only be assigned once, and its value cannot be changed again. In addition, if the assigned value is the same, it refers to the same memory address. Only when the assigned value is different, a new memory address is created. Variables can be arbitrarily changed in the storage range and can be assigned multiple times.

The memory allocation of a variable is active. Each request allocates a new memory, and the memory address is independent.

 

Data types have three layers in memory: constant layer, identity layer, and data layer. The constant layer is naturally used to store the values in the constant can not be changed, and the assigned values refer to the same memory address. In the identification layer are the variable names, which refer to the address numbers of the variable values. The data layer is filled with mutable data variables, whose values can be repeatedly modified or assigned.




This article from ZeroOne01 51 cto blog, the original link: http://blog.51cto.com/zero01/1955641, if you want to reprint, please contact our author

Use the cloud habitat community APP, comfortable ~

For details, please click

To:

Related articles

The net friend comment on