Home

 › 

Articles

 › 

7 Different Types of Variables in Java

Concept of java programming language. Web development software technology.

7 Different Types of Variables in Java

Key Points

  • Java has three types of variables: local, instance, and static.
  • Local variables are declared inside a method, block, or constructor and are only visible within that block.
  • Java has several data types, including byte, short, int, long, float, boolean, and char.

Today, we’re diving into variables in Java. Not only that, we’re going to look at data types too. As you embark on your Java programming journey, understanding these concepts is fundamental to effectively manipulating data and making your code more dynamic.

Despite the widespread belief, Java does not have a vast multitude of variables. In fact, there are only three types of variables in Java: local, instance, and static.

What often confuses beginners and even some seasoned programmers, is the concept of ‘data types.’ With a plethora of options, data types can often be mistaken for variable types.

However, they are two distinct entities. That’s why we’re going to talk about both and clear up any confusion you might have. Let’s get into it!

Variables in Java

Let’s explore the three types of variables in Java, and then delve into each data type in more detail.

Local

As the name implies, local variables are for use “locally,” or within the same method that they were declared.

Local variables are declared inside a method, block, or constructor. They don’t have any power outside of this realm, and they live and die inside the block they were born in.

One important differentiator from other variable types is you can’t use access modifiers for local variables. They are only visible within the declared block.

Let’s look at some sample code to see what this looks like:

public void myMethod() {
    int localVariable = 10; // This is a local variable
    System.out.println(localVariable); // prints 10
}

Instance

Instance variables are declared in a class but outside a method. They are created when an object is created using the ‘new’ keyword, and destroyed when the object is destroyed.

Instance variables hold values that you want to be able to access by more than one method.

Let’s see what this looks like in code:

public class MyClass {
    int instanceVariable = 100; // This is an instance variable

    public void showValue() {
        System.out.println(instanceVariable); // prints 100
    }
}

Static

Static variables are also known as class variables because they are associated with the class and common for all instances (objects) of the class.

They are declared by using the ‘static’ keyword before the data type of the variable. Static variables are created when the program starts and destroyed when the program stops. Here’s what they look like in action:

public class MyClass {
    static int staticVariable = 200; // This is a static variable

    public void showValue() {
        System.out.println(staticVariable); // prints 200
    }
}

Data Types

To clear up any misconceptions, think of a variable as a container that holds data. The ‘type’ of this container, i.e., whether it is local, instance, or static, determines its accessibility within the code.

On the other hand, the ‘data type’ specifies the kind of data this container holds. Is it an integer? A string? A float? That’s what the data type tells us.

For example, if we wanted a variable called number, we can write:

int number = 5;

In this case, int is the data type, and that lets the Java compiler know that we will be storing integers in the number variable. Let’s look at a few of the different data types in Java.

Byte

The byte is the smallest integer data type in Java. It takes up 8 bits of memory, and as such, can represent numbers from -128 to 127.

Since it takes up a minimal amount of memory, bytes are often used when memory usage optimization is crucial, such as when dealing with large amounts of data. If this is a concern, use bytes where possible.

Short

Taking up 16 bits of memory, the short is another relatively small integer data type. It is able to represent numbers from -32,768 to 32,767. Similar to the byte, it can be used for memory optimization, but it is able to handle larger numbers.

Int

In Java, int is the most common integer type used. Unlike the previous two, int uses 32 bits of memory and can represent numbers between -2,147,483,648 and 2,147,483,647. This data type can handle most uses of whole numbers, and if memory is not an issue, it can be used without concern.

Long

If you want to represent a number outside the range of int, the long data type may come in handy. Since it uses 64 bits of memory, it can represent numbers -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Longs can be useful when precision in your program is required. Note that when a long literal is created, an upper or lower case L must be on the end, as shown:

long distance = 3000000000L;

Float

Unlike the previous data types that have been used to store whole numbers, the float is used to store single-precision floating point numbers. That is, numbers with fractional parts.

With 32 bits, the float can be used when precision is not particularly important. If you want more precision, there is another variable, the double, that uses 64 bits.

Similar to long literals, float literals are denoted with an upper or lower case F, like so:

float temperature = 27.5F;

Boolean

The boolean data type is used to represent logical values, either true or false. They are useful for controlling flow, such as creating conditionals and loops. Although the data for a boolean only needs to be 1 bit, java virtual machines typically implement them as a whole byte.

Char

The char data type represents a single character, and it is commonly used to represent individual characters, symbols, or small strings. With 16 bits, it can store Unicode characters, enabling support for international symbols. Note that char literals are enclosed in single quotes, like this example:

char grade = ‘A’;

Wrapping Up

If you take anything from this article, it should be the distinction between data types and variables in Java. Once you have a solid understanding of that, you will be on your way to understanding Java’s object-oriented concepts.

You’ll be able to use various data types depending on the project and declare variables as local, instance, or static, depending on where and how you plan to use them.

Now, let’s dive into some commonly asked questions about Java variables.

Summary Table

TypeDescription
Local VariablesDeclared inside a method, block, or constructor. They don’t have any power outside of this realm, and they live and die inside the block they were born in.
Instance VariablesHold values that you want to be able to access by more than one method.
Static VariablesDeclared by using the ‘static’ keyword before the data type of the variable. Static variables are created when the program starts and destroyed when the program stops.
ByteThe smallest integer data type in Java. It takes up 8 bits of memory.
ShortTakes up 16 bits of memory, another relatively small integer data type.
IntThe most common integer type used in Java. Uses 32 bits of memory.
LongUses 64 bits of memory, can represent numbers to a large range.
FloatUsed to store single-precision floating point numbers. That is, numbers with fractional parts. With 32 bits.
BooleanUsed to represent logical values, either true or false.
CharRepresents a single character, and it is commonly used to represent individual characters, symbols, or small strings. With 16 bits, it can store unicode characters.

Frequently Asked Questions

What are the 8 variables in Java?

In Java, the 8 primitive types of data are byte, short, int, long, float, double, char, and boolean.

What are the types of variables in Java?

Aside from data types, variables are generally classified as local, instance, or static variables. Where it is present in the code and how it is being used determines what type of variable it is.

How to find type of variable in Java?

To get the type of a variable in Java, you can use the getClass and getSimpleName methods to get the class and the name of the class.

What are the limitations of variables in Java?

Java has no limit on the length of names for variables, but you cannot use any of Java’s keywords as variable names. Additionally, variable names are always case-sensitive, and should be placed to the left of assignment operators.

Does Java have constants?

Yes! Java lets you declare variables as constants. If you don’t want your variable’s value to change once assigned, you should declare it a constant. To do this, all you need to do is add the word ‘final’ before your variable declaration.

Is Java dynamic or statically typed?

Java is a statically-typed language. What this means for variables is that they need to be declared before they can be assigned values.

can variables be inherited in java?

Yes. Variables can be inherited if a subclass is created within a class using the ‘extends’ keyword. Any variables from the superclass (the class that contains the subclass) can be accessed directly within the subclass.

Does every variable in Java need a data type?

Yes. Every Java variable needs to have a data type since this is what determines what type of values the variable can hold.

To top