大数据 java 代码示例_Java变量类型与示例

大数据 java 代码示例

Java变量 (Java variables)

Variables are the user-defined names of the memory blocks, and their values can be changed at any time during program execution. They play an important role in a class/program as they help in to store, retrieve a data value.

变量是用户定义的存储块名称,它们的值可以在程序执行期间随时更改。 它们在类/程序中起着重要的作用,因为它们有助于存储,检索数据值。

Java中变量的类型 (Types of the variables in Java)

There are three types of Java variables,

Java变量有三种类型

  1. Instance Variables

    实例变量

  2. Local Variables

    局部变量

  3. Class/Static Variables

    类/静态变量

1)实例变量 (1) Instance Variables)

  • Instance variables are declared in a class but outside a Method, Block or Constructor.

    实例变量在类中声明,但在方法,块或构造函数之外。

  • Instance variables have a default value 0.

    实例变量的默认值为0

  • These variables can be created only when the object of a class is created.

    仅当创建类的对象时才能创建这些变量。

Example:

例:

public class Bike {
    public String color;

    Bike(String c) {
        color = c;
    }

    public void display() {
        System.out.println("color of the bike is " + color);
    }

    public static void main(String args[]) {
        Bike obj = new Bike("Red");
        obj.display();
    }
}

Output

输出量

Color of the bike is Red

2)局部变量 (2) Local Variables)

  • Local variables are the variables which are declared in a class method.

    局部变量是在类方法中声明的变量。

  • We can use these variables within a block only.

    我们只能在一个块中使用这些变量。

Example:

例:

public class TeacherDetails {
    public void TeacherAge() {
        int age = 0;
        age = age + 10;
        System.out.println("Teacher age is : " + age);
    }

    public static void main(String args[]) {
        TeacherDetails obj = new TeacherDetails();
        obj.TeacherAge();
    }
}

Output

输出量

Teacher age is : 10

3)类变量/静态变量 (3) Class Variables/Static Variables)

  • This can be called Both Class and Static Variable.

    这可以称为类和静态变量

  • These variables have only one copy that is shared by all the different objects in a class.

    这些变量只有一个副本,该副本由类中的所有不同对象共享。

  • It is created during the start of program execution and destroyed when the program ends.

    它在程序执行开始时创建,并在程序结束时销毁。

  • Its Default value is 0.

    其默认值为0

Example:

例:

public class Bike {
    public static int tyres;
    public static void main(String args[]) {
        tyres = 6;
        System.out.println("Number of tyres are " + tyres);
    }
}

Output

输出量

Number of tyres are 6


翻译自: https://www.includehelp.com/java/variables-types-with-examples.aspx

大数据 java 代码示例

你可能感兴趣的:(大数据 java 代码示例_Java变量类型与示例)