Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Introduction to Java Syntax (3): Variables
Introduction to Java Syntax (3): Variables
In order to store data in Java, it must be contained in a variable. The data type determines what values can be assigned to variables and what operations can be performed on variables. The two basic elements that define variables are type and identifier. In general, you can use the following syntax to describe variables:

Type identifier [,identifier]; This statement tells the compiler to create a variable of type "type" and name "identifer", and the semicolon here will tell the compiler that this is the end of a statement; Commas and identifiers in the grid indicate that several variables of the same type can be put in the same statement, and the variable names are separated by commas.

After creating a variable, you can assign a value to it or perform some operations on it with operators. Types will determine the different kinds of data represented by variables. There are two kinds of variables in the Java language. The most basic ones are simple type variables, which are not based on any other types. Integer, floating point, Boolean and character are all of this type (note that unlike other programming languages, strings appear here as instances of classes); In addition, Java can define and construct another variable type: class. These types are based on simple types, including values, variables and methods, and are a composite structure that combines data and code.

1: Description of integer variable.

Integer variables can be divided into four different types according to the memory size. The shortest integer is byte, which is only eight bits long, and then the short integer is 16 bits, the int is 32 bits, and the long integer is 64 bits. The following are examples of these integer variables.

Byte bCount (memory usage: 8 bits)

Short account; (Memory usage: 16 bits)

Int nCount (memory usage: 32 bits)

Long LCount (memory usage: 64 bits)

int nx,ny,NZ; (Memory usage: 32 bits)

2. Description of floating-point variables

Floating-point type can be explained by keyword float or double. Float floating-point variables are used to represent 32-bit single-precision floating-point numbers, and double floating-point variables are used to represent 64-bit double-precision floating-point numbers. Floating point numbers represented by double types are more accurate than float types.

Floating area;

Double weight;;

3. Description of character variables

Java uses the Unicode character set of 16 bits. Therefore, a Java character is an unsigned integer of 16 bits, and a character variable is used to store a single character. For example:

char a;

a=? c? ;

4. Boolean variable description

Boolean types have two logical values, true and false. In addition, logical operators return Boolean values, such as:

Boolean onClick

mouseOn = true

Boolean type is an independent type. Boolean types in Java do not represent two integers, 0 and 1, and cannot be converted into numbers.

5. The scope of use of variables

When you specify a variable, it will be introduced into a range, that is, the name can only be used in a specific range of the program. Variables are used from the place of interpretation to the end of the block. The block is defined by two braces, for example:

Class example

Public static void main(String args[])

int I;

......

Public void function ()

char c;

......

The integer variable I is described in the main method, because the block of main does not include the function block, so any reference to I in the function block is wrong. The same is true of the character variable c.

Under certain circumstances, a variable can be hidden by other variables, such as: explain a variable in one block, create a new block in this block and define a variable with the same name in it, so that in the second block, the program references the variable defined for the second time. In this way, we say that the first variable is hidden, and the author does not recommend this method of defining variables. Examples of variable hiding are as follows:

Class example

Public static void main(String args[])

int I; // * * *

Boolean try = true

While (try)

int I; //The following reference to the variable I refers to the I defined here.

......

//The following reference to the variable I means that I is defined in * * *.

......

When defining a variable, we must first define its activity range and name it according to its actual function. Besides, you should try to use detailed notes. These methods can make you clearly distinguish variables and greatly reduce the problem that variables are hidden.

6: Type conversion

The system method System.in.read returns an integer value, but you usually want to use it as a character. The question now is, what should I do when I have an integer and need to turn it into a character? You need to convert the type into characters. To convert from one type to another, you can use the following statement:

int a;

char b;

a =(int)b;

The int in parentheses tells the compiler that you want to convert characters to integers and put them in. On the other hand, if you want to do the opposite transformation, you can use:

b =(char)a;

It is very important to remember that the bit lengths of integer and character variables are different. Integer length is 32 bits and character length is 16, so when you convert from integer to character, you may lose information. Similarly, when you convert a 64-bit long integer into an integer, you are likely to lose information because the long integer may contain more information than 32 bits. Even if two quantities have the same number of digits, such as integer and floating point (both 32 bits), information will be lost when converting decimals. Java does not allow automatic type conversion. When performing type conversion, it should be noted that the target type can accommodate all the information of the original type, and the types that will not lose information are:

Original Type Target Type

byte-〉short-〉char-〉int-〉long-〉float-〉double

Short integer-long integer-floating point-double precision

char -〉int -〉long -〉float -〉double

int -〉long -〉float -〉double

long-float-double

Floating-point-double precision

It should be noted that when you perform a type conversion not listed here, you may not always lose information, but it is dangerous to make such a theoretically unsafe conversion.