Home > Java > javaTutorial > What should you pay attention to when using variables in java

What should you pay attention to when using variables in java

WBOY
Release: 2023-05-13 16:13:06
forward
1731 people have browsed it

Explanation

1. Each variable has a type. The type can be a basic type or a reference type.

2. The variable name must be a legal identifier.

3. A variable declaration is a complete statement, so each declaration must end with a semicolon.

Example

public class demo04 {
    //类变量
    static double salary =2500;
    // 属性   变量
    //实例变量:从属于对象;如果不进行初始化,这个类型的默认值为0,0.0。
    //布尔值:默认值为false
    //除了基本类型,其余的默认值都是null;
    String name;
    int age;
    //main方法
    public static void main(String[] args) {
        //局部变量:必须声明和初始化值;
        int i = 10;
        System.out.println(i);
        //使用实例变量
        //变量类型  变量名字 = new Demo04();
        demo04 demo04 = new demo04();  //alt+enter
        System.out.println(demo04.age);
        System.out.println(demo04.name);
        System.out.println(salary);
    }
    
    //其他方法
    public void add(){
        System.out.println(1);
    }
}
Copy after login

The above is the detailed content of What should you pay attention to when using variables in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template