Get Upto 50% Offer | OFFER ENDING IN:0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By AdminLast Updated At 2021-04-24
Explain Java data types and variables with examples

We know that Java is an OOPs, and class-based, dynamic, and high-level coding language and includes variables and data types. It is most useful in developing Android apps and enterprise software that is reliable, fast, and very secure. There are different Java data types and variables available where the variables store data values as containers and data types are specifiers.

In this blog, we are going to learn many insights on the different Java data types and variables with examples. 

Let us start with a brief intro to Java data types and the Java variables.

Variables in Java are the type of containers that store the values while running a Java program. Each variable here gets an allocation of a data type that defines the type and amount of value it can hold. Moreover, a variable is a memory location name in Java.

Java data types state the various sizes and values that are useful to store within a container. Moreover, the java language has a rich set of data types. A data type is just an attribute of a variable that informs a compiler how a programmer will use the variable. Simply, it defines the operations that we perform using this data and the value we store.

Hence, this is in brief about the data types and variables in the Java language. Let us learn them (data types and variables) in detail in the following context.

Java Data Types

There are two different Java data types - primitive and non-primitive. Let us know these data types in detail with examples.

Java Data Types

Primitive data types Java

These data types in Java are hardcoded into the compiler and are identified while a program execution. Examples of these Java data types include the following- boolean, char, byte, short, int, long, float, double, etc.

These Java data types are predefined by the coding language without which it is impossible to design programs. Also, they are the base for non-primitive data types and also for data manipulations. 

Learn more about Core Java Online Course with real-time sessions through industry experts at OnlineITGuru. You will get the best learning exp. of Java with live projects and assistance.

The Java primitive data types include the following types which we will discuss in detail.

Integer Java data type

Here integer or int data type is useful for storing integer values. This is typically used as a default data type in Java for integer values. The size of this data type is “4” bytes and includes a default value of “0”. The max value of the integer is 2^31 and the minimum value is -2^31. The value lies between the two elements only. Moreover, this data type is useful to store integer values unless there is a need for storing numbers larger or smaller than the limits. 

Example of integer:- int x=75;

Float Java data types

The float data type within Java data types is useful for holding various decimal values. By the default value is 0.0f and it includes a size of 4 bytes. Moreover, this data type includes a limitless value range. Although, it is advisable to use a float in place of double for good if there is a memory limitation. Moreover, it is a single-precision floating-point with 32-bit. This data type is not suitable for Currency as it always fluctuates.

The example for the Float data type value is- f=98.7f

||{"title":"Master in Core Java", "subTitle":"Core Java Certification Training by ITGURU's", "btnTitle":"View Details","url":"","boxType":"demo","videoId":"Vi7pdpk5Vq0"}||

Long data type in Java

The long data type within Java data types initially stores large size of numeric data. It is a 64-bit two’s complement integer holding values ranging from (-2^63) to +(2^63-1). Moreover, the size of the value here is 8 bytes only. And is useful in holding data more than the “int” Java data types.

The example value of this data type- long x=100000L

Character data type in Java

The character data type in Java data types is useful in holding the single value characters. This is a 16-bit Unicode character type data that ranges from min value ‘\u0000’ and max value '\uffff'. Moreover, the data type holds a size of 2 bytes. 

An example of this data type is- char letterX=’X’

Boolean data type in Java

This is a special Java data type that holds the values “true’ and “false” only. Further, the default value of this data type is “false” and includes a size of 1 byte. Further, the values storable under this data type are flag values. Hence, it specifies only a single bit of info but size cannot be defined. It helps to track the “true/false” conditions of the values.

An example of Boolean data type is- boolean one=false.

Byte data type in Java

This is a kind of Java data types under primitive data type which is an 8-bit two’s complement integer value. Besides, the value range lies between -128 to 127 where -128 is the min and 127 is the max value. Also, the default value here is “0”. This data type is useful to save memory within large arrays where the saving of memory is needed. Hence, it saves more memory space as the byte is 4 times smaller than the integer. Moreover, it can be a replacement for integer values. 

An example that denotes byte data type is- byte x = 10

Short data type

A short data type within the Java data types includes a 16-bit signed two’s complement. Although the short data type is 2x smaller than the int data type. Also, the value of this data type ranges from -32 to 32 with “0” as the default value. Here the min value is -32 and the maximum value is 32. Also, the short data type is useful to store value/data similar to the byte data type.

An example of short data type is- short s= 2000

Double data type

