JAVA - Exploring Data Types & Variables

·

4 min read

Data Types

The data types allocate different sizes and values that can be stored in the variable. In Java, data types are broadly classified into two major categories :

  1. Primitive Data Types - These are known as built-in data types and are defined by the language itself. The primitive data types are as follows :

    • byte - An 8-bit integer.

    • short - A 16-bit integer.

    • int - An 32-bit integer. This is the most commonly used integer data type.

    • long - An 64-bit integer.

    • float - An 32-bit floating-point number.

    • double - A 64-bit floating-point number. More accurate than float.

    • char - A single 16-bit Unicode character.

    • boolean - Gives two values, 'true' or 'false'.

Below are the basic examples of primitive data types in Java :

    byte age = 22; //byte
    short year = 2023; //short
    int salary = 1000000; //int
    long distanceBetweenCities = 64437640192976563L; //long
    float piValue = 3.14; //float
    double piPrecise = 3.141592653589793; // double
    char initial = 'R'; //char
    boolean isJavsSimple = false; //boolean
  1. User-defined/ Reference Data Types - These are references to memory locations where data is stored rather than data itself. They usually form complex data structures such as classes and interfaces. Here are a few examples :

    • Objects - User-defined classes.

    • Arrays - Data structures that store multiple values of the same type.

    • String - It is a class and creates a reference to an object of the String class.

The examples of User-defined will be later discussed with syntax in an upcoming blog.

Real-life Examples

  1. byte, short, int, long - These are different sizes of medium to store water.

    • byte - A small glass

    • short - A water bottle

    • int - A bucket

    • long - A big water tank

Just like you do choose the medium based on the amount of water you have, you can choose these data types based on the size of the number you are dealing with.

  1. float, double - Imagine you are cooking, and you need to measure ingredients.

    • float - A basic tablespoon that gives the measurement to one decimal point.

    • double - A precise tablespoon that measures up to many decimal points.

If you are making a simple dish, the basic scale is enough and for a complicated dish, you might want the precise scale.

  1. char - It is an individual alphabet block that has one letter or character on it.

  2. boolean - A fan switch. It can either ON (true) or OFF (false).

Variables

A variable is a name given to a memory location that stores data. Every variable has a data type, which determines the size and type of value it can hold. There are three main types of variables in Java:

  1. Local Variables -

    • Declared inside a method.

    • They are usually created when the method is called.

    • Initialize them before we use them.

    public void myClass() {
        int localVariable = 99; // This is a local variable
        System.out.println(localVariable);
    }
  1. Non-static Variables (Instance Variables) -

    • Declared outside a method but within a class.

    • They have default values( '0' for numeric types, 'null' for objects, and 'true' for boolean).

    public class Myclass {
        int instanceVariable;  //This is a Non static Variable

        public void showValue() {
            System.out.println(instanceVariable); //print default value o
        }
    }
  1. Static Variables (Class Variables) -
  • They are declared within a class, outside any method with the 'static' keyword.

  • They have default values like instance variables.

public class Myclass {
    static int staticVariable;  // This is a static Variable

    public void showValue() {
        System.out.println(staticVariable);  // print default value o

Variable Naming

  • Variables are case-sensitive.

  • They should start with a lowercase letter and follow the camelCase convention.

  • Special characters are not allowed except an underscore ('_') or a dollar sign ('$').

Real-life Examples

Variables in Java are like boxes where you store things:

  1. Local Variables: Small boxes used for short tasks. When the task is done, you empty the box. Think of using a box to hold toys for a bit, then emptying it and using it for books later.

  2. Instance Variables: Boxes on specific shelves. If you have a shelf for electronics, boxes on it might be labeled "Mobile Phones" or "Laptops". Another shelf for clothing might have boxes labeled "T-Shirts" or "Jeans".

  3. Class (Static) Variables: Boxes with shared items everyone can use, like a box of helmets everyone in a warehouse might need.

Basically, in Java, the variable type (like local, instance, or class) is like choosing the right box to store different things. It helps keep everything organized.

That's it from my end, and Stay tuned for more insightful content. Keep your spirits high and happy learning!