For Enquiry: 93450 45466

Data Types In Java: Primitive And Non Primitive Data Types In Java



In this blog, I will drive you through the primitive and non primitive data types of java with this table of content…
Data types in Java
Types of Data Types
Primitive Data Types In Java
Non Primitive Data Types In Java
Differences between primitive data type and non primitive data type of Java

Check out this Complete Java Online Course by FITA. FITA provides a complete Java course including core java and advanced java J2EE, and SOA training, where you will be building real time applications using Servlets, Hibernate Framework, and Spring with Aspect Oriented Programming (AOP) architecture, Struts through JDBC bundled with, placement support, and certification at an affordable price with an active placement cell,by expert software developers with 10+ years of experience in the field to make you an industry required certified java developer.

So let us get started with by first understanding what are data types in java, and various types of data types in java.

Data Types In Java And Its Categories

Data Types in java are used to store organized data. Your programs are almost built around data types so that you can work effectively and efficiently with the data. The data types in java are categorized into primitive data types and non primitive data types. Now that you understood data types and its categories, let us understand each of its categories in detail, starting with Primitive Data Type in Java.

Primitive Data Types In Java

Primitive data types are built in or pre-defined in java, and so you cannot modify their behavior. These are the basic data types including, integer, float, string, and boolean, which contain pure and simple forms of data. There are 8 primitive data types as
boolean data type in java
byte data type in java
char data type in java
short data type in java
int data type in java
long data type in java
float data type in java
double data type in java

Byte data type in java

This is an 8 bitย  or 1 byte signed data type which can store integers lying between -125 and 172. You can use this data type for memory efficiency. Here is a small program in java for demonstrating byte data type.

// Example java program to demonstrate the byte data type

import java.util.*;

 

class Main {

public static void main(String[] args) {

byte b1, b2;

b1 = 127;

System.out.println(b1); // prints 127

b2 = 177;

System.out.println(b2); // throws error

}

}

 

// Output

// error: incompatible types: possible lossy conversion from int to byte
If you understand byte data type well, letโ€™s move on to the next primitive data type in Java, under integral data types we have char data type in java.

char data type in java

This data type can be used to store a single character value, for example, โ€˜eโ€™, โ€˜@โ€™, enclosed between single quotes. You can also store the characters with their ASCII value, or one digit numbers as characters.char data type can have a size of 2 bytes.

Example program for char data type in java

// Example java program to demonstrate the char data type

import java.util.*;

 

public class Main {

public static void main(String[] args) {

char b1, b2, b3, b4;

b1 = '@';

System.out.println("character: "+b1);

b2 = '8';

System.out.println("one digit number: "+b2);

b3 = 'a';

System.out.println("alphabet: "+b2);

b4 = 67;

System.out.println("ascii value: "+b4);

}

}
Output for the above program

character: @

one digit number: 8

alphabet: 8

ascii value: C
If you understood char data type well, letโ€™s move on to the next primitive data type in Java,under integral data types we have short data type.

Short data type in java

This data type can hold integral numbers from -32,768 to 32,767. The short data type can have a size of 2 bytes or 16 bits. Example program for demonstrating short data type in java

// Example java program to demonstrate the short data type

import java.util.*;

 

public class Main {

public static void main(String[] args) {

short n = 3425;

System.out.println(n); // prints 3425

short s = 327678;ย  ย  ย  // throws an error

System.out.println(s);

}

}

 

// outputs

// error: incompatible types: possible lossy conversion from int to short
If you understand short data type well, letโ€™s move on to the next primitive data type in Java, under integral data types we have int data type In Java.

int data type in java

This data type can store values up to 4 bytes or 32 bits.It can store whole numbers between -2,147,483,648 and 2,147,483,647.This data type is usually used for storing numeric values in programs.

Example program to demonstrate int data type in java

// Example java program to demonstrate the int data type

import java.util.*;

 

public class Main {

public static void main(String[] args) {

int n = 334425;

System.out.println(n); // prints 334425

int s = 2147483648;ย  ย  ย  // throws an error

System.out.println(s);

}

}
Output for the above program

error: integer number too large
If you understood, int data type well, letโ€™s move on to the next primitive data type in Java, under integral data types we have long data type In Java.

long data type in java

This data type can have the size of 8 bytes or 64 bits, and can store values between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807, or numbers from ย -263 to 263-1.

Example program to demonstrate long data type in java

// Example java program to demonstrate the long data type

import java.util.*;

 

public class Main {

public static void main(String[] args) {

 

long l = 30000000680L;

System.out.println(l);

}

}
Output for the above program

30000000680
If you understand short data types well, letโ€™s move on to the next primitive data type in Java, under integral data types we have float data type In Java.

