In Java, variables are declared by specifying the variable type followed by the variable name. Initialization, or assigning an initial value to a variable, can be done at the time of declaration or later in the program.
[Variable Type] [Variable Name] = [Initial Value];
- Variable Type: The type of the variable, such as
int
,double
,String
, etc. - Variable Name: The identifier or name given to the variable.
- Initial Value: The initial value assigned to the variable at the time of declaration.
Here’s an example using this schema:
int age = 25;
This line of code declares an integer variable named age
and initializes it with the value 25
.
Identifiers
Identifiers are names given to variables, methods, classes, and other elements in Java. They must adhere to certain rules and conventions.
Rules for Identifiers
- Must begin with a letter, dollar sign
$
, or underscore_
. - Subsequent characters can be letters, digits, dollar signs, or underscores.
- Cannot be a reserved keyword (e.g.,
int
,class
,public
, enum, throws). - Case-sensitive.
Naming Conventions
- Camel Case:
- Starts with a lowercase letter and subsequent words are capitalized.
- Example:
myVariableName
,calculateTotalAmount
.
- Snake Case:
- All lowercase letters with words separated by underscores.
- Example:
my_variable_name
,calculate_total_amount
.
- Pascal Case:
- Starts with a uppercase letter and subsequent words are capitalized.
- Example:
Drawable
,EventListener
,DatabaseConnection
..
Naming conventions play a crucial role in Java programming as they contribute to code readability and maintainability. Adhering to consistent naming practices helps developers understand the purpose and nature of different elements in the code. Here are the common naming conventions for methods, variables, classes, and interfaces in Java:
- variables and methods: camel case
- classes and interfaces: Pascal case
- packages: All lowercase letters (e.g.,
com.example.utilities
,org.company.project.api
) - boolean variables: start with “is” (e.g.,
isActive
) - constants: Uppercase letters with underscores separating words (e.g.,
MAX_VALUE
,DEFAULT_TIMEOUT
)
Declaring Multiple Variables
Declaring multiple variables in Java is a convenient feature that allows you to declare and initialize several variables of the same type in a single line. This can improve code readability and reduce redundancy. Let’s explore the different possibilities and types of multiple variable declarations with examples:
int x = 5, y = 10, z = 15;
/* In this example, three int variables (x, y, and z) are declared and initialized with different values. */
int x, y, z;
x = 5;
y = 10;
z = 15;
/* You can declare multiple variables and initialize them later. This is useful when you want to separate declaration and initialization. */
int x = 5, y, z = 15;
/* You can mix initialized and uninitialized variables. In this example, x and z are initialized, while y is not. */
String name = "John", int age = 25; // Compilation error: Type mismatch
/* In this example, the attempt to declare both String name and int age on the same line will result in a compilation error. */