Variables in Java and their transformations

Source: Internet
Author: User

Variables in Java


A variable or variant that refers to a number that has no fixed value that can be changed. Variables are expressed as non-numeric symbols, usually in the Latin alphabet. Variables are the opposite of constants, and the use of variables is to generalize the way the instructions are described.

In essence, A variable is actually a small area of memory, using variable names to access the area, so each variable must be declared before it is used, and then it must be populated before it can be used.


Classification of variables

(1) Classification by data type: basic data type variable and reference data type variable

(2) Partitioning by declaration: Local Variables and global variables (member variables)

Local variables: variables defined within a method or block of statements;

Global variables (member variables): external to the method, variables defined internally by the class


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/89/56/wKioL1gQSZHjDLilAACr1ke6Dr8255.png "title=" The data type of the variable. png "alt=" Wkiol1gqszhjdlilaacr1ke6dr8255.png "/>


In the Java domain, the basic data type variable is stored in the data itself, whereas the reference type variable holds the spatial address of the saved data . Frankly, the basic data type variables are stored directly in the drawer, and the reference data type variable is stored in the drawer key, key and drawer one by one corresponding.


declaration of a variable

In Java, we describe variables by three elements: variable type , variable name , and variable value .

The form is as follows:

Variable type variable name = variable Value


Rules for using variables

1, the variables in Java need to be declared after use ;

2, when the variable is used, you can declare the variable to initialize at the same time, you can also declare the post-assignment value;

3, the variable can only assign one value at a time, but may modify several times ;

4. The variables defined in the main method must be assigned before they can be output ;

5, although there is no error in grammar, but in the actual development, the variable name is not recommended to use Chinese , easy to create security risks, such as the late cross-platform operation garbled and so on;



Basic Data type variables:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/89/56/wKioL1gQSonz3TP1AAGGt_pLXdw942.png "title=" Basic data type variable. png "alt=" Wkiol1gqsonz3tp1aaggt_plxdw942.png "/>


Basic data type variable description:

1, Integer type variable (No decimal parts):

byte type (byte): Byte b=3;

Short integer type: c=23;

Integral type (int): int i=223;

Long integer: L=222222l;long l=222;

When assigning a variable of type long, it is important to note that if the assignment does not exceed the range of values of type int, it is not added L, omitted,

If the assignment is greater than, the letter "L" will be added to the end of the value to indicate that it is a long variable. Without L, the default data type is int.


2. Floating-point type variable (with decimals)

Single-precision floating point number (float): float f=2.48f;

Double-precision floating-point number (double):

Double d1=100.1;double D2=199.3d;

In a Java program, if the float type data is assigned without F, it is defaulted to the double type of data, and when you assign a value to double type data, you can add no D;

In addition, you can assign an integer value to a floating-point type variable in the program as follows:

float f=100;

Double d=100;


3. Character type variable

Character (char): Char c= ' a '; char ch=97;


A character type variable is used to store a single character, and a character variable of each char type consumes 2 bytes. When assigning, it is necessary to enclose the character in single quotation marks, or an integer, and the computer automatically converts the integers to the corresponding characters.

Any content that exceeds one font cannot be declared as a character type.


In the above example, the value 97 corresponds to the character a.


Note: string is a common reference data type that is used to represent a string. In the development of programs, many operations are done using strings, such as user names, passwords, e-mail addresses, etc. in the system.


4. Boolean type variable (Boolean)


A Boolean type variable is used to store a Boolean value that has only two values, true and false, as follows:


Boolean b=false;//declares a variable of type Boolean, with an initial value of false


b=true;//change the value of B to True


Type conversions for variables:

Conversion of basic data types

In Java, the order of the types from low to High is: Byte, short, char, int, long, float, double.

1, Automatic conversion: when from low to high can be automatically converted.

byte b;int i=b;long l=b;float f=b;double d=b; Then these can be automatically converted, is legal, but Byte, short, char is a peer cannot be automatically converted, you must use the cast.

Automatic conversion

Integer, real, and character data can be mixed.

Operations, the different types of data are converted to the same type first, then the operation, the conversion from low to advanced (representing the range from small to large):

Low------------------------------------------------------------> High

byte-> short/char-> int-> long-> float-> Double

2, Cast: from high to low, you must use cast.

int i=99;byte b= (byte) I;char c= (char) i;


forcing type conversions must meet the following rules :

1. You cannot type convert a Boolean type (C can).

2. Object types cannot be converted to objects of unrelated classes.

3. Forced type conversions must be used when converting large-capacity types to small-capacity types.

4. The conversion process may result in overflow or loss of precision, such as:

int i = 128; byte b= (byte) i;

(byte) 255 = =-1 (byte) 0x5634 = = 0x34

The conversion of floating-point numbers to integers is obtained by discarding decimals, rather than rounding

(int) 23.7 = = (int) -45.89f = =-45






Variables in Java and their transformations

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.