大数据 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 代码示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值