Home » Java Tutorial » Data types in Java

Data types in Java

In the last tutorial, we made you acquainted with the basic and suggested conventions used in Java. Many of the conventions may not look like mandatory but are indeed important as and when we move ahead and the code gets more complicated.

In the current tutorial, we intend to discuss about various data types in Java.




We are sure that each one of us at some point of time would have stumbled upon our classic “Hello World” program.

Output:Hello, World

Let us just drift our focus towards the following line of code:

Datatypes-1

When we talk about classes and objects, they are all about data members and methods. Members are essentially used to store the data as in the above case, we have stored “Hello, World” in the message variable. Data members are also commonly known as identifiers, data variables or simply as variables.

These variables are responsible to obtain the memory chunk for the values to be stored.

How much memory is allocated? Who allocates the memory?

The amount of memory allocated is determined by the type of data you wish to store i.e. the datatype of the variable and the memory is allocated by the Operating system of the machine. Thus, using datatypes, we are leveraged to store different kinds of values like Boolean values, numeric values, string values etc.

What are the different kinds of datatypes in Java?

There are principally two kinds of datatypes available in Java:

  • Primitive type
  • Non – Primitive type

Though we have mainly two kinds of datatypes but one can certainly find number of variants of these two. Let us further discuss them in detail.

Datatypes-2

As discussed earlier that data types determines the type and size of the variable and the kinds of operations it can undergo, let us discuss more on it.

Primitive Data Types



Java supports eights kinds of different data types. Primitive data types are the one’s which are already defined in the language and associated with a keyword. Let us discuss each of them individually:

Data types

Datatypes-4

Default Values

Consider a situation where you declare a variable but do not assign any value to it. It is indeed important to know what happens to these kinds of variables. Variables which are declared but not assigned any value will automatically be assigned by the compiler depending on their datatype. These values are known as default values. Most of the times these values are “zero or null”.

Check out the default values below:

Datatypes-5

Non Primitive Data Types

Non primitive data types generally have three categories. Non primitive data types are also known as Reference types or reference variables. Reference variable are used to access the objects. User can create reference variables using constructors of classes. Reference variables can necessarily represent only one class type and the type cannot be further changed.

Default value of reference variables is null.

Syntax – ClassName RefObj = new ClassName();

With this, we come to an end of this tutorial. Stay tuned for the upcoming tutorials. We will keep posting more of the useful stuff on Java. Do share your reviews, comment and queries.

Read Similar Posts



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.