Home >Java >javaTutorial >JAVA Getting Started Tutorial | Chapter 3 Variable Types

JAVA Getting Started Tutorial | Chapter 3 Variable Types

黄舟
黄舟Original
2017-02-25 09:48:471127browse

We can know from the previous article that data types in java are divided into basic data types and reference data types .

#The object-oriented principle of Java is: data and operations on data must be bound together. This is a class, that is, a reference data type. Therefore, a class is also a type. Java does not need to set basic types. It only sets basic types to improve operating efficiency. The main difference between basic types and reference types is:

  1. The variable name of the basic type is the variable itself.

  2. #The name of a reference type variable is the storage location of complex data.

We know that there are three major categories of variable types supported by the Java language:

  • Local variables

  • Member variables

  • Class variables (Static variable)

This chapter actually starts from the scope and explains the scope of the variable type. Why is it called a scope? Let’s first look at the code and concepts below.


Java local variables

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

  • Local variables are created when methods, constructors, or statement blocks are executed, and when their execution is completed After that, the variable will be destroyed;

  • Access modifiers cannot be used for local variables;

  • Local variables are only declared in the method, Visible in the constructor or statement block;

  • Local variables are allocated on the stack.

  • Local variables have no default value, so after a 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 pupAge() method, and its scope is limited to this method.

package com.dujinyang.immqy;
 
public class Test{ 
   public void getAge(){
      int age=1;
      age = age + 9;
      System.out.println("--小狗的年龄 : " + age);
   }
   
   public static void main(String args[]){
      Test test = new Test();
      test.getAge();
   }
}

The above example compilation and running results are as follows:

--小狗的年龄是:10

Instance 2

In the following example, the age variable is not initialized, so an error will occur during compilation:

package com.dujinyang.immqy;
 
public class Test{ 
   public void getAge(){
      int age;
      age = age + 9;
      System.out.println("--小狗的年龄 : " + age);
   }
   
   public static void main(String args[]){
      Test test = new Test();
      test.getAge();
   }
}

The compiler will report an error directly:

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

实例变量

  • 实例变量声明在一个类中,但在方法、构造方法和语句块之外;

  • 当一个对象被实例化之后,每个实例变量的值就跟着确定;

  • 实例变量在对象创建的时候创建,在对象被销毁的时候销毁;

  • 实例变量的值应该至少被一个方法、构造方法或者语句块引用,使得外部能够通过这些方式获取实例变量信息;

  • 实例变量可以声明在使用前或者使用后;

  • 访问修饰符可以修饰实例变量;

  • 实例变量对于类中的方法、构造方法或者语句块是可见的。一般情况下应该把实例变量设为私有。通过使用访问修饰符可以使实例变量对子类可见;

  • 实例变量具有默认值。数值型变量的默认值是0,布尔型变量的默认值是false,引用类型变量的默认值是null。变量的值可以在声明时指定,也可以在构造方法中指定;

  • 实例变量可以直接通过变量名访问。但在静态方法以及其他类中,就应该使用完全限定名:ObejectReference.VariableName。

实例

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 );
      System.out.println("薪水 : " + salary);
   }
 
   public static void main(String args[]){
      Employee empOne = new Employee("KARL-dujinyang");
      empOne.setSalary(1000);
      empOne.printEmp();
   }
}

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

$ javac Employee.java 
$ java Employee名字 : KARL-dujinyang薪水 : 1000.0

类变量(静态变量)

  • 类变量也称为静态变量,在类中以static关键字声明,但必须在方法构造方法和语句块之外。

  • 无论一个类创建了多少个对象,类只拥有类变量的一份拷贝。

  • 静态变量除了被声明为常量外很少使用。常量是指声明为public/private,final和static类型的变量。常量初始化后不可改变。

  • 静态变量储存在静态存储区。经常被声明为常量,很少单独使用static声明变量。

  • 静态变量在程序开始时创建,在程序结束时销毁。

  • 与实例变量具有相似的可见性。但为了对类的使用者可见,大多数静态变量声明为public类型。

  • 默认值和实例变量相似。数值型变量默认值是0,布尔型默认值是false,引用类型默认值是null。变量的值可以在声明的时候指定,也可以在构造方法中指定。此外,静态变量还可以在静态语句块中初始化。

  • 静态变量可以通过:ClassName.VariableName的方式访问。

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

实例

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

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

深圳的平均工资:1000.0

注意:如果其他类想要访问该变量,可以这样访问:Employee.DEPARTMENT。因为它是静态的,static的关键字。

In this chapter we learned about Java variable types. In the next chapter we will introduce the use of Java modifiers.

The above is the content of JAVA Getting Started Tutorial | Chapter 3 Variable Types. For more related content, please pay attention to the PHP Chinese website (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