floating data type in java

To store decimal or float numbers such as 2.544, 80.7880, you will need to use float data type.

float data type in java

This can store fractional numbers with 4 bytes of memory. float data type can have numbers from 3.4eโˆ’03 to 3.4e+038, where 3.4eโˆ’038 is the smallest positive value of float and 3.4e+038 is the largest positive value of a float. Moreover, the float numbers or the values should end with an โ€˜fโ€™.Let us look at an example

Example program to demonstrate float data type in java

// Example java program to demonstrate the float data type

import java.util.*;

 

public class Main {

public static void main(String[] args) {

 

float num1 =67;

System.out.println(num1);

float num2 =6.7f;

System.out.println(num2);

float num3 =67.0f;

System.out.println(num3);

}

}
Output for the above program

67.0

6.7

67.0
If you understand float data types well, letโ€™s move on to the next primitive data type in Java, under floating data types we have double data type In Java.

double data type in java

This data type can store values or numbers from 1.7eโˆ’308 to 1.7e+308, ending with a โ€˜dโ€™ character, Here is an example Example program to demonstrate double data type in java

// Example java program to demonstrate the double data type

import java.util.*;

 

public class Main {

public static void main(String[] args) {

 

double num = 79.678d;

System.out.println(num);

double num2 = 79678d;

System.out.println(num2);

// adding double numbers results in an double value

double add = num + num2;

System.out.println(add);

}

}
Output for the above program

79.678

79678.0

79757.678

Check out this Complete Online Java Training by FITA. FITA provides a complete Java course including core java and advanced java J2EE, and SOA training, where you will be building real time applications using Servlets, Hibernate Framework, and Spring with Aspect Oriented Programming (AOP) architecture, Struts through JDBC bundled with, placement support, and certification at an affordable price with an active placement cell,by expert software developers with 10+ years of experience in the field to make you an industry required certified java developer.

Now that you understood various primitive data types, let us understand the next categories of data types, Non Primitive Data types In JAva in detail.

Non primitive data types in Java

The non primitive data types are also known as reference types since they refer to objects. Strings, Arrays, classes, and interfaces are the examples of non primitive data types. Let us understand each of these non primitive data types briefly.

Strings in java

Although all the non primitive data types are defined by the programmer, a String is already defined by java. It is a sequence of collections of elements or characters, joined together and enclosed between double quotes.

You can refer to string functions in java for understanding all the built in methods for java and string arrays in java for understanding how string and arrays work in java.

Example program to demonstrate String data type in java

// Example java program to demonstrate the string data type

import java.util.*;

 

public class Main {

public static void main(String[] args) {

 

String greet = "Hello From Atufa";

System.out.println(greet);

}

}
Output for the above program

Hello From Atufa
If you understood, String in java well, letโ€™s move on to the next non primitive data type Arrays in Java.

Arrays In java

Arrays are like lists in java, which can hold more than one value, or as specified at the time of declaration. The elements of the array are homogenous or are of the same data type. Moreover you can traverse through elements of the array by looping, and other operations such as adding, removing, or updating elements of the array can be done on n-dimensional arrays.

Example program to demonstrate arrays in java

// Example java program to demonstrate the arrays

import java.util.*;

 

public class Main {

public static void main(String[] args) {

String[][] strArr = { { "Best", "Java", "Training" }, { "At", "FITA", "Academy" } };

for (int row = 0; row < strArr.length; ++row) {

for (int col = 0; col < strArr[row].length; ++col) {

System.out.println(strArr[row][col]);

}

}

}

}
Output for the above program

Best

Java

Training

At

FITA

Academy
If you understood, Arrays in Java well, letโ€™s move on to the next non primitive data type Classes in Java.

Classes In Java

A class defines the attributes and behavior of an object. For example, if you have an Employee class, it can have attributes like name, salary, and age, whereas its behavior can include functions such as deduct or increment salary or calculate bonus, etc, whereas objects are the instances of classes or derived attributes and behaviors from class with actual data. Functions underclasses are known as methods. They define the behavior of a class.

The class keyword is prefixed to the class name to define a class. Example program to demonstrate arrays in java

// Example java program to demonstrate the classes

import java.util.*;

public class Main {

int val_1 = 22;

int val_2 = 32;

 

public static void main(String[] args) {

 

// creating multiple objects for the class main

Main obj_1 = new Main();

Main obj_2 = new Main();

System.out.println(obj_1.val_1);

System.out.println(obj_2.val_2);

}

}
Output for the above program

22

32
If you understood, Classes in Java well, letโ€™s move on to the next non primitive data type in Interfaces in Java.

Interfaces in java

Interfaces in java are pure abstract classes and also behave like an inheritance. The methods are declared in the base or super class without a definition body.

