Java Variables
A variable in Java is a container that holds data during program execution. Each variable has a name, data type, and value.
1. Declaration and Initialization
Before a variable can be used, it must be declared, which involves specifying its data type and a unique name.
int age = 25; // declaration + initialization
String name = "Test";
double salary; // only declaration
salary = 500.50; // initialization later
2. Types of Variables
Local Variable → Variables declared inside a method/block/constructor.
Instance Variable → Variables declared inside a class but outside methods (object-specific).
Static Variable → Variables declared with
static
, shared across all objects.
class Employee {
String name; // instance variable
static String company = "XYZ Ltd"; // static variable
void work() {
int hours = 8; // local variable
System.out.println(name + " works " + hours + " hours at " + company);
}
}
3. Java Data Types
There are two types of data types in java.
1. Primitive Data Types (8 types)
These are the basic building blocks that store simple values directly in memory.
Data Types | Default Value | Size | Example |
---|---|---|---|
byte | 0 | 1 byte | 128 to 127 |
short | 0 | 2 bytes | -32,768 to 32,767 |
int | 0 | 4 bytes | 100, -200 |
long | 0L | 8 bytes | 100000L |
float | 0.0f | 4 bytes | 12.34f |
double | 0.0d | 8 bytes | 123.456 |
char | \u0000 | 2 bytes | 'A', '9' |
Examples of primitive data types are.
int a = 10;
float b = 5.75f;
char grade = 'A';
boolean javainsight= true;
2. Non-Primitive Data Types (Object Types):
Non-primitive data types in Java, also called reference types, are used to store references (or memory addresses) to objects or data structures. Non-primitive types are more flexible and can store more complex data. They are defined by the programmer and can hold multiple values, often providing functionality that goes beyond simple data storage.
1. String
Size: The size of a string is variable and depends on the number of characters it contains.Range: A string can hold any sequence of characters, including letters, digits, symbols, and even whitespace.
Usage: Strings are used to store textual data such as names, sentences, user inputs, or file paths.
2. Arrays
Size: Arrays in Java have a fixed size once created, but the elements inside the array can be of any type (primitive or reference).
Range: Arrays can store multiple values of the same data type in a contiguous memory location. The elements can be of any primitive or reference type.
Usage: Arrays are used when you need to store a collection of values that are logically related and are of the same type. They are commonly used in data processing, manipulation of lists, and managing collections of similar objects.
Range: Arrays can store multiple values of the same data type in a contiguous memory location. The elements can be of any primitive or reference type.
Usage: Arrays are used when you need to store a collection of values that are logically related and are of the same type. They are commonly used in data processing, manipulation of lists, and managing collections of similar objects.
3. Classes
Size: The size of a class depends on the number and type of fields (variables) and methods (functions) it contains. Objects of the class will have a size determined by the data held within them.
Range: A class can represent any real-world entity and can have properties (fields) and behaviors (methods). Classes in Java are the foundation of object-oriented programming (OOP).
Usage: Classes are used to model real-world objects like “Person”, or “Product”. They encapsulate both data and behavior, allowing for easy manipulation and management of complex data.
Range: A class can represent any real-world entity and can have properties (fields) and behaviors (methods). Classes in Java are the foundation of object-oriented programming (OOP).
Usage: Classes are used to model real-world objects like “Person”, or “Product”. They encapsulate both data and behavior, allowing for easy manipulation and management of complex data.
4. Interfaces
Size: Interfaces themselves don’t hold data; they define a set of abstract methods (without implementation) that the implementing class must provide. They don’t have a size per se but play a crucial role in defining the structure of classes.
Range: An interface is a contract that a class must follow, specifying methods that must be implemented by any class that implements the interface. Interfaces enable multiple classes to share common behaviors while allowing them to have different implementations.
Usage: Interfaces are used to define common functionality that can be implemented by multiple classes, enabling polymorphism and loose coupling in object-oriented design. They are essential for implementing abstraction and defining the API of a class without dictating how it performs its tasks.
Range: An interface is a contract that a class must follow, specifying methods that must be implemented by any class that implements the interface. Interfaces enable multiple classes to share common behaviors while allowing them to have different implementations.
Usage: Interfaces are used to define common functionality that can be implemented by multiple classes, enabling polymorphism and loose coupling in object-oriented design. They are essential for implementing abstraction and defining the API of a class without dictating how it performs its tasks.
Comments
Post a Comment