Tutorials Hut

Java Data Types

What are java data types? Java Data types in java is the same as real world data types which are used to represent a certain set of data and their basic nature, for example in mathematics there are integers, floats etc. similarly in the programming world to represent those we have some data types.

If you have already worked on C, C++ or any similar programming language then you must have used int, float, double, char etc data types.

Remember these data types need space in memory to store for example int will take some bytes(8 bit will be equal to 1 byte) in memory and float will take more bytes to store in memory as the complexity and requirement increases data type memory requirement changes as well.

There are two data types categories are in java:

  1.  Primitive Data Types.
  2. Non-primitive/ Reference/Object Data Type

Primitive Data Types

As the name suggests, primitive data types are old and primitive which we have been using or known to them in mathematics or any programming language.

You are already aware about a few of them but just don’t know till now that they are primitive in java for example int (integer in mathematics) or float.

As a programmer you should know these data types and how much memory they take so you will be able to choose appropriate one for your program and also will save memory which will make your program high performant.

Let’s jump into java’s primitive data types:

  • boolean: Boolean is used for flag or any condition, it can hold only two values true or false, it is widely used in conditions in code.
  • byte: byte can a small data type which can hold -128 to 127 any integer(in mathematics)
  • char: char is used to represent single characters, example a-z any letter or A-Z any letter etc..
    short: it is kind of integer used to represent smaller range of integer numbers.
  • int: int represents integer from mathematics.
  • long: it is kind of integer but have ability to hold huge numbers.
  • foat: it is used to hold float numbers with decimal points.
  • double: it is used to hold float values with decimal points but has ability to hold large float numbers.
Below tabular data is showing all primitive data types in java, their sample values, memory required and range:
Java’s primitive data types

Non-primitive/ Reference/Object Data Type

As the name suggests non primitive data types are newer data types which are also known as reference or object data types.

Examples of reference data types in java are Long, Integer, Boolean and Class, at first some of these may look primitive but they are completely different.

Each reference or object data type may even have methods within them for operations.

Few Examples of non primitive:

  • Integer
  • Boolean
  • List
  • Array
  • String
  • Class (any class)

Below diagram of data types gives quick overview of both types:

diagram of data types

How to Declare Data Types In Code

Syntax:

<Name of primitive or non-primitive data type> <desired name to hold>  = <value>;

 Examples of primitive data type:

int age = 20;
float amount = 20.0f;
char firstChar = 'c';

Examples of non primitive data types:

Integer weight = 20;
Float price = 20.0F;
String name =  "developer";

















  • Leave a Reply

    Your email address will not be published. Required fields are marked *