The methods are defined in the child class or derived class of the base class with the implement keyword. Let me clear you with an example

Example program to demonstrate interfaces in java

// Example java program to demonstrate the interfaces

import java.util.*;

 

// A Java Interface

interface Shape {

 

// Abstract methods (does not have a body)

public abstract void area();

 

public abstract void perimeter();

 

}

 

// Subclass (inherit from Shape)

class Circle implements Shape {

public void area() {

 

// The body of area() for the circle is provided here

System.out.println("Area of the circle: pi*radius*radius");

}

 

public void perimeter() {

 

// The body of perimeter() for the circle is provided here

System.out.println("The perimeter of the circle: 2*pi*radius");

}

}

 

// Subclass (inherit from Shape)

class Triangle implements Shape {

public void area() {

 

// The body of area() for the triangle is provided here

System.out.println("Area of the triangle: half*base*height");

}

 

public void perimeter() {

 

// The body of area() for the triangle is provided here

System.out.println("The perimeter of the triangle: base+height+side");

}

}

 

class Main {

public static void main(String[] args) {

Circle crc = new Circle(); // Create a Circle object

crc.perimeter();

crc.area();

System.out.println();

Triangle tri = new Triangle(); // Create a Triangle object

tri.perimeter();

tri.area();

 

}

}
Output for the above program

The perimeter of the circle: 2*pi*radius

Area of the circle: pi*radius*radius

The perimeter of the triangle: base+height+side

Area of the triangle: half*base*height
Now that you understood the Primitive and Non-Primitive Data Types In Java, it is the time to compare them and see their differences.

Differences between primitive data type and non-primitive data type of Java

Non primitive data types are defined by the programmer, whereas primitive data types are built-in in java.
A non primitive data type can have a null value, but a primitive data type cannot.
The size of primitive data types is pre-defined and cannot be changed, where as non primitive data types size depends on the type and number of objects.

This was all about primitive and non primitive data types in java, implementation, and their differences with example programs. To get in depth knowledge of core Java and advanced java, J2EE,ย  SOA training along with its various applications and real-time projects using Servlets, Spring with Aspect Oriented Programming (AOP) architecture, Hibernate Framework,and Struts through JDBC you can enroll in Certified Java Training in Chennai or Certified Java Training in Bangalore by FITA or a virtual class for this course,at an affordable price, bundled with real time projects, certification, support, and career guidance assistance and an active placement cell, to make you an industry required certified java developer.

