Java variables (identifier; data type; variable classification)

Java variable

1, Variable name identifier

  • An identifier consists of a number, a letter, an underscore, a dollar sign $, but cannot begin with a number.
  • Identifiers are case sensitive.
  • Identifier has no length limit.
  • Cannot use Keyword As an identifier.
/* Legal identifier: */
int amount = 17;
int $apple = 8;
int _index = 3;
int height = 180;
/* Illegal identifier: */
int float = 3.14;  // Cannot use keyword
int #Index = 2;    // Out of commission#
int 3a = 9;        // Cannot start with a number

 

2, Data type of variable -- basic data type, reference type

 

3, Classification of variables -- local variables, member variables, static variables

(1) Local variable: defined within a method or statement block, the life cycle starts from the declaration to the end of the method or statement block.

★ local variables must be declared and initialized before use.

public class TestVarible {
    public static void main(String args[]) {
        {
            int age;    // age Is a local variable, only belonging to this statement block
            System.out.printf("age = %d", age); // Compile error, age Not initialized
        }
        age = 18;       // Compile error, age No definition
    }
}

 

(2) Member variable: a variable defined outside a method or inside a class. Its life cycle is always accompanied by an object.

★ when member variables are declared, they will be initialized automatically. The default initial values are shown in the following table:

Default initial value
data type Initial value
int 0
char '\u0000'
double 0.0
boolean false
public class TestVarible {
    int testValue;  // Member variables, Initial value is 0
}

 

(3) Static variables: variables defined by static are used, and the life cycle accompanies the class all the time.

★ when a static variable is declared, it will be initialized automatically. The default initial value is the same as the member variable.

public class TestVarible {
    static double score;    // Static variables, score Belong to category, Initial value is 0.0
    boolean flag;           // Member variables, flag Belong to object, The initial value is false
    public static void main(String args[]) {
        {
            int age;        // local variable, age Only belongs to this statement block
            age = 17;
        }
        int fighting = 88;  // Local variable, fighting Belong to method
    }
}

 

IV. variable and constant variable -- Declaration and initialization

★ the only difference between constant variable and variable is that constant variable cannot be changed after initialization.

Data type variable name; / / variable declaration
 final data type variable name; / / constant variable declaration
 Variable name = value; / / variable initialization

★ when initializing variables, integers are of type int by default, and floating-point numbers are of type double by default.

5, Basic data type

★ the low range variable can be assigned to the high range variable. If it is out of the range, it needs to be converted!

(1) Integer: byte, short, int, long

1. Basic information

data type Occupied memory Scope of representation Definition
byte 1 byte -128~127 -
short 2 byte -32768~32767 -
int 4 byte About 2 billion 100 million -
long 8 byte -263~263-1 Add 'l' or 'l' at the end

2. Representation

  • 10 hexadecimal:
  • Hexadecimal: starts with 0x or 0x
  • Octal: starts with 0
  • Binary: starts with 0b or 0b
public class TestVarible {
    public static void main(String args[]) {
        /* Correct declaration and initialization */
        int a = 320000;
        int b = 'Yes';
        long c = a;
        long d = 3200000000L;  // More than 2.1 billion, Need to add conversion: add at the end"L"or"l"

        /* Illegal initialization */
        byte e = 128;           // Out of range
        unsigned int f = 200;   // Non-existent unsigned
    }
}

 

(2) Floating point: float, double

1. Basic information

data type Occupied memory Scope of representation Definition
float 4 byte -3.403E38~3.403E38 Add 'f' or 'f' at the end
double 8 byte -1.798E308~1.798E308 Add 'd' or 'd' at the end

2. Representation

  • 10 hexadecimal:
  • Scientific counting: aEb or aeb (a is real, b is integer)
public class TestVarible {
    public static void main(String args[]) {
        /* Correct declaration and initialization */
        float a = 3.14F;
        float b = (int)6.89;
        float c = 0x0123;           // 0x0123=291, Within 2.1 billion

        /* Illegal initialization */
        float d = 1e4;              // Compile error, 1e4 by double type
        float e = 3.03d;            // Compile error, 3.03d by double type

        /* float It's better not to compare the two types */
        float m = 0.1f;
        double n = 0.1;
        System.out.println(m==n);   // Output is false
    }
}

 

(3) Character type: char

1. Basic information

char type takes up 2 bytes, which is encoded in Unicode, and its representation range is 0-65535.

2. Representation

  • Character constant: 'x'
  • Hexadecimal: 'uxxxx' (x in the range of 0-F)
  • Escape character : '\ X' (x has different meanings)
public class TestVarible {
    public static void main(String args[]) {
        /* Correct declaration and initialization */
        char a = 'step';
        char b = 24179;
        char c = '\u51e1';
        System.out.printf("%c%c%c", a, b, c);

        /* Illegal initialization */
        char d = '\uabcg';  // g Exceeding 0~f Range
        int e = 'a';
        char f = e;         // e by int type, Higher than char type
    }
}

 

(4) boolean: boolean

 

 

 

 

 

 

 

[References]:

Data type picture of variable: Speed school

Tags: Java

Posted on Thu, 12 Mar 2020 04:45:01 -0700 by Hades666