I. Variables

Definition of Variables

Regarding variables, literally understood, they are quantities that can change and are an extremely important part of programming languages.

Declaration and Composition of Variables

In a strongly typed language like Java, each variable must have its type declared.

A variable in Java is the most basic storage unit in a program, consisting of three elements: variable name, variable type, and scope.

Definition of Variables

For the definition of variables, Java specifies the basic format as follows:

Data type + variable name = value (multiple variables of the same type can be declared separated by commas, but this is generally not recommended as it reduces code readability)

Notes

When defining and declaring variables, the following points need attention:

Each variable must have a type, which can be a primitive type or a reference type, but it must be specified.

The variable name must be a valid identifier, and the declaration should be a complete statement, so each declaration must end with a semicolon.

Examples are as follows:

int a=1;
int b=2;
int c=3;
String name="zhangsan";
char x='X';
double pi=3.14;

In this code snippet, six variables are defined, belonging to different data types. Regardless of the data type, the way of definition and declaration should comply with Java language writing specifications.

Types of Variables

(1) Local Variables

In Java, if a variable is declared within a method, it only acts within that method and cannot be used in other classes or methods. Such variables that act within a certain scope are called local variables and must be declared and initialized. The code example is as follows:

public void method() {
int i = 0; // Local variable: must be declared and initialized
}

If you want to use it outside this method, you must redefine and declare it;

(2) Instance Variables

Instance variables belong to an object. After being defined and declared, they can be used in class methods. The example code is as follows:

int age;
public static void main(String[] args) {
    int i = 0;
    // Variable type variable name = new Demo1(variable value);
    Demo1 demo1 = new Demo1();
    System.out.println(demo1.str);
}

In the above code, an instance variable is first defined, then a new object is created in the method.

Since no value is assigned to the object variable str in the code, the output value here is the default initial value of the String type.

In this process, the default value of the int type is 0, the default value of the boolean type is false, and the default output of all non-primitive data types is null.

(3) Class Variables

Class variables can be directly output after being defined and declared, and they appear and disappear along with the class. If the class is destroyed or deleted, the defined variable attributes and values will become invalid. The example code is as follows:

static double count = 4500; // Class variable
String str; // Instance variable
public static void main(String[] args) {
    Demo1 demo1 = new Demo1();
    System.out.println(demo1.str);
    System.out.println(count);
}

In this class, a double-type variable count is defined and assigned a value of 4500.

The running result shows that the double-type data is directly output, so class variables can be used directly.

II. Constants

A constant is a special variable whose value is not allowed to change during program operation after being set. It is defined using the keyword final.

Its naming rule is: final constant name = constant value

In this process, final is a modifier, and the order of writing does not matter. For example:

static final int a = 10;
    final static double count = 4500; // Class variable
    public static void main(String[] args) {
    System.out.println(a);
    System.out.println(count);
}

It can be seen that in the above code, the constant declarations are written in different ways.

The running result shows that the order of applying modifiers has no impact on constant declaration. In code writing, the way of defining and declaring constants is generally used for constant references that cannot be changed. For example, when developing a program, if you need to define a fixed and unchangeable data in the program, you can use this way.

III. Additional Notes

Naming conventions for variables:

  1. All variables, methods, and class names should follow the principle of “knowing the meaning by the name”: the meaning and function of a variable or method can be understood from its name.
  2. Class member variables follow the camelCase principle: except for the first word, the first letter of each subsequent word is capitalized, such as: LastTool.
  3. Local variables should start with a lowercase letter and follow the camelCase principle, for example: twoName.
  4. Constants should be defined with uppercase letters and underscores, for example: final MAX_VALUE = 100;
  5. Class names should start with an uppercase letter and follow the camelCase principle, such as: Main, MaxSize.
  6. Method names should start with a lowercase letter and follow the camelCase principle: run(), runRun();
Avatar

By BytePilot

Because sharing makes us better. Let’s learn, build, and grow — one byte at a time.

Leave a Reply

Your email address will not be published. Required fields are marked *