Home > Java > javaTutorial > body text

Java basics variable introduction

巴扎黑
Release: 2017-06-23 16:37:11
Original
1425 people have browsed it

Declaration and use of variables

Concept:

A variable refers to a storage area in memory, which must have its own Name (variable name), type (data type), the data in this area can continuously change values ​​within the range of the same data type;

Precautions for using variables:

Variables in Java must be declared before they can be used;

The scope of the variable: the valid range within a pair of {};

Variables need to be initialized before they can be used;

Definition of variables:

Data type Variable name = initialization value;

The use of variables is to access the storage in the memory area pointed to by the variable name. value.

Data type:

Basic data type (from large to small):

Numeric type:

Integer type: long (-263~263-1), int (-231~231- 1), short (-215~215-1), byte (-128~127)

long naming rules: long (data type) myLong = 100L; You need to add L or l after the value

Floating point type: double (double precision), float (single precision)

float naming rules: float myFloat = 100.2F; required Add F or f

after the value. Character type: char can only point to one character (English, Chinese, punctuation marks, Japanese and other languages ​​and escape characters) such as: a But multiple characters such as ab are not acceptable;

Boolean type: boolean

Reference data type: class (class), interface (interface), array ([])

There is a very commonly used type in class: String This is a string type. It is a class that has been defined in Java and can be used directly.

Operations between variables (regardless of Boolean type):

Automatic type conversion: When data types with small capacity and data types with large capacity do During operation, data types with small capacity will be automatically converted into data types with large capacity

From small to large: char, byte, short——>int——>long——>float ——>double

When performing operations on char and numeric types, the char type will be converted into the corresponding int type number according to the ASCII code table for operation;

byte and short are also The result of char operation is automatically converted to int type data by default;

The sample code is as follows:

 1 class TestVeriable{ 2    public static void main(String args[]){ 3        int i1=10; 4        short s1=2; 5        int i2 = i1+s1; 6       7        float f1=12.5F; 8        float f2=f1+i2; 9 10        long l=12L;11        float f3 = l;12 13        char c1= 'a';14        char c2= 'A';15        int i3 = c1+1;16        int i4= c2+1;17  18        //short、byte、char之间的运算结果都被自动转化为int类型19        short ss1=12;20        byte bb1= 1;21        char cc1='a';22        int ii1=ss1+bb1+cc1;23        24    }        
25 }
Copy after login

Forced type conversion : To convert a data type with a large capacity to a data type with a small capacity, a forced type conversion symbol must be used: ()

For example: long l1=100L;

int i1=(int )l1;

Problems that need to be paid attention to when using forced type conversion: loss of data precision

For example: byte b1=(byte)l1;

Operations between strings and basic data types: only connection operations can be performed between the two, that is, splicing two data together, and the result is still a string type data;

The sample code is as follows:

1 class TestVeriable1{2    public static void main(String args[]){3        String str1 = "abc";4        int  i1 = 123;5        String str2 = str1+i1;6        System.out.println(str2);7 }8 }
Copy after login

The above is the detailed content of Java basics variable introduction. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template