Key Difference – Variables vs DataLiteralsin Java
计算机程序是执行任务的一组指令。编程时需要存储数据。因此,这些数据存储在内存中。这些保留的存储位置称为变量。这些变量应该具有唯一的名称,因为应轻松识别它们以执行数学或逻辑操作。变量分配了值。有时这些值是固定的,不会改变。这些值称为数据文字。在程序中,如果有一个语句为int value = 5,则“ int”是数据类型。“值”是变量,而“ 5”是数据文字。 This article discusses the difference between variables and data literals inJava。这key difference在变量和数据文字之间Javais that变量是保留的存储位置,用于存储具有符号名称的值,而数据文字是表示编程中固定值的注释。
内容
1.Overview and Key Difference
2.What are Variables in Java
3.Java中的数据文字是什么
4.Similarities Between Variables and Data Literals in Java
5.并排比较 - 变量与java中的数据文字为表格形式
6。概括
What are Variables in Java?
变量是将值存储在内存中的地方。每个内存位置都可以存储特定类型的数据。Java语言支持八种原始数据类型。他们是一个字节,简短,int, long,布尔人,float, double, andchar。这data type byte is 8-bit signed two’s complement integer. It is helpful save space in large arrays because it is 4 times smaller than int. The data type short is 16-bit signed two’s complement integer. It is 2 times smaller than int. The int is a 32-bit signed two’s complement integer. It is the most common data type to store numerical values without decimal points when there is not much concern about memory. The long data type is a 64-bit signed two’s complement integer. It is used to store a wide range of numbers. The float and double are two data types to store numerical values with a decimal point. The float is 32bit and double is 64 bits. The Boolean is used to store true or false. A single character can be stored using char data type. Those are the major primitive data types in Java.
When there is a statement such as an int x; it means that the variable x can hold an integer value. It does not set aside any memory for the variable number. When there is a statement as int x =5; it means that the variable x can hold integer values and it contains value 5. The initialized value can be changed in the program later. The x value can be equalized to some other integer such as 10 later. e.g. x =10;
Each variable has unique names to identify them. They are calledidentifiers。这programmer should follow the rules when giving names for variables. Java is a case-sensitive language. Therefore, the variable name ‘number’ is different from ‘NUMBER’. The variable name can containUnicodeletters and digits. They cannot have spaces. Refer the below program.

图01:带变量的Java程序
根据上述程序,X和Y是具有整数值的变量。总和分配给变量总和。长度和宽度是双变量。乘法存储在区域变量中,该变量被声明为双变量。单个字符可以存储在变量字母中。它包含“ a”。角色放置在单引号中。
Java中的数据文字是什么?
数据文字是源code representation of a fixed value. The values such as 5, 4.3, true do not require any computation. Therefore, they are known as data literals. When there is a statement, double number = 20.5; the ‘double’ is the data type. The ‘number’ is the variable. The 20.5 is the data literal.
这re are various types of literals. They are integer literals, floating point literals, character and细绳文字。整数文字用于初始化整数数据类型的变量,例如字节,短,int和long。浮点文字用于初始化数据类型float和double的变量。浮点字面的结尾是f或f,它是类型的float。如果以D或D结尾,则是双重的。写作D是可选的。字符和字符串文字由Unicode字符组成。字符文字代表一个字符,而字符串文字表示一组字符。字符文字在单个报价中。例如 – ‘B’. The string literals are inside the double quotes. e.g.- “Programming”. Refer the below program.

图02:带有文字的Java程序
According to the above program, the number is a variable. The integer literal in the number variable is 10. The doubleValue variable can hold a double value. The floatValue variable can hold a float. Therefore, 5.4 and 5.4f are floating point literals. The letter variable contains a character ‘B’. It is a character literal. The word variable contains a set of characters. So, it is a string literal.
What is the Similarity Between Variables and Data Literals in Java?
- Both Variables and Data Literals in Java are used in programming.
What is the Difference Between Variables and Data Literals in Java?
变量与数据Literals |
|
变量是保留的存储位置,可存储具有符号名称的值。 | Data literals are source code representations of fixed values. |
Association | |
变量与内存位置关联。 | Data literals are associated with fixed values that are placed inside the variables. |
概括–变量与数据Literalsin Java
变量和数据文字是与编程相关的常见术语。本文讨论了变量和数据文字之间的差异。Java中变量和数据文字之间的区别在于,变量是存储具有符号名称值的保留存储位置,而数据文字是表示编程中固定值的注释。
Reference:
点,教程。“ Java Basic Datatypes。”,Tutorials Point, 8 Jan. 2018.Available here
发表评论