Data Types and Variables
Duration: 5 min
What are Data Types?
A data type specifies the type of data a variable can hold. Java is a strongly-typed language, meaning every variable must have a declared type. This helps catch errors early and makes code more predictable.
Primitive Data Types
Java has 8 primitive data types:
• byte: 8-bit integer (-128 to 127)
• short: 16-bit integer (-32,768 to 32,767)
• int: 32-bit integer (most common for whole numbers)
• long: 64-bit integer (for very large numbers)
• float: 32-bit decimal number
• double: 64-bit decimal number (more precise than float)
• char: Single character (e.g., 'A', '5')
• boolean: true or false
public class PrimitiveTypes {
public static void main(String[] args) {
// Integer types
byte age = 25;
short population = 5000;
int salary = 50000;
long distance = 1000000000L; // L suffix for long
// Floating-point types
float price = 19.99f; // f suffix for float
double pi = 3.14159265359;
// Character and boolean
char grade = 'A';
boolean isJavaFun = true;
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
System.out.println("Pi: " + pi);
System.out.println("Grade: " + grade);
System.out.println("Is Java fun? " + isJavaFun);
}
}Age: 25
Salary: 50000
Pi: 3.14159265359
Grade: A
Is Java fun? trueReference Data Types
Reference types store references (memory addresses) to objects, not the actual values. The main reference types are:
• String: Text data
• Arrays: Collections of values
• Classes: Custom objects
• Interfaces: Contracts for classes
Unlike primitives, reference types can be null (no value).
public class ReferenceTypes {
public static void main(String[] args) {
// String - reference type
String name = "Alice";
String message = "Hello, " + name;
// Arrays - reference type
int[] numbers = {1, 2, 3, 4, 5};
String[] fruits = {"Apple", "Banana", "Orange"};
System.out.println(message);
System.out.println("First number: " + numbers[0]);
System.out.println("First fruit: " + fruits[0]);
}
}Hello, Alice
First number: 1
First fruit: AppleType Casting
Type casting is converting one data type to another. There are two types:
• Widening (automatic): Converting a smaller type to a larger type (int to long)
• Narrowing (manual): Converting a larger type to a smaller type (double to int) - requires explicit casting
public class TypeCasting {
public static void main(String[] args) {
// Widening - automatic
int intValue = 100;
long longValue = intValue; // No casting needed
System.out.println("Int to Long: " + longValue);
// Narrowing - requires explicit casting
double doubleValue = 99.99;
int intFromDouble = (int) doubleValue; // Explicit cast
System.out.println("Double to Int: " + intFromDouble);
// String to number
String numberString = "42";
int number = Integer.parseInt(numberString);
System.out.println("String to Int: " + number);
}
}Int to Long: 100
Double to Int: 99
String to Int: 42💡 Tip: When narrowing, data may be lost. For example, converting 99.99 to int gives 99 (decimal part is truncated).
Learn more: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
❓ Which primitive data type should you use for storing decimal numbers with high precision?
❓ What happens when you cast a double value 45.99 to an int?
💡 Tip: Pro Tip: Master Data Types and Variables thoroughly. This foundation is crucial for writing professional Java code.