Java Variables and Literals: A Comprehensive Guide
Java Variables and Literals
This guide explores Java variables and literals, illustrated with clear examples for practical understanding.
Java Variables
A variable is a designated memory location that stores data.
Each variable must have a unique identifier to reference the storage area. Learn more about Java identifiers.
Creating Variables in Java
Here’s how to declare and initialize a variable:
int speedLimit = 80;
In this example, speedLimit is an int holding the value 80. The int type restricts the variable to integer values. For deeper insight, visit Java data types.
Variable declaration and initialization can be performed separately:
int speedLimit;
speedLimit = 80;
Note: Java is statically typed; all variables must be declared before use.
Modifying Variable Values
Variables can change during program execution. For instance:
int speedLimit = 80;
...
speedLimit = 90;
Initially, speedLimit is 80; later it updates to 90. However, the data type cannot change within the same scope.
For example, the following is invalid:
int speedLimit = 80;
...
float speedLimit;
Learn more at: Can I change declaration type for a variable in Java?
Naming Rules for Java Variables
Java’s naming conventions enhance code readability and maintainability:
- Case sensitivity: age and AGE are distinct variables.
- Starting character: Variables must begin with a letter, underscore (
_), or dollar sign ($). - No leading digits:
1ageis invalid. - No whitespace:
my ageis invalid. Use camelCase for multi‑word names, e.g., myAge. - Descriptive names: Prefer meaningful identifiers such as score or level over single letters.
- Single‑word names: Use all lowercase letters, e.g., speed instead of SPEED.
Java distinguishes four variable categories:
- Instance (non‑static) fields
- Class (static) fields
- Local variables
- Method parameters
Explore variable types in more depth: Java Variable Types.
Java Literals
Literals are fixed values embedded directly in source code. They provide a convenient way to assign data:
int a = 1;
float b = 2.5;
char c = 'F';
Below are the primary literal categories in Java.
1. Boolean Literals
Boolean literals are limited to true or false:
boolean flag1 = false;
boolean flag2 = true;
2. Integer Literals
Integer literals represent whole numbers and come in four bases:
- Binary (base‑2) – prefix
0b - Decimal (base‑10)
- Octal (base‑8) – prefix
0 - Hexadecimal (base‑16) – prefix
0x
Examples:
// binary
int binaryNumber = 0b10010;
// octal
int octalNumber = 027;
// decimal
int decNumber = 34;
// hexadecimal
int hexNumber = 0x2F;
Note: Integer literals initialize byte, short, int, and long variables.
3. Floating‑point Literals
Floating‑point literals include fractional or exponential notation:
class Main {
public static void main(String[] args) {
double myDouble = 3.4;
float myFloat = 3.4F;
double myDoubleScientific = 3.445e2;
System.out.println(myDouble); // 3.4
System.out.println(myFloat); // 3.4
System.out.println(myDoubleScientific); // 344.5
}
}
These literals initialize float and double variables.
4. Character Literals
Characters are single Unicode symbols enclosed in single quotes:
char letter = 'a';
Escape sequences are also valid, e.g., \b, \t, \n, etc.
5. String Literals
Strings are sequences of characters enclosed in double quotes:
String str1 = "Java Programming";
String str2 = "Programiz";
Java
- Mastering C++ Variables, Literals, and Constants: A Comprehensive Guide
- C Variables, Constants, and Literals: A Complete Guide
- Python Variables, Constants, and Literals – A Comprehensive Guide
- Understanding Java: JDK, JRE, and JVM Explained
- Mastering Java While & Do‑While Loops: Step‑by‑Step Tutorial
- Java Classes and Objects: A Practical Guide
- Java Variables and Data Types – A Comprehensive Guide with Examples
- Understanding Java Variable Types: A Comprehensive Guide
- Understanding C Constants and Literals: Types, Usage, and Best Practices
- Mastering C# Constants & Literals: Types, Rules, and Best Practices