Data Types in Java
In Java, data types specify what kind of data a variable can hold. Java provides two types of data types: **primitive** and **non-primitive**.
1. Primitive Data Types
Primitive data types are the most basic data types in Java. They are predefined by Java and represent simple values. There are 8 primitive data types in Java:
Data Type | Size | Default Value | Range |
---|---|---|---|
byte | 1 byte | 0 | -128 to 127 |
short | 2 bytes | 0 | -32,768 to 32,767 |
int | 4 bytes | 0 | -2^31 to 2^31-1 |
long | 8 bytes | 0L | -2^63 to 2^63-1 |
float | 4 bytes | 0.0f | -3.4e38 to 3.4e38 |
double | 8 bytes | 0.0d | -1.7e308 to 1.7e308 |
char | 2 bytes | '\u0000' | 0 to 65,535 (Unicode characters) |
boolean | 1 bit | false | true or false |
2. Non-Primitive Data Types
Non-primitive data types are more complex and are objects in Java. Unlike primitive data types, non-primitive types can store multiple values and come with built-in methods. Some examples of non-primitive types include:
- String - A sequence of characters. E.g.,
String name = "John";
- Array - A collection of similar types of elements. E.g.,
int[] numbers = {1, 2, 3};
- Class - Used to define the blueprint of objects. E.g.,
class Car { int speed; }
- Interface - An abstract type used to define a contract for classes. E.g.,
interface Drive { void drive(); }
3. Wrapper Classes
Wrapper classes are the object representation of primitive data types. Each primitive type has a corresponding wrapper class in Java. These wrapper classes provide useful methods for converting between types, comparing values, etc. The wrapper classes are part of the java.lang package.
Primitive Type | Wrapper Class |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Example of Using All Data Types in One Program
public class AllDataTypesExample {
public static void main(String[] args) {
// Primitive types
byte b = 100;
short s = 2000;
int i = 100000;
long l = 1000000000L;
float f = 3.14f;
double d = 3.14159;
char c = 'A';
boolean bool = true;
// Wrapper classes
Byte bWrapper = b;
Short sWrapper = s;
Integer iWrapper = i;
Long lWrapper = l;
Float fWrapper = f;
Double dWrapper = d;
Character cWrapper = c;
Boolean boolWrapper = bool;
// Displaying values
System.out.println("Byte: " + b);
System.out.println("Short: " + s);
System.out.println("Int: " + i);
System.out.println("Long: " + l);
System.out.println("Float: " + f);
System.out.println("Double: " + d);
System.out.println("Char: " + c);
System.out.println("Boolean: " + bool);
System.out.println("\nWrapper class values:");
System.out.println("Byte Wrapper: " + bWrapper);
System.out.println("Short Wrapper: " + sWrapper);
System.out.println("Integer Wrapper: " + iWrapper);
System.out.println("Long Wrapper: " + lWrapper);
System.out.println("Float Wrapper: " + fWrapper);
System.out.println("Double Wrapper: " + dWrapper);
System.out.println("Character Wrapper: " + cWrapper);
System.out.println("Boolean Wrapper: " + boolWrapper);
}
}
4. Program to Get Range of Data Types in Java
The following program will print the range of various primitive data types in Java:
public class DataTypesRange {
public static void main(String[] args) {
System.out.println("Byte range: " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
System.out.println("Short range: " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
System.out.println("Integer range: " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
System.out.println("Long range: " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
System.out.println("Float range: " + Float.MIN_VALUE + " to " + Float.MAX_VALUE);
System.out.println("Double range: " + Double.MIN_VALUE + " to " + Double.MAX_VALUE);
System.out.println("Character range: " + (int) Character.MIN_VALUE + " to " + (int) Character.MAX_VALUE);
System.out.println("Boolean: " + Boolean.FALSE + " or " + Boolean.TRUE);
}
}
This program uses Java's predefined constants to print the minimum and maximum values for primitive data types.
Data Types and Type Conversions in Java
Java provides different ways of converting between primitive types, wrapper classes, and other types. Let's go through some of the common conversions.
1. Primitive to Wrapper Conversion (Autoboxing)
Autoboxing is the automatic conversion of primitive types to their corresponding wrapper classes. Java handles this internally when you assign a primitive value to a wrapper class object.
public class PrimitiveToWrapper {
public static void main(String[] args) {
int i = 10;
// Primitive to Wrapper (Autoboxing)
Integer integerObj = i; // Autoboxing
System.out.println("Primitive to Wrapper: " + integerObj);
}
}
2. Wrapper to Primitive Conversion (Unboxing)
Unboxing is the automatic conversion of wrapper class objects to their corresponding primitive types.
public class WrapperToPrimitive {
public static void main(String[] args) {
Integer integerObj = 100;
// Wrapper to Primitive (Unboxing)
int i = integerObj; // Unboxing
System.out.println("Wrapper to Primitive: " + i);
}
}
3. String to Wrapper Conversion
To convert a String to a wrapper type, you can use the valueOf() method of the wrapper class.
public class StringToWrapper {
public static void main(String[] args) {
String str = "123";
// String to Wrapper using valueOf()
Integer integerObj = Integer.valueOf(str);
System.out.println("String to Wrapper: " + integerObj);
}
}
4. Wrapper to String Conversion
To convert a wrapper object to a String, you can call the toString() method on the wrapper object.
public class WrapperToString {
public static void main(String[] args) {
Integer integerObj = 100;
// Wrapper to String
String str = integerObj.toString();
System.out.println("Wrapper to String: " + str);
}
}
5. Primitive to String Conversion
To convert a primitive type to a String, you can use the String.valueOf() method or simply concatenate the primitive value with an empty string.
public class PrimitiveToString {
public static void main(String[] args) {
int i = 10;
// Primitive to String
String str = String.valueOf(i);
System.out.println("Primitive to String: " + str);
}
}
6. String to Primitive Conversion
To convert a String to a primitive type, you can use the parse
method of the corresponding wrapper class.
public class StringToPrimitive {
public static void main(String[] args) {
String str = "123";
// String to Primitive using parse()
int i = Integer.parseInt(str);
System.out.println("String to Primitive: " + i);
}
}
7. Summary Table of Conversion Methods
Conversion | Method/Approach | Example |
---|---|---|
Primitive to Wrapper | Autoboxing (e.g., Integer i = 10; ) |
int i = 10; Integer integerObj = i; |
Wrapper to Primitive | Unboxing (e.g., int i = integerObj; ) |
Integer integerObj = 100; int i = integerObj; |
String to Wrapper | Wrapper.valueOf(String) |
Integer integerObj = Integer.valueOf("123"); |
Wrapper to String | Wrapper.toString() |
String str = integerObj.toString(); |
Primitive to String | String.valueOf(primitive) |
String str = String.valueOf(10); |
String to Primitive | parse |
int i = Integer.parseInt("123"); |
Type Casting in Java
Type casting is the process of converting one data type to another. In Java, there are two main types of type casting:
- Implicit Casting (Widening): Automatically performed by Java when a smaller type is converted into a larger type.
- Explicit Casting (Narrowing): Done manually by the programmer when a larger type is converted into a smaller type.
1. Implicit Casting (Widening)
Implicit casting happens when you assign a smaller data type to a larger data type. This type of casting is done automatically by the Java compiler because there is no risk of losing data.
public class ImplicitCasting {
public static void main(String[] args) {
int intVar = 100;
// Implicit casting: int to long
long longVar = intVar;
System.out.println("Implicit Casting (int to long): " + longVar);
}
}
2. Explicit Casting (Narrowing)
Explicit casting requires the programmer to manually cast a larger type to a smaller type. Since data might be lost in this type of casting, Java requires explicit casting using parentheses.
public class ExplicitCasting {
public static void main(String[] args) {
double doubleVar = 10.75;
// Explicit casting: double to int
int intVar = (int) doubleVar;
System.out.println("Explicit Casting (double to int): " + intVar);
}
}
3. Upcasting and Downcasting (Object Casting)
Upcasting and downcasting involve casting between class hierarchies. Upcasting occurs when a subclass object is assigned to a superclass reference, and downcasting occurs when a superclass reference is cast back to a subclass object.
Upcasting
Upcasting is the process of assigning a subclass object to a superclass reference variable. It is done implicitly and does not require explicit casting.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class UpcastingExample {
public static void main(String[] args) {
Animal animal = new Dog(); // Upcasting
animal.sound(); // Calls Dog's sound method
}
}
Downcasting
Downcasting is the process of converting a superclass reference back into a subclass object. This requires explicit casting and can throw a ClassCastException
if not done carefully.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class DowncastingExample {
public static void main(String[] args) {
Animal animal = new Dog(); // Upcasting
Dog dog = (Dog) animal; // Downcasting (Explicit casting)
dog.sound(); // Calls Dog's sound method
}
}
4. Summary Table of Type Casting in Java
Type of Casting | Description | Example |
---|---|---|
Implicit Casting (Widening) | Automatically performed when a smaller type is assigned to a larger type. | int x = 5; long y = x; |
Explicit Casting (Narrowing) | Requires explicit casting when a larger type is assigned to a smaller type. | double x = 10.5; int y = (int) x; |
Upcasting | Assigning a subclass object to a superclass reference variable (implicit). | Dog dog = new Dog(); Animal animal = dog; |
Downcasting | Assigning a superclass reference variable back to a subclass object (explicit). | Animal animal = new Dog(); Dog dog = (Dog) animal; |