In this blog, I will drive you through the primitive and non primitive data types of java with this table of content…
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 asByte 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
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);
}
}
character: @
one digit number: 8
alphabet: 8
ascii value: C
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
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);
}
}
error: integer number too large
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);
}
}
30000000680
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);
}
}
67.0
6.7
67.0
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);
}
}
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);
}
}
Hello From Atufa
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]);
}
}
}
}
Best
Java
Training
At
FITA
Academy
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);
}
}
22
32
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();
}
}
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
Differences between primitive data type and non-primitive data type of Java
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.