What is a Variable?
A variable is a named memory location used to store data in a program.
Syntax:
dataType variableName = value ;
Enter fullscreen mode
Exit fullscreen mode
Example:
int age = 25 ; String name = "John" ; double salary = 25000.50 ;
Enter fullscreen mode
Exit fullscreen mode
Rules for Creating Variables
Names can contain letters, digits, _ and $ .
A variable name cannot start with a digit.
Spaces are not allowed.
Java keywords cannot be used as variable names.
Variable names are case-sensitive ( age and Age are different).
Use meaningful names and follow camelCase.
Types of Variables in Java
Java has three types of variables:
Local Variables
Instance Variables (Non-static)
Static Variables
1. Local Variables
Declared inside a method, constructor, or block.
Accessible only within that method or block.
Must be initialized before use.
Do not receive default values.
void display () { int age = 25 ; System . out .β¦