Variables and literals are fundamental concepts in Java programming. Variables store data that can be manipulated during program execution, while literals are the fixed values assigned to these variables. Understanding their usage and types is essential for efficient programming.
Variables in Java
Definition
A variable is a named storage location in memory that holds a value of a specific type. The value of a variable can change during the program execution.
Declaring a Variable
A variable declaration specifies the type and name of the variable:
datatype variableName;
Initializing a Variable
Variables can be initialized at the time of declaration:
datatype variableName = value;
Example
int age = 25; // ‘age’ is an integer variable storing the value 25
Types of Variables
1. Local Variables
- Declared inside a method, constructor, or block.
- Accessible only within the block in which they are declared.
- Must be initialized before use.
- Example:
public class Main {
public static void main(String[] args) {
int localVar = 10; // Local variable
System.out.println(localVar);
}
}
2. Instance Variables
- Declared outside methods but within a class.
- Each object of the class gets its own copy.
- Default initialized to null, 0, or false (depending on type).
- Example:
public class Person {
String name; // Instance variable
public void displayName() {
System.out.println(“Name: ” + name);
}
}
3. Static Variables
- Declared using the static keyword.
- Shared among all objects of the class (class-level variable).
- Example:
public class Employee {
static String company = “Tech Corp”; // Static variable
}
Variable Scope
- Scope refers to the portion of the program where a variable can be accessed.
- Local Scope: Limited to a method or block.
- Instance Scope: Accessible within non-static methods.
- Static Scope: Accessible in static methods and throughout the class.
Data Types and Variables
Java variables are strongly typed, meaning they must be declared with a specific data type.
Primitive Data Types
- Integer types: byte, short, int, long
- Floating-point types: float, double
- Character type: char
- Boolean type: boolean
Example
int number = 10;
double price = 99.99;
char grade = ‘A’;
boolean isAvailable = true;
Reference Data Types
- Includes objects, arrays, and user-defined classes.
- Example:
String name = “Java”;
int[] numbers = {1, 2, 3};
Literals in Java
Definition
Literals are fixed values assigned to variables. They represent constant data in the source code.
Types of Literals
- Integer Literals
- Numbers without a decimal point.
- Example:
int num = 100; // Decimal
int hex = 0x1A; // Hexadecimal
int bin = 0b1010; // Binary
- Floating-point Literals
- Numbers with a decimal point.
- Example:
float pi = 3.14f;
double e = 2.71828;
- Character Literals
- A single character enclosed in single quotes.
- Example:
char letter = ‘A’;
- String Literals
- Sequence of characters enclosed in double quotes.
- Example:
String message = “Hello, Java!”;
- Boolean Literals
- Represents true or false.
- Example:
boolean isActive = true;
- Null Literal
- Represents a null reference.
- Example:
String name = null;
Rules for Naming Variables
- Variable names are case-sensitive.
- Must begin with a letter, $, or _.
- Cannot use reserved keywords.
- No spaces or special characters allowed except $ and _.
Examples:
int _count = 5; // Valid
int $amount = 100; // Valid
int 1number = 10; // Invalid
Example Program: Variables and Literals
public class VariablesLiterals {
// Static variable
static String language = “Java”;
// Instance variable
int year = 2024;
public static void main(String[] args) {
// Local variable
int age = 25;
// Integer literal
int num = 100;
// Floating-point literal
double price = 299.99;
// Character literal
char grade = ‘A’;
// String literal
String message = “Learning Java!”;
// Boolean literal
boolean isJavaFun = true;
// Output
System.out.println(“Age: ” + age);
System.out.println(“Number: ” + num);
System.out.println(“Price: $” + price);
System.out.println(“Grade: ” + grade);
System.out.println(“Message: ” + message);
System.out.println(“Is Java fun? ” + isJavaFun);
System.out.println(“Static variable (language): ” + language);
}
}
Output:
Age: 25
Number: 100
Price: $299.99
Grade: A
Message: Learning Java!
Is Java fun? true
Static variable (language): Java
Summary
Aspect | Variables | Literals |
Definition | Named storage location for data. | Fixed values assigned to variables. |
Types | Local, Instance, Static | Integer, Floating-point, Character, String, Boolean, Null |
Usage | Declared and used to store and manipulate data. | Directly assigned as constant values. |
Examples | int x = 10;, String name = “Java”; | 100, 3.14, ‘A’, “Hello”, true. |
By mastering variables and literals, Java developers can effectively manage data and build dynamic, versatile applications.