Day 3: Data Types

Data Type data type in programming defines the type of data a variable can store. It helps the computer understand how much memory to allocate and what operations can be performed on the data. For example, in Java: int → Stores whole numbers (e.g., 10, -5) double → Stores decimal numbers (e.g., 3.14, -0.99) char → Stores a single character (e.g., 'A', 'b') boolean → Stores true or false values Why are data types important? Memory efficiency – Prevents unnecessary memory usage. Data integrity – Ensures only valid operations are performed. Code clarity – Makes the program easier to understand. Error prevention – Avoids accidental data mismatches. Primitive Data Types in Java In Java, primitive data types are the basic building blocks of data manipulation. They are predefined by the language and store simple values directly in memory. Types of Primitive Data Types Java has 8 primitive data types, which store simple values directly in memory. 1.byte – Stores small whole numbers from -128 to 127. Uses 1 byte of memory. Example: byte age = 25; 2.short– Stores larger whole numbers from -32,768 to 32,767. Uses 2 bytes of memory. Example: short year = 2025; 3.int – The most commonly used type for whole numbers, ranging from -2,147,483,648 to 2,147,483,647. Uses 4 bytes. Example: int salary = 50000; 4.long – Stores very large whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Uses 8 bytes. Requires L at the end of the number. Example: long distance = 12345678910L; 5.float – Stores decimal numbers with less precision. Uses 4 bytes. Requires f at the end. Example: float pi = 3.14f; 6.double – Stores decimal numbers with higher precision. Uses 8 bytes. Example: double price = 99.99; 7.char – Stores a single character using Unicode (e.g., 'A', '1', '$'). Uses 2 bytes. Example: char letter = 'A'; 8.boolean – Stores true or false. Size depends on the JVM, but usually 1 byte. Example: boolean isJavaFun = true; Non-Primitive Data Types in Java Non-primitive data types are more complex than primitive data types. They do not store the actual value directly in memory but instead store a reference (address) to the value. Types of Non-Primitive Data Types 1.Strings – A sequence of characters. Example: String name = "Sugumar"; System.out.println(name); 2.Arrays – A collection of elements of the same type. Example: int[] numbers = {10, 20, 30, 40}; System.out.println(numbers[0]); // Output: 10 3.Classes – User-defined blueprints for creating objects. Example: class Person { String name = "Sugumar"; } public class Main { public static void main(String[] args) { Person p = new Person(); System.out.println(p.name); } } 4.Objects – Instances of a class that contain both data and methods. Example: class Car { void display() { System.out.println("This is a car."); } } public class Main { public static void main(String[] args) { Car myCar = new Car(); myCar.display(); } } 5.Interfaces – A blueprint for classes that defines methods without implementations. Example: interface Animal { void makeSound(); } class Dog implements Animal { public void makeSound() { System.out.println("Bark!"); } } public class Main { public static void main(String[] args) { Dog d = new Dog(); d.makeSound(); } } 6.Collections (ArrayList, HashMap, etc.) – Used to store multiple objects dynamically. Example using ArrayList: import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("Java"); list.add("Python"); System.out.println(list); } } Java is not considered fully object-oriented. Java is an Object-Oriented Programming (OOP) language, but it also supports primitive data types like int, char, and boolean, which are not objects. This is why Java is not considered fully object-oriented. Why is Java not 100% Object-Oriented? 1. Primitive Data Types: $ Java includes primitive types (int, char, boolean, etc.) that are not objects. $ This improves performance because objects use more memory than primitives. 2. Static Methods: $ Java allows static methods (Math.sqrt(), Integer.parseInt(), etc.), which do not require objects. 3. Direct Memory Access: $ Java allows direct memory allocation using primitives, which is not purely OOP. What Makes Java an Object-Oriented Language? Java follows OOP principles: ✔ Encapsulation – Data hiding using private variables and getters/setters. ✔ Inheritance – One class can inherit properties from another. ✔ Polymorphism – The same method can behave differently in different classes. ✔ Abstraction – Hiding implementation details using abstract classes and interfaces.

Mar 28, 2025 - 06:29
 0
Day 3: Data Types

Data Type