FITAโ€™s courses training is delivered by professional experts who have worked in the software development and testing industry for a minimum of 10+ years, and have experience of working with different software frameworks and software testing designs.





  • Trending Courses

    JAVA Training In Chennai Software Testing Training In Chennai Selenium Training In Chennai Python Training in Chennai Data Science Course In Chennai Digital Marketing Course In Chennai DevOps Training In Chennai German Classes In Chennai Artificial Intelligence Course in Chennai AWS Training in Chennai UI UX Design course in Chennai Tally course in Chennai Full Stack Developer course in Chennai Salesforce Training in Chennai ReactJS Training in Chennai CCNA course in Chennai Ethical Hacking course in Chennai RPA Training In Chennai Cyber Security Course in Chennai IELTS Coaching in Chennai Graphic Design Courses in Chennai Spoken English Classes in Chennai Data Analytics Course in Chennai

    Spring Training in Chennai Struts Training in Chennai Web Designing Course In Chennai Android Training In Chennai AngularJS Training in Chennai Dot Net Training In Chennai C / C++ Training In Chennai Django Training in Chennai PHP Training In Chennai iOS Training In Chennai SEO Training In Chennai Oracle Training In Chennai Cloud Computing Training In Chennai Big Data Hadoop Training In Chennai UNIX Training In Chennai Core Java Training in Chennai Placement Training In Chennai Javascript Training in Chennai Hibernate Training in Chennai HTML5 Training in Chennai Photoshop Classes in Chennai Mobile Testing Training in Chennai QTP Training in Chennai LoadRunner Training in Chennai Drupal Training in Chennai Manual Testing Training in Chennai WordPress Training in Chennai SAS Training in Chennai Clinical SAS Training in Chennai Blue Prism Training in Chennai Machine Learning course in Chennai Microsoft Azure Training in Chennai Selenium with Python Training in Chennai UiPath Training in Chennai Microsoft Dynamics CRM Training in Chennai VMware Training in Chennai R Training in Chennai Automation Anywhere Training in Chennai GST Training in Chennai Spanish Classes in Chennai Japanese Classes in Chennai TOEFL Coaching in Chennai French Classes in Chennai Informatica Training in Chennai Informatica MDM Training in Chennai Big Data Analytics courses in Chennai Hadoop Admin Training in Chennai Blockchain Training in Chennai Ionic Training in Chennai IoT Training in Chennai Xamarin Training In Chennai Node JS Training In Chennai Content Writing Course in Chennai Advanced Excel Training In Chennai Corporate Training in Chennai Embedded Training In Chennai Linux Training In Chennai Oracle DBA Training In Chennai PEGA Training In Chennai Primavera Training In Chennai Tableau Training In Chennai Spark Training In Chennai Appium Training In Chennai Soft Skills Training In Chennai JMeter Training In Chennai Power BI Training In Chennai Social Media Marketing Courses In Chennai Talend Training in Chennai HR Courses in Chennai Google Cloud Training in Chennai SQL Training In Chennai CCNP Training in Chennai PMP Training in Chennai OET Coaching Centre in Chennai Business Analytics Course in Chennai NextJS Course in Chennai Vue JS Course in Chennai

  • Read More Read less
  • Are You Located in Any of these Areas

    Adambakkam, Adyar, Akkarai, Alandur, Alapakkam, Alwarpet, Alwarthirunagar, Ambattur, Ambattur Industrial Estate, Aminjikarai, Anakaputhur, Anna Nagar, Anna Salai, Arumbakkam, Ashok Nagar, Avadi, Ayanavaram, Besant Nagar, Bharathi Nagar, Camp Road, Cenotaph Road, Central, Chetpet, Chintadripet, Chitlapakkam, Chengalpattu, Choolaimedu, Chromepet, CIT Nagar, ECR, Eechankaranai, Egattur, Egmore, Ekkatuthangal, Gerugambakkam, Gopalapuram, Guduvanchery, Guindy, Injambakkam, Irumbuliyur, Iyyappanthangal, Jafferkhanpet, Jalladianpet, Kanathur, Kanchipuram, Kandhanchavadi, Kandigai, Karapakkam, Kasturbai Nagar, Kattankulathur, Kattupakkam, Kazhipattur, Keelkattalai, Kelambakkam, Kilpauk, KK Nagar, Kodambakkam, Kolapakkam, Kolathur, Kottivakkam, Kotturpuram, Kovalam, Kovilambakkam, Kovilanchery, Koyambedu, Kumananchavadi, Kundrathur, Little Mount, Madambakkam, Madhavaram, Madipakkam, Maduravoyal, Mahabalipuram, Mambakkam, Manapakkam, Mandaveli, Mangadu, Mannivakkam, Maraimalai Nagar, Medavakkam, Meenambakkam, Mogappair, Moolakadai, Moulivakkam, Mount Road, MRC Nagar, Mudichur, Mugalivakkam, Muttukadu, Mylapore, Nandambakkam, Nandanam, Nanganallur, Nanmangalam, Narayanapuram, Navalur, Neelankarai, Nesapakkam, Nolambur, Nungambakkam, OMR, Oragadam, Ottiyambakkam, Padappai, Padi, Padur, Palavakkam, Pallavan Salai, Pallavaram, Pallikaranai, Pammal, Parangimalai, Paruthipattu, Pazhavanthangal, Perambur, Perumbakkam, Perungudi, Polichalur, Pondy Bazaar, Ponmar, Poonamallee, Porur, Pudupakkam, Pudupet, Purasaiwakkam, Puzhuthivakkam, RA Puram, Rajakilpakkam, Ramapuram, Red Hills, Royapettah, Saidapet, Saidapet East, Saligramam, Sanatorium, Santhome, Santhosapuram, Selaiyur, Sembakkam, Semmanjeri, Shenoy Nagar, Sholinganallur, Singaperumal Koil, Siruseri, Sithalapakkam, Srinivasa Nagar, St Thomas Mount, T Nagar, Tambaram, Tambaram East, Taramani, Teynampet, Thalambur, Thirumangalam, Thirumazhisai, Thiruneermalai, Thiruvallur, Thiruvanmiyur, Thiruverkadu, Thiruvottiyur, Thoraipakkam, Thousand Light, Tidel Park, Tiruvallur, Triplicane, TTK Road, Ullagaram, Urapakkam, Uthandi, Vadapalani, Vadapalani East, Valasaravakkam, Vallalar Nagar, Valluvar Kottam, Vanagaram, Vandalur, Vasanta Nagar, Velachery, Vengaivasal, Vepery, Vettuvankeni, Vijaya Nagar, Villivakkam, Virugambakkam, West Mambalam, West Saidapet

    FITA Velachery or T Nagar or Thoraipakkam OMR or Anna Nagar or Tambaram or Porur or Pallikaranai branch is just few kilometre away from your location. If you need the best training in Chennai, driving a couple of extra kilometres is worth it!