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.






Quick Enquiry

Please wait while submission in progress...


Contact Us

Chennai

  93450 45466

Bangalore

 93450 45466

Coimbatore

 95978 88270

Online

93450 45466

Madurai

97900 94102

Pondicherry

93635 21112

For Hiring

 93840 47472
 hr@fita.in

Corporate Training

 90036 23340


FITA Academy Branches

Chennai

FITA Academy - Velachery
Plot No 7, 2nd floor,
Vadivelan Nagar,
Velachery Main Road,
Velachery, Chennai - 600042
Tamil Nadu

    :   93450 45466

FITA Academy - Anna Nagar
No 14, Block No, 338, 2nd Ave,
Anna Nagar,
Chennai 600 040, Tamil Nadu
Next to Santhosh Super Market

    :   93450 45466

FITA Academy - T Nagar
05, 5th Floor, Challa Mall,
T Nagar,
Chennai 600 017, Tamil Nadu
Opposite to Pondy Bazaar Globus

    :   93450 45466

FITA Academy - Tambaram
Nehru Nagar, Kadaperi,
GST Road, West Tambaram,
Chennai 600 045, Tamil Nadu
Opposite to Saravana Jewellers Near MEPZ

    :   93450 45466

FITA Academy - Thoraipakkam
5/350, Old Mahabalipuram Road,
Okkiyam Thoraipakkam,
Chennai 600 097, Tamil Nadu
Next to Cognizant Thoraipakkam Office
& Opposite to Nilgris Supermarket

    :   93450 45466

FITA Academy - Porur
17, Trunk Rd,
Porur
Chennai 600116, Tamil Nadu
Above Maharashtra Bank

    :   93450 45466

FITA Academy - Pallikaranai
335A, 13th Main Rd,
Ram Nagar South Extn,
Pallikaranai, Chennai,
Tamil Nadu 600100

    :   93450 45466

Bangalore

FITA Academy Marathahalli
No 7, J J Complex,
ITPB Road, Aswath Nagar,
Marathahalli Post,
Bengaluru 560037

    :   93450 45466

Coimbatore

FITA Academy - Saravanampatty
First Floor, Promenade Tower,
171/2A, Sathy Road, Saravanampatty,
Coimbatore - 641035
Tamil Nadu

    :   95978 88270

FITA Academy - Singanallur
348/1, Kamaraj Road,
Varadharajapuram, Singanallur,
Coimbatore - 641015
Tamil Nadu

    :   95978 88270

Other Locations

FITA Academy - Madurai
No.2A, Sivanandha salai,
Arapalayam Cross Road,
Ponnagaram Colony,
Madurai - 625016, Tamil Nadu

    :   97900 94102

FITA Academy - Pondicherry
410, Villianur Main Rd,
Sithananda Nagar, Nellitope,
Puducherry - 605005
Near IG Square

    :   93635 21112

FITA Academy - Tiruppur
61D, Poongodi Towers 2nd floor,
Periyar Colony Bus Stop,
Tirupur - 641 652

    :   9940122502

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

    Adyar, Adambakkam, Anna Salai, Ambattur, Ashok Nagar, Aminjikarai, Anna Nagar, Besant Nagar, Chromepet, Choolaimedu, Guindy, Egmore, K.K. Nagar, Kodambakkam, Koyambedu, Ekkattuthangal, Kilpauk, Meenambakkam, Medavakkam, Nandanam, Nungambakkam, Madipakkam, Teynampet, Nanganallur, Navalur, Mylapore, Pallavaram, Purasaiwakkam, OMR, Porur, Pallikaranai, Poonamallee, Perambur, Saidapet, Siruseri, St.Thomas Mount, Perungudi, T.Nagar, Sholinganallur, Triplicane, Thoraipakkam, Tambaram, Vadapalani, Valasaravakkam, Villivakkam, Thiruvanmiyur, West Mambalam, Velachery and Virugambakkam.

    FITA Velachery or T Nagar or Thoraipakkam OMR or Anna Nagar or Tambaram or Porur 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!