JAVA Getting Started Tutorial | Chapter 3 Variable Types
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:
The variable name of the basic type is the variable itself.
#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)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
