Home >Java >javaTutorial >[java tutorial] Java variable types

[java tutorial] Java variable types

黄舟
黄舟Original
2016-12-26 11:26:321408browse

Java variable types

In the Java language, all variables must be declared before use. The basic format for declaring variables is as follows:

type identifier [ = value][, identifier [= value] ...] ;

Format description: type is a Java data type. identifier is the variable name. Multiple variables of the same type can be declared separated by commas.

The following lists some examples of variable declarations. Note that some include an initialization process.

int a, b, c;         // 声明三个int型整数:a、 b、c。
int d = 3, e, f = 5; // d声明三个整数并赋予初值。
byte z = 22;         // 声明并初始化z。
double pi = 3.14159; // 声明了pi。
char x = 'x';        // 变量x的值是字符'x'。

The variable types supported by the Java language are:

Local variables

Member variables

Class variables

Java local variables

Local variables are declared in methods, constructors or statement blocks;

Local variables are declared in methods, Constructors or statement blocks are created when they are executed. When they are executed, the variables will be destroyed;

Access modifiers cannot be used for local variables;

Local variables are only used in the method, constructor or statement in which they are declared. Visible in the block;

Local variables are allocated on the stack.

Local variables have no default value, so after the local variable is declared, it must be initialized before it can be used.

Example 1

In the following example, age is a local variable. It is defined in the pubAge() method, and its scope is limited to this method.

public class Test{ 
   public void pupAge(){
      int age = 0;
      age = age + 7;
      System.out.println("Puppy age is : " + age);
   }
   
   public static void main(String args[]){
      Test test = new Test();
      test.pupAge();
   }
}

The compilation and running results of the above example are as follows:

Puppy age is: 7

Example 2

In the following example, the age variable is not initialized, so an error occurs during compilation.

public class Test{ 
   public void pupAge(){
      int age;
      age = age + 7;
      System.out.println("Puppy age is : " + age);
   }
   
   public static void main(String args[]){
      Test test = new Test();
      test.pupAge();
   }
}

The compilation and running results of the above example are as follows:

Test.java:4:variable number might not have been initialized
age = age + 7;
         ^
1 error

Instance variables

Instance variables are declared in a class, but outside methods, constructors and statement blocks;

When an object is instantiated, each instance The value of the variable is then determined;

Instance variables are created when the object is created and destroyed when the object is destroyed;

The value of the instance variable should be referenced by at least one method, constructor or statement block, so that the outside can pass these Method to obtain instance variable information;

Instance variables can be declared before or after use;

Access modifiers can modify instance variables;

Instance variables are visible to methods, constructors or statement blocks in the class. In general, instance variables should be made private. Instance variables can be made visible to subclasses by using access modifiers;

Instance variables have default values. The default value of numeric variables is 0, the default value of Boolean variables is false, and the default value of reference type variables is null. The value of a variable can be specified at the time of declaration or in the constructor;

Instance variables can be accessed directly through the variable name. But in static methods and other classes, you should use the fully qualified name: ObjectReference.VariableName.

Example:

import java.io.*;
public class Employee{
   // 这个成员变量对子类可见
   public String name;
   // 私有变量,仅在该类可见
   private double salary;
   //在构造器中对name赋值
   public Employee (String empName){
      name = empName;
   }
   //设定salary的值
   public void setSalary(double empSal){
      salary = empSal;
   }  
   // 打印信息
   public void printEmp(){
      System.out.println("name  : " + name );
      System.out.println("salary :" + salary);
   }

   public static void main(String args[]){
      Employee empOne = new Employee("Ransika");
      empOne.setSalary(1000);
      empOne.printEmp();
   }
}

The compilation and running results of the above example are as follows:

name  : Ransika
salary :1000.0

Class variables (static variables)

Class variables are also called static variables. They are declared with the static keyword in the class, but must be used in method constructors and statements outside the block.

No matter how many objects a class creates, the class only has one copy of the class variables.

Static variables are rarely used except when declared as constants. Constants refer to variables declared as public/private, final and static types. Constants cannot be changed after initialization.

Static variables are stored in static storage area. Often declared as constants, variables are rarely declared using static alone.

Static variables are created at the beginning of the program and destroyed at the end of the program.

Have similar visibility to instance variables. But in order to be visible to users of the class, most static variables are declared as public types.

Default values ​​are similar to instance variables. The default value of numeric variables is 0, the default value of Boolean variables is false, and the default value of reference types is null. The value of a variable can be specified when declaring it or in the constructor. In addition, static variables can also be initialized in static statement blocks.

Static variables can be accessed through: ClassName.VariableName.

类变量被声明为public static final类型时,类变量名称必须使用大写字母。如果静态变量不是public和final类型,其命名方式与实例变量以及局部变量的命名方式一致。

实例:

import java.io.*;
public class Employee{
   //salary是静态的私有变量
   private static double salary;
   // DEPARTMENT是一个常量
   public static final String DEPARTMENT = "Development ";
   public static void main(String args[]){
      salary = 1000;
      System.out.println(DEPARTMENT+"average salary:"+salary);
   }
}

以上实例编译运行结果如下:

Development average salary:1000

注意:如果其他类想要访问该变量,可以这样访问:Employee.DEPARTMENT。

本章节中我们学习了Java的变量类型,下一章节中我们将介绍Java修饰符的使用。

 以上就是【java教程】Java变量类型的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn