Java variables (identifier; data type; variable classification)

Java variables

A variable name - the identifier

  • Identifier numbers, letters, underscores _, dollar signs $ composition, but can not start with a number.
  • Identifier sensitive to letter case.
  • Identifier no length limit.
  • Can not keywords as identifiers.
/ * Valid identifier: * / 
int AMOUNT =. 17 ;
 int $ = Apple. 8 ;
 int the _index =. 3 ;
 int Height = 180 ;
 / * no valid identifier: * / 
int  a float = 3.14;   // can not use the key word 
int #Index = 2;     // not use # 
int . 3A =. 9;         // can begin with a number

 

Two data types, variable - The basic data types, reference types

Variable data types

 

Third, the classification of variables - local variables, member variables, static variables

(A) local variables: the method as defined in statement blocks, at the beginning of the life cycle of the method from the statements or statements at the end of the block.

★ before using local variables must be declared and initialized.

public  class TestVarible {
     public  static  void main (String args []) { 
        { 
            int Age;     // Age is a local variable, only part of this statement block 
            System.out.printf ( "D% = Age", Age); // compiled error, age not initialized 
        } 
        Age = 18 is;        // compiler error, age not defined 
    } 
}

 

(B) the member variables: the methods defined within an external, class variables, objects are always associated with the life cycle.

★ automatically initializes the member variable declaration, the default initial value in the table below:

Default initial value
type of data The initial value
int 0
char '\u0000'
double 0.0
boolean false
public  class TestVarible {
     int testValue;   // member variable, initial value 0 
}

 

(C) static variables: the use of static-defined variables, like always accompany the life cycle.

★ automatically initialized when you declare a static variable, the default initial value of the member variables of the same.

public  class TestVarible {
     static  Double Score;     // static variables, score belong to the class, the initial value of 0.0 
    Boolean In Flag;            // member variable, flag belong to the object, the initial value to false 
    public  static  void main (String args []) { 
        { 
            int Age;         // local variables, age belongs to the present block of statements 
            Age =. 17 ; 
        } 
        int Fighting = 88;   // local variables, fighting method belonging 
    } 
}

 

 Fourth, variable, constant variable - declarations, initialization

The only difference ★ often variable and variable is: constant argument can no longer be changed after initialization.

Data type variable name;            // declare variables 
final variable name data type;      // constant variable declarations 
variable name = value;                // initialize variables

★ When initializing variables, the default integer type int, float Default type double.

Fifth, the basic data types

★ low range of variables can be assigned to a variable high range, need to add the conversion when out of range!

(A) integer: byte, short, int, long

1. Basic information

type of data used internal memory It represents the range definition
byte 1 byte -128~127 -
short 2 bytes -32768~32767 -
int 4 bytes About 2.1 billion -
long 8 bytes -263~263-1 Increasing the end 'L' or 'l'

2. Representation

  • 10 hex:-
  • Hex: start with 0x or 0X
  •   Octal: beginning with 0
  •   Binary: begin with 0b or 0B
public  class TestVarible {
     public  static  void main (String args []) {
         / * proper declaration and initialization * / 
        int A = 320000 ;
         int B = 'on' ;
         Long C = A;
         Long D = 3200000000L;   // value exceeds 2.1 billion, the need to increase the conversion: adding at the end of "L" or "L" 

        / * illegal initialization * / 
        byte E = 128;            // beyond the scope of 
        unsigned int F = 200 is;    // unsigned absence 
    } 
}

 

(B) Float: float, double

1. Basic information

type of data used internal memory It represents the range definition
float 4 bytes -3.403E38 ~ 3.403E38 Increasing the end 'F' or 'f'
double 8 bytes -1.798E308 ~ 1.798E308 Increase in 'D' or 'd' end

2. Representation

  • 10 hex:-
  • Scientific notation: aEb or aeb (a is a real number, b is an integer)
public  class TestVarible {
     public  static  void main (String args []) {
         / * proper declaration and initialization * / 
        a float A = 3.14F ;
         a float B = ( int ) 6.89 ;
         a float C = 0x0123;            // 0x0123 = 291, in in the range of 2.1 billion 

        / * illicit initialization * / 
        a float D = 1E4;               // compile error, 1e4 double type 
        a float E = 3.03d;             // compile error, 3.03d double type 

        / * a float type and double-type best Do not compare * / 
        a float m = 0.1f;
         Double n-= 0.1 ; 
        System.out.println (m == n-);    // output to false 
    } 
}

 

(C) Character: char

1. Basic information

char type 2 bytes, used in the form of Unicode encoding, which indicates a range from 0 to 65,535.

2. Representation

  • Character constant: 'x'
  • Hex: '\ uxxxx' (x in the range 0 ~ f)
  • Escape character : '\ x' (x have different meanings)
public  class TestVarible {
     public  static  void main (String args []) {
         / * proper declaration and initialization * / 
        char A = 'step' ;
         char B = 24179 ;
         char C = '\ u51e1' ; 
        System.out.printf ( "% C% C% C" , a, B, C); 

        / * illicit initialization * / 
        char D = '\ uabcg';   // G exceeds the range of 0 ~ f 
        int E = 'a' ;
         char F = E ;          // E type int, char type higher 
    } 
}

 

(D) Boolean: boolean

 

 

 

 

 

 

 

 [Reference]:

Variable data types Photo: Speed School

 

Guess you like

Origin www.cnblogs.com/bpf-1024/p/12481911.html