![]()
In Java, data types and variables are fundamental concepts that define how data is stored, manipulated, and used in a program. Below is a detailed explanation of data types and variables in Java.
1. Data Types in Java
Data types specify the type of data that a variable can hold. Java is a statically-typed language, meaning the data type of a variable must be declared before it can be used. Java data types are divided into two categories:
a. Primitive Data Types
Primitive data types are predefined by Java and represent basic values. They are not objects and are stored directly in memory. There are 8 primitive data types in Java:
| Data Type | Size (in bytes) | Default Value | Description | Example |
|---|---|---|---|---|
byte | 1 | 0 | Stores whole numbers from -128 to 127 | byte b = 100; |
short | 2 | 0 | Stores whole numbers from -32,768 to 32,767 | short s = 5000; |
int | 4 | 0 | Stores whole numbers from -2^31 to 2^31-1 | int i = 100000; |
long | 8 | 0L | Stores whole numbers from -2^63 to 2^63-1 | long l = 100L; |
float | 4 | 0.0f | Stores fractional numbers (6-7 decimal digits) | float f = 3.14f; |
double | 8 | 0.0d | Stores fractional numbers (15 decimal digits) | double d = 3.14; |
char | 2 | ‘\u0000’ | Stores a single character/letter | char c = 'A'; |
boolean | 1 bit | false | Stores true or false values | boolean b = true; |
b. Non-Primitive Data Types (Reference Types)
Non-primitive data types are created by the programmer and are also called reference types. They refer to objects in memory. Examples include:
| Data Type | Description | Example |
|---|---|---|
String | Stores a sequence of characters | String s = "Hello"; |
Array | Stores a collection of elements of the same type | int[] arr = {1, 2, 3}; |
Class | User-defined data types | class MyClass {} |
Interface | Defines a contract for classes | interface MyInterface {} |
2. Variables in Java
A variable is a container that holds a value of a specific data type. Variables must be declared before they can be used.
Syntax for Declaring a Variable:
data_type variable_name = value;
- data_type: The type of data the variable can hold (e.g.,
int,String). - variable_name: The name of the variable (must follow Java naming conventions).
- value: The initial value assigned to the variable (optional).
Example:
int age = 25; // Declares an integer variable 'age' and assigns it the value 25
String name = "John"; // Declares a String variable 'name' and assigns it the value "John"
Types of Variables in Java
Java supports three types of variables based on their scope and lifetime:
a. Local Variables
- Declared inside a method, constructor, or block.
- Must be initialized before use.
- Scope is limited to the block in which they are declared.
- Example:
java void myMethod() { int x = 10; // Local variable System.out.println(x); }
b. Instance Variables
- Declared inside a class but outside any method.
- Belong to an instance of the class (object).
- Initialized with default values if not explicitly assigned.
- Example:
java class MyClass { int x; // Instance variable }
c. Static Variables
- Declared with the
statickeyword inside a class but outside any method. - Belong to the class rather than any specific instance.
- Shared among all instances of the class.
- Example:
java class MyClass { static int x; // Static variable }
Variable Naming Conventions
- Variable names must start with a letter, underscore (
_), or dollar sign ($). - Cannot start with a number.
- Cannot use Java keywords (e.g.,
int,class). - Use camelCase for variable names (e.g.,
myVariable).
Type Casting in Java
Type casting is the process of converting one data type into another. There are two types of type casting:
a. Widening Casting (Implicit)
- Converts a smaller data type to a larger one (e.g.,
inttodouble). - Done automatically by the compiler.
- Example:
java int x = 10; double y = x; // Widening casting (int to double)
b. Narrowing Casting (Explicit)
- Converts a larger data type to a smaller one (e.g.,
doubletoint). - Requires explicit casting using parentheses.
- Example:
java double x = 10.5; int y = (int) x; // Narrowing casting (double to int)
Example Program
public class Main {
static int staticVar = 10; // Static variable
int instanceVar = 20; // Instance variable
public static void main(String[] args) {
int localVar = 30; // Local variable
System.out.println("Static Variable: " + staticVar);
System.out.println("Local Variable: " + localVar);
Main obj = new Main();
System.out.println("Instance Variable: " + obj.instanceVar);
}
}
| Concept | Description |
|---|---|
| Primitive Data Types | Basic data types like int, double, char, etc. |
| Non-Primitive Types | Reference types like String, arrays, classes, and interfaces. |
| Variables | Containers for storing data. |
| Local Variables | Declared inside methods/blocks; scope is limited to the block. |
| Instance Variables | Declared inside a class; belong to an object. |
| Static Variables | Declared with static; shared among all instances of a class. |
| Type Casting | Converting one data type to another (widening or narrowing). |
Understanding data types and variables is essential for writing efficient and effective Java programs.