The double data type within the Java data types is similar to the float data type. This includes a 64-bit IEEE 754 floating-point within it. This data type is typically useful for decimal values similar to float. However, the double data type is also not useful for precise value such as currency value. 

The example of double data type is- double d=12.3

Hence, these are the different Java data types with examples. And further, we will see in the chart of data types size in brief.

Java data types size

The following chart represents the various Java data types size for different values. There are various sizes of data types available in this coding language.

Data Type    Size                   Description

Byte            1 byte            This data type stores the whole numbers from -128 to 127

short          2 bytes          Short data type stores the numbers from -32,768 to 32,767

int              4 bytes          the int data type holds numbers from -2,147,648 to 2,147,647

long           8 bytes         long data type holds the numbers from -9,223,036,  -  9,223,036,

float           4 bytes        float data type mainly holds fractional numbers storing 6 to 7 Dec. digits

double       8 bytes       double data type holds frac numbers useful in storing 15 Dec. digits

Boolean      1 bit               the boolean data type stores true or false values

char           2 bytes     the char data type Stores a single character or ASCII values with two bytes

Non-primitive data types

The non-primitive Java data types are user-defined special type data or the program consists of their description. Examples of non-primitive data types in Java include the following- Classes, Interfaces, and arrays. 

Let us learn these non-primitive Java data types in detail.

Java Classes

The “Classes” within the non-primitive Java data types are the special user-defined data type. Also, this includes member variables and class methods. In general, a class in Java is nothing but a group of various objects with some common properties. The class in Java coding includes fields, blocks, methods, etc.

Interfaces in Java

The interfaces within Java coding language are similar to or the blueprint of classes. They include static constants and abstract methods. These interfaces have no method body as they are abstract. Also, interfaces are the mechanism to achieve abstraction within the Java program. 

Strings in Java

A string in Java is a non-primitive data type and an object that is useful in performing certain ops on strings. Also, in Java, there are different types of methods useful to transform strings like substring, length, etc.


An array in Java is a collection of elements of a similar type that includes adjacent memory locations. Moreover, arrays are index-based and always start indexing from “0”. An array is a kind of structure that stores the elements of a similar data type. 

Furthermore, an array is also passed as local variables, static fields, and method parameters, etc. This is within the program and they get a dynamic allocation. 

Example of the array: int arr[] = latest int[200];

Hence, this is all about Java Data Types and their different methods. How they are useful in real-time and how the programmer makes them easier for users now we will look into the different types of variables in Java.

Java variables

In Java, variables are similar to containers that store or holds value during the performance of the Java program. It is a name for the memory location where a variable is allotted a type of data. Here, local, static, and instance types of Java variables are available. In other words, it holds a reserve area distribution within memory. 

Let us look into variable types in Java variables.

Local variables

These types of variables declaration held within the body of the method. Therefore, this variable is useful within that particular method only and other method classes even don’t aware of its existence.

These variables’ development held while the block enters into. And the same is deleted after exiting from the block/method.

Moreover, the scope of local variables exists only inside the block in which the variable is declared. This is said that variable access is done only within that block and no other place.

Instance variables

The instance variables are openly announced outside the body of the method, or block. And without the Static keyword usage. Moreover, these variables are another name for instance variables due to their instance-specific value. 

The declaration of instance variables is within a class and is built while an object of the class is built. Besides, these are deleted while the object is complete.

Unlike local variables, here we can use access specifiers for these types of variables. In case, the user does not mention any specifier then the default access specifier is useful here.

Moreover, the operation of the Instance Variable is not necessary as the default value of this variable is “0”.

By developing objects only these variables are given access to the user.

||{"title":"Master in Core Java", "subTitle":"Core Java Certification Training by ITGURU's", "btnTitle":"View Details","url":"","boxType":"reg"}||

Static variables

These variables declare statically and are started only once at the beginning of the program. Here a user can make a copy of the static variable and share it across the instances of the class. Moreover, the memory allocation for this type of variable done once only while loading of class into the memory. 

The following example denotes all three variables in one place.

static int x = 01; (type-static variable)  

    int data = 99;  (type-instance variable)  

    void method () {

        int y = 80; (type-local variable)  



The above discussion on different Java data types and variables in Java ends here. I hope you got the basic idea of the variables and data types and their use in this coding language. However, we tried to learn every aspect of the data types and variables in this blog. Knowing about various data types and variables in Java is important  If you have the interest to learn more in Java in real-time then go through the Core Java Online Training program with ITGuru. The industry expert mentors at this platform will guide you each and everything with real-time experience.