Table of contents
Prologue
We have been using data types to define data members, but we are yet to fully understand what they are, how they work, and where to use them.
Data types
A data type defines the kind of value a variable can hold and the operations that can be performed on it. It helps the compiler understand how much memory to allocate for the variable and ensures that the right kind of data is stored and manipulated. Java has two main types of data: primitive data types (e.g., int, boolean, char) for basic values and reference data types (e.g., objects, arrays) for more complex structures.
Think of datatypes as a box that store some specific type of values and it has a limited space to store that value
Primitive Type
It is a basic datatype which is predefined by the language and serve as a building blocks for data manipulation. These type of memory hold their memory directly in memory, not as references to objects. They are not objects themself
int
, char
, boolean
, etc.). These primitive types are not objects, which deviates from the pure OOP philosophy, where everything should ideally be treated as an objectbyte (8 bits): Represents small whole numbers.
short (16 bits): Represents medium-sized whole numbers.
int (32 bits): Represents larger whole numbers.
long (64 bits): Represents very large whole numbers.
float (32 bits): Represents decimal numbers with single-precision.
double (64 bits): Represents decimal numbers with double-precision for greater accuracy.
char (16 bits): Represents a single character or symbol.
bool : Represents only true or false values
Datatype | Range |
byte | -27 to 27-1 |
short | -215 to 215-1 |
int | -231 to 231-1 |
long | -263 to 263-1 |
char | 0 to 65535 |
float | 1.4E-45 to 3.4E+38 |
boolean | true / false |
double | 4.9E-324 to 1.8E+308 |
DEFAULT VALUES
Data Type | Default Values |
byte | 0 |
short | 0 |
int | 0 |
float | 0.0f |
long | 0L |
double | 0.0d |
char | '\u0000' |
string (or any object) | null |
boolean | false |
Reference Type
Reference type refers to any variable that holds a reference (or address) to an object rather than the object itself. Unlike primitive types (such as int
, char
, boolean
, etc.), which store their actual values, reference types store a reference to the memory location where the object is stored.
Classes: Templates for creating objects that encapsulate data and behavior through attributes (fields) and methods.
Arrays: Ordered collections of elements of the same type, allowing for indexed access to multiple values stored in contiguous memory.
Interfaces: Abstract types that define a contract of methods that implementing classes must provide, enabling polymorphism and multiple inheritance in Java.
(I will explain these in detail later.)
Program
class Example {
public static void main(String[] args) {
// 1. byte: 8-bit signed integer (range: -128 to 127)
byte byteValue = 100;
System.out.println("Byte value: " + byteValue);
// 2. short: 16-bit signed integer (range: -32,768 to 32,767)
short shortValue = 10000;
System.out.println("Short value: " + shortValue);
// 3. int: 32-bit signed integer (range: -2,147,483,648 to 2,147,483,647)
int intValue = 100000;
System.out.println("Int value: " + intValue);
// 4. long: 64-bit signed integer (range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
long longValue = 100000L; // L suffix indicates this is a long literal
System.out.println("Long value: " + longValue);
// 5. float: single-precision 32-bit (approx. ±3.40282347E+38)
float floatValue = 10.5f; // f suffix indicates this is a float literal
System.out.println("Float value: " + floatValue);
// 6. double: double-precision 64-bit (approx. ±1.79769313486231570E+308)
double doubleValue = 20.99;
System.out.println("Double value: " + doubleValue);
// 7. char: single 16-bit Unicode character (range: 0 to 65,535)
char charValue = 'A'; // Alternatively, you can use Unicode value like '\u0041'
System.out.println("Char value: " + charValue);
// 8. boolean: represents one bit of information, either true or false
boolean booleanValue = true;
System.out.println("Boolean value: " + booleanValue);
}
}
OUTPUT:
NOTE: If you assign values outside the range of a primitive data type in Java, it can lead to different results, including compilation errors, unexpected behavior, or runtime exceptions
What’s next?
So, today we learned about Data Types and there is much more to come. In the next part, we’ll learn Conditional Statements which helps in decision making.
So, Stay tuned!!
PS: I am also learning Java while teaching it. I believe in continuous growth and am committed to learning as much as I am teaching. If you have any questions, suggestions, or just want to chat about Java, please leave a comment! I'm also new to blogging, so any feedback is appreciated as I strive to improve. Thank you for joining me on this journey! Let's learn and grow together.