” Data Types” in Java…
Java is an object-oriented language. but we can say that it is not a fully object-oriented language, since it supports the primitive type data types. Object Oriented means it is a single root hierarchy as everything deliver from the Object level class. There are two types of data types in Java language.
1. Primitive data type
2. Reference data type
Primitive Data Types
Primitive data types, also known as basic data types, are the simplest building blocks of data in a programming language. They are predefined by the language and represent fundamental values like numbers, characters, and boolean values. Here are some common primitive data types you’ll find in many programming languages:
- byte: Used to store 8-bit signed integers. Range: -128 to 127.
- short: Used to store 16-bit signed integers. Range: -32,768 to 32,767.
- int: Used to store 32-bit signed integers. Range: -2³¹ to 2³¹ — 1.
- long: Used to store 64-bit signed integers. Range: -2⁶³ to 2⁶³ — 1.
- float: Used to store single-precision 32-bit floating-point numbers.
- double: Used to store double-precision 64-bit floating-point numbers.
- char: Used to store a single 16-bit Unicode character.
- boolean: Used to store either true or false.
byte myByte = 42;Get Kalana Sudesh Gunaweera’s stories in your inbox
Join Medium for free to get updates from this writer.
short myShort = 10000;
int myInt = 123456;
long myLong = 9876543210L;
float myFloat = 3.14f;
double myDouble = 2.71828;
char myChar = 'A';
boolean isJavaFun = true;
Reference Data Types
Reference data types, also known as complex data types, are more sophisticated than primitive data types. They are called “reference” types because they store references or memory addresses pointing to the actual data. This allows them to represent more complex structures and dynamically allocated data. Common examples of reference data types include:
Classes: User-defined classes are reference data types. They encapsulate data and behavior within objects.
// Create strings
String greeting = "Hello, ";
String name = "Alice";
// Concatenate strings
String message = greeting + name;Arrays: Arrays are collections of elements of the same type, and they are reference data types.
// Create an array of integers
int[] numbers = {1, 2, 3, 4, 5};
// Access elements of the array
int firstNumber = numbers[0]; // Accessing the first elementInterfaces: Interfaces define a contract for classes to implement, and they are reference types.
// Define an interface
interface Shape {
double area();
}
// Implement the interface in classes
class Circle implements Shape {
double radius;
@Override
public double area() {
return Math.PI * radius * radius;
}
}- Enumerations: Enums represent a fixed set of constants and are reference data types.
// Define an enum for days of the week
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
// Use the enum constants
Day today = Day.WEDNESDAY;








