Skip to main content

Java Variables

  

Java Variables

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 TypesDefault ValueSizeExample
byte01 byte128 to 127
short02 bytes-32,768 to 32,767
int04 bytes100, -200
long0L8 bytes100000L
float0.0f4 bytes12.34f
double0.0d8 bytes123.456
char\u00002 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.

String str = "Hello, World!";

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.

int[] numbers = {1, 2, 3};

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.

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.

Comments

Popular posts from this blog

Path To Java Proficiency

I have created a detailed roadmap that takes you from beginner Core Java to advanced Java concepts. I’ve broken it down into 4 phases to help you keep track of your progress. You will learn important concepts and tools during each phase. Java Basics: Tutorials on getting started with Core Java. Advanced Java Concepts: Dive into topics like concurrency, memory management, and JVM internals, web components. Java Frameworks: Popular frameworks like Spring, Hibernate. Java for Web Development: Focus on using Java for building web applications. Java Best Practices: Code optimization, clean code, and design patterns. Phase 1 : Java Fundamental (Beginner Level) 1. Introduction to Java Install Java (JDK) and set up your IDE (e.g., IntelliJ IDEA, Eclipse, VS Code). Hello World Program : Start by writing and running the simplest Java program. Understand the Java Development Environment : Learn how JVM and JRE work . 2. Core Syntax & Basics Constructs Variables and Data Types : int ...