Java syntax (3): Variables

Source: Internet
Author: User
Java syntax introduction (3): variables-general Linux technology-Linux programming and kernel information. The following is a detailed description. To store a data in Java, it must be contained in a variable. The data type determines the value that a variable can be assigned and the operations on the variable. Two basic elements for defining a variable are: type and identifier. You can use the following syntax to describe the variable:

Type identifer [, identifer]; this statement tells the compiler to create a variable using the type of "type" and the name of "identifer, here, the semicolon tells the compiler that this is the end of a description statement. The comma and identifier in the square indicate that you can put several variables of the same type in the same statement for description. Variable names are separated by commas.

After you create a variable, you can assign values to it or use operators to perform some operations on it. The type determines the different types of data represented by variables. There are two variables in Java. The most basic types are simple type variables, which are not built on any other types. integers, floating points, Boolean, and character types are all of this type (note that they are not the same as other programming languages, the string here appears as an instance of a class). In addition, Java can define and construct another variable type: class, which is based on a simple type, it includes values, variables, and methods. It is a combination of data and code.

1: Description of Integer Variables

An integer variable can be divided into four different types based on the memory size occupied. The short integer is a byte variable with only eight characters long and then a short integer. It has 16 characters, the int type has 32 bits and the long integer long is 64 bits. The following is an example of these integer variables.

Byte bCount; (memory occupied: 8 Bits)
Short sCount; (memory usage: 16 Bits)
Int nCount; (memory occupied: 32 Bits)
Long LCount; (memory occupied: 64 Bits)
Int nx, ny, nz; (in memory: 32 Bits)

2: Description of floating point Variables

The floating point type can be illustrated by the keyword float or double. The float Type Floating Point variable is used to represent a 32-Bit Single-precision floating point number, while the double Type Floating Point variable is used to represent a 64-bit double-precision floating point number. The floating point number indicated by the double type is more accurate than the float type.

Float areas;
Double weihgt;
3: character variable description

Java uses a 16-bit Unicode Character Set. Therefore, the Java character is a 16-bit unsigned integer, and the character variable is used to store a single character. For example:

Char;
A = 'C ';
4: Boolean variable description

Boolean has two logical values: true and false. In addition, logical operators also return boolean values, for example:

Boolean onClick;
MouseOn = true;
The boolean type is an independent type. The boolean type in Java does not represent the integers 0 and 1, and cannot be converted to numbers.

5: Scope of use of Variables

When you describe a variable, it will be introduced into a range, that is, the name can only be used within a specific range of the program. The scope of use of a variable ranges from where it is explained to the end of the block where it is located. 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 main block does not include the function block, any reference to I in the function block is incorrect. The same applies to variable c of the balanced type.

In a specific scenario, a variable can be hidden by another variable, for example, a variable is described in a block, create a new block in the block and define the variable with the same name in it. In this way, in the second block, the program uses this variable to indicate the variable defined for the second time. In this case, the first variable is hidden, and the author does not recommend using this variable definition method. An example of variable hiding is as follows:

Class Example

Public static void main (String args [])

Int I ;//***
Boolean try = true;
While (try)

Int I; // the following references to variable I refer to the I defined here
......
// The following references to variable I refer to the I defined in ***
......

When defining a variable, you must first define its activity scope and name it based on its actual functions. In addition, you should try to use detailed annotations, these methods allow you to clearly distinguish variables, and the hidden problems of variables will be greatly reduced.

6: type conversion

The System method System. in. read returns an integer value, but you often want to use it as a character. Now the question is, what should you do when there is an integer and you need to convert it into a character? You need to convert a type to a character. You can use the following statement to convert data from one type to another:

Int;
Char B;
A = (int) B;
The int in parentheses tells the compiler that you want to convert the character into an integer and put it in a. On the other hand, if you want to convert the character to the opposite, you can use:

B = (char);
It is very important to remember that the length of an integer is different from that of a balanced variable. The length of an integer is 32 characters long and that of a balanced variable is 16 characters long. Therefore, when you convert an integer to a balanced variable, information may be lost. Similarly, when you convert a 64-bit long integer to an integer, you may also lose information because the long integer may have more information than 32-bit. Even if the two measuring tools have the same number of digits, such as the integer and floating point type (both 32-bit), you will lose the information during decimal conversion. Java does not allow automatic type conversion, when you perform type conversion, make sure that the target type can accommodate all the information of the original type, and there will be no loss of information for type conversion:

Source Type Target type

Byte-> short-> char-> int-> long-> float-> double
Short-> int-> long-> float-> double
Char-> int-> long-> float-> double
Int-> long-> float-> double
Long-> float-> double
Float-> double
It should be noted that when you execute a type conversion not listed here, it may not always lose information, but it is very dangerous to perform such a theoretically insecure conversion.
Related Article

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.