The difference between Java quantity and variable

Source: Internet
Author: User

constant: A constant whose value is unchanged.
Syntax:
data type constant name = value;
double PI = 3.14;
remark:
The general default constant name is uppercase.
relationship between variables and constants (inter-volume relationship)
Let 's start with a simple example to understand the relationship between variables and constants in Java.
The following program declares two variables commonly used by Java, the integer variable num and the character variable ch. After assigning them values, display their values separately on the console:
///The following program declares two variables, one is integer and one is character type
Public class testjava{
Public static void Main (String args[]) {
int num = 3;//Declare an integer variable num with a value of 3
char ch = ' Z ';//declare a character variable ch, assign Z
System.out.println (num+ "is an integer!) "); The value of the output num
System.out.println (ch + "is a character! "); Value of Output ch
}
}
-----------------------------------------------------------------
Output Result:
3 is an integer!
Z is the character!
Description:
two different types of variables, num and CH, are declared, and the constants 3 and the character "Z" are assigned to both variables, and they are then displayed on the monitor. When declaring a variable, the compiler creates a memory space in memory that is sufficient to accommodate the variable. Regardless of how the value of the variable changes, the same memory space is always used. Therefore, the use of variables will be a memory-saving way.
A constant is a type that differs from a variable, whose value is fixed, such as an integer constant, a string constant, and so on. When assigning a value to a variable, the constant is assigned to it, and in program Testjava, line 6th num is the integer variable, and 3 is the constant. The purpose of this line is to declare num as an integer variable and assign the value of the constant 3 to it.
The same, Line 7th declares a character variable ch and assigns the character constant ' z ' to it. Of course, in the process of the program, you can re-assign a value to a variable, or you can use a variable that has already been declared.

technology sharing: www.kaige123.com

The difference between Java quantity and variable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.