Java Part 2: Basic Data Types and Variables in Java

Java Part 2: Understanding Basic Data Types And Variables In Java

Please Subscribe Youtube| Like Facebook | Follow Twitter


Java is a popular programming language that is known for its strong-typing. In Java, every variable and expression has a specific data type that defines the kind of values it can hold. Java supports several built-in data types that can be used to define variables. In this article, we’ll discuss the basic data types and variables in Java.

Primitive Data Types:

Java has eight primitive data types, which are divided into four categories:

  1. Integer Types:
    • byte: 8-bit signed two’s complement integer.
    • short: 16-bit signed two’s complement integer.
    • int: 32-bit signed two’s complement integer.
    • long: 64-bit signed two’s complement integer.
  2. Floating-Point Types:
    • float: 32-bit IEEE 754 floating-point number.
    • double: 64-bit IEEE 754 floating-point number.
  3. Character Type:
    • char: 16-bit Unicode character.
  4. Boolean Type:
    • boolean: represents one of two values: true or false.


Variables are used to store values in Java, and are defined with a specific data type. To define a variable, you need to specify the data type, followed by the variable name. Here’s an example:

int age = 25;

In the above example, we have defined a variable called “age” of type “int”, and assigned it a value of 25.

You can also declare multiple variables of the same type on the same line, like this:

int age = 25, height = 175;

It’s important to note that when you define a variable, you are only reserving memory space for it. To assign a value to the variable, you need to use the assignment operator (=).

Example Code

Here’s an example code that demonstrates the use of basic data types and variables in Java:

public class Main {
  public static void main(String[] args) {
    // Integer data type
    int a = 42;
    System.out.println("Value of a: " + a);
    System.out.println("Data type of a: " + Integer.TYPE);

    // Float data type
    float b = 3.14f;
    System.out.println("Value of b: " + b);
    System.out.println("Data type of b: " + Float.TYPE);

    // Double data type
    double c = 2.71828;
    System.out.println("Value of c: " + c);
    System.out.println("Data type of c: " + Double.TYPE);

    // Boolean data type
    boolean d = true;
    System.out.println("Value of d: " + d);
    System.out.println("Data type of d: " + Boolean.TYPE);

    // Character data type
    char e = 'A';
    System.out.println("Value of e: " + e);
    System.out.println("Data type of e: " + Character.TYPE);

    // Array data type
    int[] f = {1, 2, 3, 4, 5};
    System.out.print("Value of f: [");
    for (int i = 0; i < f.length; i++) {
      if (i != f.length - 1) {
        System.out.print(", ");
    System.out.println("Data type of f: " + f.getClass().getComponentType());

    // String data type
    String g = "Hello, World!";
    System.out.println("Value of g: " + g);
    System.out.println("Data type of g: " + g.getClass().getSimpleName());

    // Object data type
    Person h = new Person("John", 30);
    System.out.println("Value of h: " + h);
    System.out.println("Data type of h: " + h.getClass().getSimpleName());

class Person {
  private String name;
  private int age;

  public Person(String name, int age) { = name;
    this.age = age;

  public String toString() {
    return "Person [name=" + name + ", age=" + age + "]";


Value of a: 42
Data type of a: int
Value of b: 3.14
Data type of b: float
Value of c: 2.71828
Data type of c: double
Value of d: true
Data type of d: boolean
Value of e: A
Data type of e: char
Value of f: [1, 2, 3, 4, 5]
Data type of f: int
Value of g: Hello, World!
Data type of g: String
Value of h: Person [name=John, age

In the above code, we have declared and initialized variables of different data types, and then printed out their values and types using the System.out.println() method.

Java Beginner Tutorial Series

Please Subscribe Youtube| Like Facebook | Follow Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *