Welcome to The Coding College, your trusted resource for mastering programming concepts! In this tutorial, we’ll explore Java Data Types, one of the core aspects of Java programming. By understanding data types, you can store, manipulate, and operate on data efficiently in your programs.
What Are Data Types in Java?
In Java, data types specify the type of data that a variable can hold. They are categorized into two main groups:
- Primitive Data Types: Predefined by the language (e.g.,
int
,char
). - Non-Primitive Data Types: User-defined or derived types (e.g.,
String
,Array
).
1. Primitive Data Types
Java has 8 primitive data types:
Data Type | Description | Size | Example |
---|---|---|---|
byte | Integer (-128 to 127) | 1 byte | byte b = 100; |
short | Integer (-32,768 to 32,767) | 2 bytes | short s = 5000; |
int | Integer (-2^31 to 2^31-1) | 4 bytes | int num = 100000; |
long | Large integer (-2^63 to 2^63-1) | 8 bytes | long l = 100000L; |
float | Decimal (up to 7 digits) | 4 bytes | float f = 10.5f; |
double | Decimal (up to 15 digits) | 8 bytes | double d = 99.99; |
char | Single character | 2 bytes | char c = 'A'; |
boolean | True or false | 1 bit | boolean b = true; |
Example: Using Primitive Data Types
public class PrimitiveDataTypes {
public static void main(String[] args) {
byte age = 25;
int salary = 50000;
double height = 5.9;
char grade = 'A';
boolean isPassed = true;
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
System.out.println("Height: " + height);
System.out.println("Grade: " + grade);
System.out.println("Passed: " + isPassed);
}
}
Output:
Age: 25
Salary: 50000
Height: 5.9
Grade: A
Passed: true
2. Non-Primitive Data Types
Non-primitive data types are created by the programmer. They include:
- String: Represents a sequence of characters.
- Array: Stores multiple values of the same type.
- Classes: User-defined blueprints for objects.
- Interfaces: Define methods that a class must implement.
Example: Using Non-Primitive Data Types
public class NonPrimitiveDataTypes {
public static void main(String[] args) {
String name = "The Coding College";
int[] numbers = {1, 2, 3, 4, 5}; // Array
System.out.println("Name: " + name);
System.out.print("Numbers: ");
for (int num : numbers) {
System.out.print(num + " ");
}
}
}
Output:
Name: The Coding College
Numbers: 1 2 3 4 5
Key Differences: Primitive vs Non-Primitive
Feature | Primitive | Non-Primitive |
---|---|---|
Predefined/Custom | Predefined by Java | Created by programmers |
Stored in Memory | Stored directly in memory | Stores reference to the memory |
Example | int , char , boolean | String , Array , Class |
Null Allowed | Cannot be null | Can be null |
Type Conversion in Java
1. Implicit Type Casting (Widening Conversion)
Java automatically converts smaller types to larger types.
Example:
int num = 10;
double decimal = num; // int to double
System.out.println("Decimal: " + decimal);
2. Explicit Type Casting (Narrowing Conversion)
Larger types can be converted to smaller types manually.
Example:
double decimal = 99.99;
int num = (int) decimal; // double to int
System.out.println("Number: " + num);
Practice Exercise
- Declare variables for all primitive data types and print their default values.
- Write a program to store and print the names of 5 programming languages in an array.
- Perform type conversions between
int
,double
, andfloat
.
Conclusion
Understanding Java data types helps you manage memory and perform operations effectively. Both primitive and non-primitive types have their uses, and mastering them is essential for becoming a proficient Java programmer.
For more tutorials, visit TheCodingCollege.com, where learning is tailored for your success. Keep coding and growing! 🚀