java中的变量_Java中的变量

java中的变量

In this tutorial we will learn about Java variables, various types of variables along with code examples to understand Java variables.

在本教程中,我们将学习Java变量,各种类型的变量以及理解Java变量的代码示例。

什么是变量? (What is a Variable?)

When we want to store any information, we store it in an address of the computer. Instead of remembering the complex address where we have stored our information, we name that address.The naming of an address is known as variable. Variable is the name of memory location.

当我们要存储任何信息时,我们会将其存储在计算机的地址中。 我们无需记住存储信息的复杂地址,而是为该地址命名。地址的命名称为变量。 变量是内存位置的名称。

In other words, variable is a name which is used to store a value of any type during program execution.

换句话说,变量是一个名称,用于在程序执行期间存储任何类型的值。

To declare the variable in Java, we can use following syntax

要在Java中声明变量,我们可以使用以下语法

datatype variableName;

Here, datatype refers to type of variable which can any like: int, float etc. and variableName can be any like: empId, amount, price etc.

在这里, 数据类型指的是变量的类型,可以是: int,float等,而variableName可以是任何类型: empId,amount,price等。

Java Programming language defines mainly three kind of variables.

Java编程语言主要定义三种变量。

  1. Instance Variables

    实例变量

  2. Static Variables (Class Variables)

    静态变量(类变量)

  3. Local Variables

    局部变量

Java中的实例变量 (Instance variables in Java)

Instance variables are variables that are declare inside a class but outside any method,constructor or block. Instance variable are also variable of object commonly known as field or property. They are referred as object variable. Each object has its own copy of each variable and thus, it doesn't effect the instance variable if one object changes the value of the variable.

实例变量是在类内部但在任何方法,构造函数或块外部声明的变量。 实例变量也是对象变量,通常称为字段或属性。 它们被称为对象变量。 每个对象都有其自己的每个变量副本,因此,如果一个对象更改了变量的值,则它不会影响实例变量。

class Student
{
    String name;
    int age;
}
Here name and 名称age are instance variable of Student class. 年龄是Student类的实例变量。

Java中的静态变量 (Static variables in Java)

Static are class variables declared with static keyword. Static variables are initialized only once. Static variables are also used in declaring constant along with final keyword.

静态是使用static关键字声明的类变量。 静态变量仅初始化一次。 静态变量也与final关键字一起用于声明常量。

class Student
{
    String name;
    int age;
    static int instituteCode=1101;
}

Here instituteCode is a static variable. Each object of Student class will share instituteCode property.

这里instituteCode是一个静态变量。 Student类的每个对象都将共享InstituteCode属性。

静态变量的其他要点: (Additional points on static variable:)

  • static variable are also known as class variable.

    静态变量也称为类变量。

  • static means to remain constant.

    静态意味着保持恒定。

  • In Java, it means that it will be constant for all the instances created for that class.

    在Java中,这意味着对于为该类创建的所有实例,它都是恒定的。

  • static variable need not be called from object.

    静态变量不需要从对象中调用。

  • It is called by classname.static_variable_name

    它由classname.static_variable_name调用

Note: A static variable can never be defined inside a method i.e it can never be a local variable.

注意:绝对不能在方法内部定义静态变量,即永远不能是局部变量。

Example:

例:

Suppose you make 2 objects of class Student and you change the value of static variable from one object. Now when you print it from other object, it will display the changed value. This is because it was declared static i.e it is constant for every object created.

假设您创建了Student类的2个对象,并且从一个对象更改了静态变量的值。 现在,当您从其他对象打印它时,它将显示更改后的值。 这是因为它被声明为静态的,即对于创建的每个对象它都是恒定的。

package studytonight;

class Student{
    int a;
    static int id = 35;

    void change(){

        System.out.println(id);
    }
}

public class StudyTonight {
    public static void main(String[] args) {

        Student o1 = new Student();
        Student o2 = new Student();

        o1.change();

        Student.id = 1;
        o2.change();
    }
}

35 1

35 1

Java中的局部变量 (Local variables in Java)

Local variables are declared in method, constructor or block. Local variables are initialized when method, constructor or block start and will be destroyed once its end. Local variable reside in stack. Access modifiers are not used for local variable.

局部变量在方法,构造函数或块中声明。 局部变量在方法,构造函数或块开始时进行初始化,并在结束时被销毁。 局部变量驻留在堆栈中。 访问修饰符不用于局部变量。

float getDiscount(int price)
{
 float discount;
 discount=price*(20/100);
 return discount;
}
Here discount is a local variable. 折扣是一个局部变量。

Java中的可变范围 (Variable Scope in Java)

Scope of a variable decides its accessibility throughout the program. As we have seen variables are different types so they have their own scope.

变量的范围决定了它在整个程序中的可访问性。 如我们所见,变量是不同的类型,因此它们具有自己的范围。

Local variable: Scope of local variable is limited to the block in which it is declared. For example, a variables declared inside a function will be accessible only within this function.

局部变量局部变量的范围仅限于声明它的块。 例如,在函数内部声明的变量只能在该函数内访问。

Instance variable: scope of instance variable depends on the access-modifiers (public, private, default). If variable is declared as private then it is accessible within class only.

实例变量实例变量的范围取决于访问修饰符(public,private,default) 。 如果变量声明为私有 ,则只能在类中访问。

If variable is declared as public then it is accessible for all and throughout the application.

如果将变量声明为公共变量,则所有应用程序都可以访问它。

If variable is declared as default the it is accessible with in the same package.

如果将变量声明为默认值 ,则可以在同一包中对其进行访问。

For more details about accessibility you can refer our detailed tutorial. Click here to know more about Variable Scope

有关可访问性的更多详细信息,请参阅我们的详细教程。 单击此处以了解有关可变范围的更多信息

例: (Example:)

In this example, we created two variables a and i, first is declared inside the function and second is declared inside for loop. Both variables have their own scope in which they are declared, so accessing outside the block reports an error.

在此示例中,我们创建了两个变量ai ,第一个变量在函数内部声明,第二个变量在for循环内部声明。 这两个变量都有其声明的作用域,因此在块外部进行访问将报告错误。

public class HelloWorld {

    public static void main(String[] args) {

        int a = 10;
        
        for(int i = 0; i<5; i++) {
            System.out.println(i);
        }
        
        System.out.println("a = "+a);
        System.out.println("i = "+i); // error

    }

}

error: cannot find symbol i

错误:找不到符号我

翻译自: https://www.studytonight.com/java/variable.php

java中的变量

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值