Primitive and Non-Primitive Data Types in Java

Primitive and Non-Primitive Data Types in Java



Data Types in Java (In Depth)

Java has a strong and statically typed system, meaning every variable must have a declared data type. These data types are broadly classified into Primitive Data Types and Non-Primitive (Reference) Data Types.


1. Primitive Data Types

Primitive data types are the most basic types and are predefined by the Java language.

Numeric Data Types

1.1 Integer Data Types (Whole Numbers)

Data Type

Size

Range

Default Value

byte

1 byte (8 bits)

-128 to 127

0

short

2 bytes (16 bits)

-32,768 to 32,767

0

int

4 bytes (32 bits)

-2,147,483,648 to 2,147,483,647

0

long

8 bytes (64 bits)

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

0L

Example:

java

byte smallNumber = 100;

short mediumNumber = 20000;

int normalNumber = 100000;

long bigNumber = 10000000000L; // 'L' suffix is required


1.2 Floating-Point Data Types (Decimal Numbers)

Data Type

Size

Precision

Default Value

float

4 bytes

6-7 decimal digits

0.0f

double

8 bytes

15-16 decimal digits

0.0d

Example:

java

float price = 19.99f; // 'f' suffix is required

double largePrice = 19999.9999; // 'd' suffix is optional


1.3 Character Data Type

Data Type

Size

Description

Default Value

char

2 bytes

Stores a single character (Unicode)

'\u0000' (null character)

Example:

java

char letter = 'A';

char unicodeLetter = '\u0041'; // Unicode for 'A'


1.4 Boolean Data Type

Data Type

Size

Possible Values

Default Value

boolean

1 bit

true, false

false

Example:

java

boolean isJavaFun = true;

boolean isCold = false;


2. Non-Primitive (Reference) Data Types

Non-primitive data types store references to objects rather than actual data.

2.1 String

A String in Java is an object that represents a sequence of characters.

Example:

java

String name = "Java Programming";

System.out.println(name.length()); // Get length of the string

Important Notes on Strings:

  • Strings are immutable (cannot be changed after creation).
  • Stored in a String Pool for memory optimization.

2.2 Arrays

An array is a collection of elements of the same data type.

Example:

java

int[] numbers = {10, 20, 30, 40, 50}; // Integer array

String[] names = {"John", "Jane", "Alice"}; // String array

System.out.println(numbers[1]); // Output: 20


2.3 Classes and Objects

A class is a blueprint for creating objects.

Example:

java

class Car {

    String brand;

    int speed;

 

    void display() {

        System.out.println("Brand: " + brand + ", Speed: " + speed);

    }

}

 

public class Main {

    public static void main(String[] args) {

        Car myCar = new Car();

        myCar.brand = "Tesla";

        myCar.speed = 200;

        myCar.display();

    }

}


2.4 Interface

An interface defines a set of abstract methods that a class must implement.

Example:

java

interface Animal {

    void makeSound();

}

 

class Dog implements Animal {

    public void makeSound() {

        System.out.println("Woof Woof!");

    }

}

 

public class Main {

    public static void main(String[] args) {

        Dog myDog = new Dog();

        myDog.makeSound(); // Output: Woof Woof!

    }

}


2.5 Collections (Dynamic Data Structures)

Java provides built-in collections such as:

  • List (ArrayList, LinkedList)
  • Set (HashSet, TreeSet)
  • Map (HashMap, TreeMap)

Example (ArrayList):

java

import java.util.ArrayList;

 

public class Main {

    public static void main(String[] args) {

        ArrayList<String> list = new ArrayList<>();

        list.add("Java");

        list.add("Python");

        list.add("C++");

 

        System.out.println(list.get(1)); // Output: Python

    }

}


3. Type Conversion & Type Casting

3.1 Implicit Type Conversion (Widening)

Java automatically converts smaller types to larger types.

Example:

java

int num = 10;

double result = num; // Automatically converts int to double

System.out.println(result); // Output: 10.0

3.2 Explicit Type Conversion (Narrowing)

Manually converting a larger type to a smaller type.

Example:

java

double pi = 3.14159;

int intPi = (int) pi; // Explicit cast

System.out.println(intPi); // Output: 3


4. Wrapper Classes

Java provides wrapper classes for primitive types, allowing them to be used as objects.

Primitive Type

Wrapper Class

byte

Byte

short

Short

int

Integer

long

Long

float

Float

double

Double

char

Character

boolean

Boolean

Example (Autoboxing and Unboxing):

java

Integer myInt = 100; // Autoboxing (primitive to object)

int num = myInt; // Unboxing (object to primitive)


Conclusion

Java provides a variety of data types to store different kinds of data efficiently. Primitive data types are used for simple values, while non-primitive types provide object-oriented capabilities. Understanding these data types is crucial for writing optimized Java programs.

Would you like any further explanation on a specific part? 😊🚀

Visit:

learn to grow


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
https://otieu.com/4/9433883
https://otieu.com/4/9433883