Java Data Types: A Beginner's Guide to Understanding Primitives and References
Satish Jassal
?? Full Stack Engineer ? | Elevate your digital presence! Crafting brilliance for portfolios, e-commerce, podcasts, events & custom web apps. ???? #nextjs #nodejs #innovation
Byte: Storing a person's age in a school application.
byte age = 25
Short: Representing the number of students in a class.
short temperature = -10;
short numberOfStudents = 2500;
short yearOfBirth = 1995
Int: Storing the population of a city, where the count is within the range of the int data type.
int population = 1000000;
Long: Recording the distance traveled by a vehicle, where the distance can be very large.
long distance = 3000000000L;
Float: Storing the temperature readings in a weather application, where the data may have decimal points.
float weightKilograms = 2.5f;
float weightPounds = 150.7f;
float temperatureCelsius = 25.5f;
Double: Double is for super precise decimal numbers used in math, science, money, and important calculations.
double interestRate = 0.045; // 4.5% interest rat
double investmentReturn = 0.072; // 7.2% return on investment
double distanceInMeters = 458.92;
double atomicMass = 45.56;
Char: Storing a single character, such as a letter or digit, like 'A', 'b', '5'. '$', etc.
char grade = 'A';
char symbol = '$';
Boolean: Representing whether a light switch is turned on (true) or off (false).
oolean isRaining = true;
boolean isButtonClicked = false;
// Listen for button clicks and set isButtonClicked to true when clicked
Reference data types: Do not have a fixed size in terms of bits and bytes.
String: In real life, strings are widely used for handling names, messages, and textual data.
String name = "John";
// The reference variable 'name' points to a String object in memory.
String message = "Hello, World!";
/ The 'message' variable references a String object.
Arrays: Arrays used to store collections of elements of the same type. In real life, arrays can represent lists of items like student names or temperatures.
int[] studentGrades = {85, 92, 78, 90, 88};
// Each element in the array represents the grade of an individual student.
double[] dailyTemperatures = {25.5, 28.2, 26.8, 24.0, 27.3, 29.1, 30.5};
// Each element represents the temperature on a specific day.
String[] shoppingList = {"Milk", "Bread", "Eggs", "Fruits", "Vegetables"};
// used to represent a shopping list, where each element is an item to buy.
Date: Storing a specific date in a calendar application, like a birthday such as "1995-08-25".
import java.util.Date
Date currentDate = new Date();
These examples demonstrate the usage of both primitive and reference data types in Java. Primitive data types store values directly, while reference data types store references to objects or instances of classes.
Remember that the size of data types may vary depending on the system architecture, but the ranges and general concepts remain the same. These data types provide the flexibility to work with different types of data in Java and are essential for writing efficient and effective code.
About User-defined reference data types like class, object will discuss in another blog. Thats It For Now!
If its helpful like or comment for any suggestions.
If you have any query, please feel free to contact me.
Cheers ??.