Datatypes in Java

Data types in Java categorize variables into either primitive types (int, float) for basic values or reference types (classes, arrays) for more complex structures, facilitating memory management and variable manipulation within programs.

Integer Data Type

The Integer data type is used to store whole numbers without any fractional or decimal part. It is represented by the int keyword in Java.

Example
int age = 25;

In this example, we declare an integer variable named "age" and assign it a value of 25. Integer data types support arithmetic operations and can be used for counting, indexing, and calculations involving whole numbers.

Floating-Point Data Types

Java provides two floating-point data types to represent numbers with a fractional part: float and double.

Float Data Type

Example The float data type is used to represent single-precision floating-point numbers. It is declared using the float keyword.

Example
float weight = 65.5f;

In this example, we declare a float variable named "weight" and assign it a value of 65.5. Note that we append the letter 'f' to the value to indicate that it is a float.

Double Data Type

The double data type is used to represent double-precision floating-point numbers. It provides more precision than the float data type. Double values are declared using the double keyword.

Example
double pi = 3.14159;

In this example, we declare a double variable named "pi" and assign it the value of Pi. Double data types are commonly used when precision is crucial, such as in mathematical calculations or when dealing with large or small numbers.

Character Data Type

The char data type is used to represent a single character. It is declared using the char keyword and enclosed in single quotes ('').

Example
char grade = 'A';

In this example, we declare a char variable named "grade" and assign it the value 'A'. Character data types are commonly used to store individual characters, letters, or symbols.

Boolean Data Type

The boolean data type represents a logical value that can be either true or false. It is declared using the boolean keyword.

Example
boolean isRaining = true;

In this example, we declare a boolean variable named "isRaining" and assign it the value true. Boolean data types are commonly used for conditional statements and decision-making in program logic.

Reference Data Types

Reference data types are used to store references to objects, which are more complex data structures that can store multiple data values. The following are the reference data types in Java:

String Data Type

The String data type in Java represents a sequence of characters. It is a reference data type, even though it is commonly used and behaves like a primitive type. Strings are immutable, meaning their values cannot be changed once assigned.

Example
String message = "Hello, World!";

In this example, we declare a String variable named "message" and assign it the value "Hello, World!". String data types are widely used for storing and manipulating text or character-based data.

Arrays:

Arrays in Java are reference data types used to store multiple values of the same data type. They provide a way to group related data together. Arrays have a fixed length, and each element is accessed using an index starting from 0.

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

In this example, we declare an integer array named "numbers" and initialize it with five elements. Arrays can be used for storing collections of data, implementing algorithms, and processing large amounts of information efficiently.

Classes and Objects:

In Java, we can create our own classes to define custom reference data types. A class serves as a blueprint for creating objects that encapsulate data and behavior.

Example

class Person {
    String name;
    int age;
}
public class Main {
    public static void main(String[] args) {
        Person person = new Person();
        person.name = "Nitu";
        person.age = 25;
    }
}

In this example, we define a class named "Person" with attributes "name" and "age". We then create an object of the Person class named "person" and assign values to its attributes. Custom classes allow us to model real-world entities, create reusable code, and implement complex systems.

Collections Framework

Java's Collections Framework provides a set of interfaces and classes to work with groups of objects. It includes data structures such as lists, sets, queues, and maps. Collections allow us to store, manipulate, and retrieve data efficiently.

Example
import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
        ArrayList<String>names = new ArrayList<>();
        names.add("Vikas");
        names.add("Vicky");
        names.add("Nitu");
    }
}

In this example, we import the ArrayList class from the java.util package. We create an ArrayList named "names" that can store String objects. We add three names to the list using the add() method. Collections provide powerful tools for managing groups of objects and performing various operations on them.