Overview of Java Data Types


<aside> <img src="/icons/table_red.svg" alt="/icons/table_red.svg" width="40px" /> Table of Contents

</aside>

<aside> 💡

  1. Java Variables </aside>

<aside> 💡

  1. Scope of Variables In Java </aside>

Java Variables


  1. Java Variables

    1. Variables are the containers for storing the data values or you can also call it a memory location name for the data. Every variable has a:
      1. Data Type – The kind of data that it can hold. For example, int, string, float, char, etc.
      2. Variable Name – To identify the variable uniquely within the scope.
      3. Value – The data assigned to the variable.
    2. There are three types of variables in Java – Local, Instance, and Static.
      1. Example:

        int age = 27; // integer variable having value 27
        
        String name = "Hello" // string variable 
        

  2. How to Declare Java Variables?

    1. We can declare variables in Java as pictorially depicted below:

      image.png

    2. From the image, it can be easily perceived that while declaring a variable, we need to take care of two things that are:

      1. Data type: Type of data that can be stored in this variable.
      2. Data name: Name was given to the variable.
    3. In this way, a name can only be given to a memory location. It can be assigned values in two ways:

      1. Variable Initialization
      2. Assigning value by taking input

  3. How to Initialize Java Variables?

    1. It can be perceived with the help of 3 components explained above:

      image.png

    2. Example:

      // Declaring float variable
      float simpleInterest; 
      
      // Declaring and initializing integer variable
      int time = 10, speed = 20; 
      
      // Declaring and initializing character variable
      char var = 'h';
      

  4. Types of Java Variables

    1. There are different types of variables  which are listed as ****follows:

      1. Local Variables

      2. Instance Variables

      3. Static Variables

        image.png

    2. Type of variable listed here in detail.


  5. Local Variables

    1. A variable defined within a block or method or constructor is called a local variable.

    2. The Local variable is created at the time of declaration and destroyed after exiting from the block or when the call returns from the function.

    3. The scope of these variables exists only within the block in which the variables are declared, i.e., we can access these variables only within that block.

    4. Initialization of the local variable is mandatory before using it in the defined scope.

    5. Example 1:

      // Java Program to show the use of local variables
      import java.io.*;
      
      class SampleProgram {
          public static void main(String[] args)
          {
              // Declared a Local Variable
              int var = 10;
      
              // This variable is local to this main method only
              System.out.println("Local Variable: " + var);
          }
      }
      
    6. Output:

      Local Variable: 10
      
    7. Example 2:

      // Java Program to show the use of 
      // Local Variables
      import java.io.*;
      public class SampleProgram {
          public static void main(String[] args)
          {
              // x is a local variable
              int x = 10;
      
              // message is also a local variable
              String message = "Hello, world!";
      
              System.out.println("x = " + x);
              System.out.println("message = " + message);
      
              if (x > 5) {
                  // result is a local variable
                  String result = "x is greater than 5";
                  System.out.println(result);
              }
      
              // Uncommenting the line below will result in a
              // compile-time error System.out.println(result);
      
              for (int i = 0; i < 3; i++) {
                  String loopMessage
                      = "Iteration "
                        + i; // loopMessage is a local variable
                  System.out.println(loopMessage);
              }
      
              // Uncommenting the line below will result in a
              // compile-time error
              // System.out.println(loopMessage);
          }
      }
      
    8. Output:

      x = 10
      message = Hello, world!
      x is greater than 5
      Iteration 0
      Iteration 1
      Iteration 2