Java Data Types
By AmarSivas | | Updated : 2022-05-31 | Viewed : 45 times

Everyone knows that each programming language such as Java processes data based on the requirements. As a developer, we must know about all types of data in Java.
Table of Contents:
In Java, Two types of data types are used to write the different programs. These data types in the Jave are Primary Data Types and Non-Primary Data Types.
Primary Data Types
The primary data types are the crucial data types in Java and without these data types, it is not possible to process the data in Java. Primary data types are predefined by Java and developers can simply use them and are not required to declare them. Primitive data types are the main data blocks for non-primitive data types.
Default size: 1 byte default value: false
boolean isJavaRobust = true;
Default size: 2 bytes default value: '\u0000';
char ch = 's';
Default size: 1 byte Default value: 0
byte a= 65;
byte b = -65;
Default size: 2 bytes Default value: 0
short a = 100;
short b = -100;
Default size: 2^31 to 2^31-1 Default value: 0
int a = 100000;
int a = -100000;
Default size: 2^63 to 2^63-1 Default value: 0
int a = 4350000000000l;
int b = -4350000000000l;
Default size: infinity Default value: 0.0
float f = 10.5f;
Default size: infinity Default value: 0.0
double f = 10.5;
Non-Primary Data Types
Using Primary Data Types, we can define the Non-Primary Data Types to achieve the requirements through the programming. In Java, Non-Primary Data Types are
-
Class
-
Interface
-
String
-
Array
1. Classes In Java:
Classes are User-defined data type that contains different types of primitive and non-primitive data types. The classes are called prototypes of objects.
public class BankAccount {
private int accountNumber;
private int balance;
public BankAccount(int accountNumber,int balance){
this.accountNumber = accountNumber;
this.balance = balance;
}
void withdraw(int amount) {
this.balance = this.balance - amount;
System.out.println("Withdrawing money: "+amount+" in account of : "+this.accountNumber);
System.out.println("Balance amount: "+this.balance);
}
void deposit(int amount) {
this.balance = this.balance + amount;
System.out.println("depositing money"+ amount +" in account of : "+this.accountNumber);
System.out.println("Balance amount: "+this.balance);
}
public static void main(String[] args) {
BankAccount acc = new BankAccount(1000110002, 20022500);
acc.withdraw(1000);
acc.deposit(1000);
}
}
2. Interfaces In Java
This is similar to classes but the object will not be created for the interface. And interface does have empty body methods. . When implemented class is instantiated then data will be created in that implemented class's object.
public interface Vehicle {
short wheels= 4;
public short getNoOfWheels();
public String getEngineName();
}
public class Audi implements Vehicle {
public String getEngineName() {
return "Engine X555X";
}
public short getNoOfWheels() {
return this.wheels;
}
public static void main(String[] args) {
Audi audi = new Audi();
System.out.println("Audi has engine" + audi.getEngineName() +"and with wheels: "+ audi.wheels);
}
}
3. Strings In Java
A special data type designed for storing the collection of characters. It is a predefined class that has lot of methods used to manipulated the string data type.
String str = "This is string.";
String subStr = str.substring(0,4);
System.out.println(subStr);
4. Arrays in Java
Arrays are collection of homogeneous data type. homogeneous means we can store collection integers, chars or float in one array. Array are index based and index will be started form 0. Arrays is the predefined class which is used to manipulated the array data type.
int inArray[] = new int[10];
char charArray[] = new char[20];
float floatArray[] = new float[30];
System.out.println(inArray.length);
System.out.println(charArray.length);
System.out.println(floatArray.length);
Please find example code repo here Java-Data-Types-Example-App