Datatypes in Java

Adil Wadhwania
4 min readFeb 13, 2023

Datatypes are used to specify the type and size of data that can be stored in the variable. There are two types of datatypes in java.

  1. Primitive Data Types :-
  • Primitive data types are the basic data types that are there in java language and are used to represent simple values such as numbers, characters and boolean values.

Let use see mainly used datatypes in java language

a) Boolean : Represents a true or false value.

//Boolean datatype
boolean value=true; // true or false
System.out.println(value);//output= true
  • The boolean data type can hold only two values, true or false

b) int : Represents a 32 bit signed integer.

//int datatype
int a = 5;
System.out.println(a)// output
  • Int data type is signed 32-bit integer that can store value between -2,147,483,648 to 2,147,483,647.
  • You can not store decimal values in int data type.

c) long : Represents a 64 bit signed integer.

//long datatype
long x=564564564659L;
System.out.println(x);//output 564564564659
  • Long is the extended version of int data type we can say because it is signed data type that stores 64 bit values in the range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • We need to add ‘l’ or ‘L’ while writing a long number after the numerical value to represent that it is long value or else if we don’t write that then it will be considered as ‘int’ by default

d) float : Represents a single precision 32 bit floating number.

//float data type
float c=5.6325f;
System.out.println(c);// 5.6325
  • Single precision uses 32 bits, with the range of 3.4 x 10^-38 to 3.4 x 10³⁸ and has precision of 7 decimal places.
  • We need to add f after the float number value to differentiate it with the double value

e) double : Represents a double precision 64 bit floating number.

//double data type
double d=18.235648953532;
System.out.println(d);
  • Double data type stores values from 4.9 x 10^-324 to 1.8 x 10³⁰⁸ and has a precision of 15 to 16 decimal places, that is much higher precision but the decimal places depends on the number.

2. Reference Data types :-

  • Reference data type is used to store reference to an objects rather than the actual value of the object.
  • Reference variable holds the memory address of object in java, most common reference data type is object.

Examples of Reference data types :

a) Array :

  • An array is collection of similar data types in java. It can be of any type such as int[], String[] or Object[].
  • In simple primitive data types we can only store one value in a variable to store more than one variable of same data type you can use arrays.
  • Initializing and adding values in the array separately.
//Arrays
System.out.println("******** Array ********");
int[] f= new int[3];//Initialization
//Adding values
f[0]=1;
f[0]=2;
f[0]=3;
for (int i=0;i<f.length;i++)
{
System.out.println(f[i]);
}
  • Initializing and adding value directly
System.out.println("******* Directly adding the values in array *******");
int[] g={5,4,3,2,1};// Initialization and adding the values
for (int i=0;i<g.length;i++)
{
System.out.println(g[i]);
}

b) String :

  • A String is sequence of characters. It is an object of the String class in java.
//String
System.out.println("******** String ********");
String e="adil";
System.out.println(e);//adil
  • Java Strings are immutable, once you assign a value you can not change that value and if you try to change the value then it will be created at a new address in the memory and forgetting or removing the reference of old value.

c) Object :

  • Object is instance of class, a basic unit of data that can be used to store data and behavior in the form of methods and variables.
  • The most general reference data type in java, it can reference any kind of object.
  • To create an object we need to create a class first.
class Employee
{
// Instance or global variables
int id;
String name;

public Employee(int id, String name)
{
this.id = id;
this.name = name;
}
}
  • We have created a class employee with two instance variable “id” & name and a constructor.
  • Now to create an object of variable we need to initialize this instance variables.
System.out.println("******* Object in java *******");
Employee employee=new Employee(1,"Adil");
System.out.println(employee.id);// 1
System.out.println(employee.name);//adil
  • Here we have created an object of Employee with the help of new keyword(it is used to create an new object every time) and passing the value for instance variables for employee id and employee name as ‘1’ and ‘adil’ respectively.
  • Then we have accessed the value of id and name using the object and printed the values of the variables.

“Thank you for taking the time to read my article! I hope you found the information and insights shared here helpful and interesting. If you enjoyed this article, be sure to follow my article for more similar content. I’m always exploring new topics and sharing my thoughts and insights on a variety of subjects. I appreciate your support and look forward to connecting with you through my writing. Until next time!”

ADIL WADHWANIA :)

Topics :- java data types, data types in java, primitive data types, reference data types, java tutorials, java programming, programming concepts, java

--

--

Adil Wadhwania

Tech-savvy writer sharing insights on programming,current affairs,sports,great personalities, and new technologies. Follow for engaging and informative content.