Home > Java > Javagetting Started > The difference between java constants and variables

The difference between java constants and variables

藏色散人
Release: 2020-05-29 11:51:24
Original
3814 people have browsed it

The difference between java constants and variables

The difference between Java constants and variables

1. Constants

Modify with final (also called final variable)

Constants must be assigned an initial value when declared, and the value cannot be modified after assignment

Constant names are usually expressed in all uppercase letters

You need to add the final or static final type modifier when declaring, for example:

private final int PI=3.141596; //常量,类加载时确定或者更靠后确定值
private static final int PI=3.14159;//静态常量(编译期常量),编译时就确定值(编译为class文件)
Copy after login

2. Variables

1. Different variable types, the allocated memory types are also different

2. Default value of uninitialized member variables

3. Automatic arithmetic operation conversion of variables

When performing arithmetic operations on two variables with different data types, the data needs to be processed first Type conversion

The system's automatic type conversion is performed from low to high precision

Automatic data type conversion rules:

The difference between java constants and variables

Data conversion Example

//两个byte型数据相加
public class Add_two_byte{
  public static void main(String args[]){
    byte a = 5;
    byte b = 3;
    //byte c = a+b; 错误,两个操作数都转成int型
    int c = a+b; //正确操作
    System.out.println(a+"+"+b+"="+c);
  }
}
Copy after login

Related recommendations: "java learning"

The above is the detailed content of The difference between java constants and variables. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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