Java Data Types and Variables Explained

Java Data Types and Variables

Java is one of the most popular language widely used for building software applications. It was introduced by James Gosling and his team at Sun Microsystems in 1995. This programming language is based on object-oriented programming, which includes the concepts of class and objects. 

A class is a blueprint or template for creating an object, whereas an object is a unique identity representing the state and behavior of a real-world entity. This language was built with the intention of write once/run anywhere and thus making it platform independent. 

In this article we’ll learn about data types and variables in Java with working examples and detailed explanations. 

What are Data Types in Java?

Java data types define the size and types of values that a variable can hold. It is divided into two categories, which are primitive data types and non-primitive data types. Given below is the description of these two data types. 

1. Primitive Data Types 

These data types are predefined by the language and are reserved through a specific keyword. Primitive data type do not share data like strings, numbers and boolean with other primitive values. It is further divided into different categories. Given below is the description of different types of primitive data types.

  • byte: This primitive data type is declared using the byte keyword and used to store very small integer values. The size of a byte is 8 bits, which is equal to 1 byte. The range of possible values in byte ranges from -128 to 127.
  • short: This data type is declared using the short keyword, with a size of 16-bits, used to store small integer values. The range of possible values under this category ranges from -32,768 to 32, 767, including the whole numbers.
  • int: This data type is declared using the int keyword which is a 32-bit size and the most commonly used data type for general integer values. The possible value that can be stored in this data type ranges from -2^31 to 2^31-1. 
  • long: This data type is 64-bit in size, used to store large integer value and declared through the keyword long in Java. It includes integer values ranges from -2^63 to 2^63-1. 
  • float: The float data type is a single-precision 32-bit size declared using the float keyword and used to store decimal values. The range of possible values in this category ranges from ±1.175 x 10⁻³⁸ to ±3.403 x 10³⁸.
  • double: This data type is also used to store decimal values with a size of 64 bits. The double keyword is used for declaring the double data type. This data type is generally the default choice for decimal values and it accepts values ranging from 1.7 x 10⁻³⁰⁸ to 1.7 x 10³⁰⁸
  • boolean: This data type returns either true or false as output. It is used in conditional statements and loops for decision making or controlling the flow of a program.
  • char: The char data type is used to store a single character with keyword char. This data type is of 16-bit size and one of the fundamental building blocks for working with text in Java.

2. Non-Primitive Data Type

These are the user-defined data type that store references to objects in memory, rather than actual values themselves. Non-primitive data types are used to call methods to perform certain operations and they starts with an uppercase letter. Non-primitive data types are further divided into multiple categories. Given below is the description of them.

  • Classes: It is the fundamental concept of object-oriented programming that groups related fields (data/state) and methods (behavior) into a single unit. A class act as a blueprint for creating an object. 
  • Strings: This data type is used to store the sequence of characters enclosed within double quotes allowing it to store numbers, symbols, spaces and serves to handle textual information in programming.
  • Arrays: It is used to store multiple values of the same data type in a single variable using a contiguous memory location, where each variable is accessed through an index.
  • Interfaces: An interface is a reference type that act as a blueprint for classes by specifying what methods a class should implement. Interfaces provide abstraction and multiple inheritance.
  • Object: An instance of a class representing a real-world entity with state and behavior.

Importance of Data Types in Java

Given below is the list of points that explains why data types are important in Java.

  1. Allocate appropriate memory
  2. Prevent invalid operations
  3. Improve performance
  4. Ensure type safety
Looking for a Website Developer in Delhi NCR?

Get a professionally designed and developed website tailored to your needs.
As an experienced website developer based in Delhi NCR, I offer customized solutions to build responsive, SEO-friendly, and user-friendly websites. Whether it’s for a personal blog, business site, or e-commerce store, I ensure your online presence stands out.

What are variables in Java?

A variable is a container used to hold a value that can change during the execution of a program. Variables in Java are broadly categorized into three main types, which define the scope and operations that can be performed on it. Given below is the description of different types of variables in Java.

1. Local Variables

These variables are declared within a method, constructor, or a specific block of code. The scope and lifetime of local variable are limted to a particular function under which they are declared. Given below is the example of a local variable.

public class localVariable {
    public void showText() {
       String text = “Welcome to TpointTech”; // text is a method local variable
          System.out.println(“The message of local variable is: ” + text);
       } 
public static void main(String[] args) {
      localVariable example = new localVariable();
      example.showText();
    }
}           

Output:

The message of local variable is: Welcome to TpointTech

2. Instance Variables (Non-static Field)

These variables are declared within a class but outside of any method, constructor, or block. The scope of instance variable is inside the class but outside of any methods, constructors, or block, and its lifetime is the lifetime of the objects in which it resides.

Example

public class Student {
    // Instance variable
    String name;
    int age;
    // Constructor to initialize instance variables
    public Student(String name, int age) {
       this.name = name;
     this.age = age;
   }
// Instance method to display the details
public void displayDetails() {
    System.out.println(“Name: ” + name + “ Age: ” + age);
   }


public static void main(String[] args) {
    Student s1 = new Student(“Arpit”, 19);
    s1.displayDetails();
}
} 

Output

Name: Arpit Age: 19

3. Class Variables

A class variables also known as static variables are variables that belongs to the class itself rather than any specific instance (object) of the class. They are declared using the static keyword. 

A class variable can be accessed directly using the class name, without needing to create an object of the class.

Example

public class TpointTech {
     // This is a class variable (static variable)
     public static int myClassVariable = 10;
     // This is instance variable (non-static field).
     public int myInstanceVariable = 20;
    
    public static void main(String[] args) {
         TpointTech obj = new TpointTech();
         System.out.println(“Class Variable: ” + myClassVariable);
         System.out.println(“Instance Variable: ” + obj.myInstanceVariable);
}     
}

Output

Class Variable: 10
Instance Variable: 20

Conclusion

Understanding Java’s data types and variables is fundamental to writing effective and efficient code. Primitive data types store simple values, while non-primitive data types handle complex data and objects. Variables allow programs to store and manipulate data efficiently. These concepts are essential for learning advanced Java concepts.

This article is about variables and data types in Java. To know more about Java and other advanced concepts, you can visit the TpointTech Website, where you can find various articles on programming and other technology related to computer science with interview questions, an online compiler and working examples as well.

Also Read: 6 Reasons Why ReactJS is Dominating Frontend Development

Related posts