Before we talk about data type first we know about variables because data type is used with variables. Without variables the meaning of the data type is nothing, it’s incomplete. Data types are used when you create variables in the program. Each variable is assigned a data type.
The variable is the name that is used to reserve space in memory For example if we use
int a;
Then, ‘a’ is the variable of integer type. So, this is clear in the above example that without the variable data type is not complete, and without the data type, the variable can do nothing. Data types define the type of data that the variable is an integer type, float type, character type or so on.
In Java, there are two main categories of data types: primitive types and reference types.
Primitive Types:
boolean: Represents a boolean value(true/false).
byte: Represents a signed 8-bit integer value.
short: Represents a signed 16-bit integer value.
int: Represents a signed 32-bit integer value.
long: Represents a signed 64-bit integer value.
float: Represents a single-precision 32-bit floating-point value.
double: Represents a double-precision 64-bit floating-point value.
char: Represents a single 16-bit Unicode character.
Reference Types:
Class types: Represents objects created from classes.
Arrays: Represents a collection of elements of the same type(same datatype like int a[5]).
Interfaces: Represents a contract for a set of methods that a class may implement.
Enumerations: Represents a fixed set of constants.
Additionally, Java supports autoboxing and unboxing, which allows automatic conversion between primitive types and their corresponding wrapper classes (e.g., int to Integer).
You can also create custom data types using classes, interfaces, and enums to represent more complex structures and behaviors in your programs.
PRIMITIVE DATATYPES IN JAVA
In Java, primitive data types are the most basic types of data that are built-in to the language. They are used to represent simple values, such as numbers or characters. Java has eight primitive data types:
Primitive Data Types:
int: Represents integer values. Example: int myNumber = 10;
double: Represents floating-point numbers. Example: double myDouble = 3.14;
boolean: Represents the truth values true and false. Example: boolean isTrue = true;
char: Represents a single character. Example: char myChar = ‘A’;
byte: Represents a small integer value. Example: byte myByte = 100;
short: Represents a short integer value. Example: short myShort = 1000;
long: Represents a long integer value. Example: long myLong = 100000L;
float: Represents a floating-point number with less precision than double. Example: float myFloat = 1.23f;
These primitive data types are used to declare variables and are not objects themselves. They have specific sizes and behaviors defined by the Java language specification.
Here’s an example of a Java program that demonstrates the use of primitive data types
public class PrimitiveDataTypes {
public static void main(String[] args) {
// Integer types
byte myByte = 10;
short myShort = 500;
int myInt = 1000;
long myLong = 100000L;
// Floating-point types
float myFloat = 3.14f;
double myDouble = 2.71828;
// Character type
char myChar = ‘A’;
// Boolean type
boolean myBoolean = true;
// Output
System.out.println(“Byte: ” + myByte);
System.out.println(“Short: ” + myShort);
System.out.println(“Int: ” + myInt);
System.out.println(“Long: ” + myLong);
System.out.println(“Float: ” + myFloat);
System.out.println(“Double: ” + myDouble);
System.out.println(“Char: ” + myChar);
System.out.println(“Boolean: ” + myBoolean);
}
}
In this program, we declare variables of various primitive data types:
Byte (myByte): Represents a signed 8-bit integer value.
Short (myShort): Represents a signed 16-bit integer value.
Int (myInt): Represents a signed 32-bit integer value.
Long (myLong): Represents a signed 64-bit integer value.
Float (myFloat): Represents a single-precision 32-bit floating-point value.
Double (myDouble): Represents a double-precision 64-bit floating-point value.
Char (myChar): Represents a single Unicode character.
Boolean (myBoolean): Represents a boolean value (either true or false).
The program assigns values to these variables and then prints their values using the System.out.println() statement.
When you run this program, you will see the values of each variable printed to the console.
NON-PRIMITIVE DATATYPES IN JAVA
In Java, there are several non-primitive data types, also known as reference types or objects. These data types are derived from classes or interfaces and provide more complex and sophisticated functionalities compared to primitive types.
String: Represents a sequence of characters. Example: String myString = “Hello, world!”;
Array: Represents a collection of elements of the same type(data type ). Example: int[] myArray = {1, 2, 3, 4, 5};
Class: Represents a user-defined data type. Example: MyClass myObject = new MyClass();
Interface: Represents a contract for classes to implement. Example: MyInterface myInterface = new MyImplementation();
Enum: Represents a fixed set of constants. Example: enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY };
Primitive data types are stored directly in memory, while reference types store references (memory addresses) to the actual data.
Here’s an example of a Java program that uses a non-primitive data type, specifically an array of strings:
public class NonPrimitiveExample {
public static void main(String[] args) {
// Declare and initialize an array of strings
String[] names = {“XCnotes”, “dot”, “Com”, “Rekha”};
// Print the elements of the array
System.out.println(“Names:”);
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
}
}
In this program, we declare and initialize an array of strings called names. The array contains four elements: ” XCnotes “, “dot”, “Com”, and “Rekha”. We then use a for loop to iterate over the elements of the array and print them to the console.
Arrays are non-primitive data types in Java because they are objects that can contain multiple values of the same type. In this case, the array holds multiple string values.