Site icon Tutorials Website: Upgrade Your Web Development & Coding Skills

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.

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.

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

Exit mobile version