data type in programming defines the type of data a variable can store. It helps the computer understand how much memory to allocate and what operations can be performed on the data.

For example, in Java:

int → Stores whole numbers (e.g., 10, -5)

double → Stores decimal numbers (e.g., 3.14, -0.99)

char → Stores a single character (e.g., 'A', 'b')

boolean → Stores true or false values

Why are data types important?

  1. Memory efficiency – Prevents unnecessary memory usage.

  2. Data integrity – Ensures only valid operations are performed.

  3. Code clarity – Makes the program easier to understand.

  4. Error prevention – Avoids accidental data mismatches.

Primitive Data Types in Java

In Java, primitive data types are the basic building blocks of data manipulation. They are predefined by the language and store simple values directly in memory.

Types of Primitive Data Types

Java has 8 primitive data types, which store simple values directly in memory.

1.byte – Stores small whole numbers from -128 to 127. Uses 1 byte of memory.

Example: byte age = 25;

2.short– Stores larger whole numbers from -32,768 to 32,767. Uses 2 bytes of memory.

Example: short year = 2025;

3.int – The most commonly used type for whole numbers, ranging from -2,147,483,648 to 2,147,483,647. Uses 4 bytes.

Example: int salary = 50000;

4.long – Stores very large whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Uses 8 bytes. Requires L at the end of the number.

Example: long distance = 12345678910L;

5.float – Stores decimal numbers with less precision. Uses 4 bytes. Requires f at the end.

Example: float pi = 3.14f;

6.double – Stores decimal numbers with higher precision. Uses 8 bytes.

Example: double price = 99.99;

7.char – Stores a single character using Unicode (e.g., 'A', '1', '$'). Uses 2 bytes.

Example: char letter = 'A';

8.boolean – Stores true or false. Size depends on the JVM, but usually 1 byte.

Example: boolean isJavaFun = true;

Non-Primitive Data Types in Java

Non-primitive data types are more complex than primitive data types. They do not store the actual value directly in memory but instead store a reference (address) to the value.

Types of Non-Primitive Data Types

1.Strings – A sequence of characters.

Example:

String name = "Sugumar";
System.out.println(name);

2.Arrays – A collection of elements of the same type.

Example:


int[] numbers = {10, 20, 30, 40};
System.out.println(numbers[0]);  // Output: 10

3.Classes – User-defined blueprints for creating objects.

Example:

class Person {
    String name = "Sugumar";
}

public class Main {
    public static void main(String[] args) {
        Person p = new Person();
        System.out.println(p.name);
    }
}

4.Objects – Instances of a class that contain both data and methods.

Example:

class Car {
    void display() {
        System.out.println("This is a car.");
    }
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.display();
    }
}

5.Interfaces – A blueprint for classes that defines methods without implementations.

Example:

interface Animal {
    void makeSound();
}

class Dog implements Animal {
    public void makeSound() {
        System.out.println("Bark!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.makeSound();
    }
}

6.Collections (ArrayList, HashMap, etc.) – Used to store multiple objects dynamically.

Example using ArrayList:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList list = new ArrayList<>();
        list.add("Java");
        list.add("Python");
        System.out.println(list);
    }
}

Java is not considered fully object-oriented.

Java is an Object-Oriented Programming (OOP) language, but it also supports primitive data types like int, char, and boolean, which are not objects. This is why Java is not considered fully object-oriented.

Why is Java not 100% Object-Oriented?

1. Primitive Data Types:

$ Java includes primitive types (int, char, boolean, etc.) that are not objects.

$ This improves performance because objects use more memory than primitives.

2. Static Methods:

$ Java allows static methods (Math.sqrt(), Integer.parseInt(), etc.), which do not require objects.

3. Direct Memory Access:

$ Java allows direct memory allocation using primitives, which is not purely OOP.

What Makes Java an Object-Oriented Language?

Java follows OOP principles:
✔ Encapsulation – Data hiding using private variables and getters/setters.
✔ Inheritance – One class can inherit properties from another.
✔ Polymorphism – The same method can behave differently in different classes.
✔ Abstraction – Hiding implementation details using abstract classes and interfaces.