DEV Community

Tamilselvan K
Tamilselvan K

Posted on

Day-83 Understanding Type Casting and Comparable in Java

1. Type Casting in Java

Type casting is the process of converting one data type into another. In Java, type casting is of two types:

a) Widening (Upcasting)

  • Converting a smaller data type into a larger data type.
  • Happens automatically (implicit casting).

Example (Primitive):

int myInt = 9;
double myDouble = myInt;  // int is widened to double
System.out.println(myDouble);  // Output: 9.0
Enter fullscreen mode Exit fullscreen mode

Example (Non-Primitive / Objects):

class Animal {}
class Dog extends Animal {}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();  // Upcasting: Dog → Animal
    }
}
Enter fullscreen mode Exit fullscreen mode

-

b) Narrowing (Downcasting)

  • Converting a larger data type into a smaller data type.
  • Must be done manually (explicit casting).

Example (Primitive):

double i = 10.5;
int j = (int) i;   // explicit narrowing
System.out.println(j);  // Output: 10
Enter fullscreen mode Exit fullscreen mode

Example (Non-Primitive / Objects):

class Animal {}
class Dog extends Animal {}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        Dog d = (Dog) a;  // Downcasting: Animal → Dog
    }
}
Enter fullscreen mode Exit fullscreen mode

2. The Comparable Interface

When we need to sort or compare objects in Java (like numbers, strings, or custom classes), the Comparable interface helps us define the natural ordering.

Key Points:

  • Defined in java.lang package
  • It has a single method:
  int compareTo(Object obj);
Enter fullscreen mode Exit fullscreen mode
  • Returns:

    • positive → current object is greater
    • negative → current object is smaller
    • zero → both objects are equal
  • Used for single sorting criteria (e.g., price, name, age).

Example: Comparing Mobiles by Price

package objectLearn;

public class Mobiles implements Comparable<Mobiles> {
    int price, ram, mp;

    public Mobiles(int price, int ram, int mp) {
        this.price = price;
        this.ram = ram;
        this.mp = mp;
    }

    public static void main(String[] args) {
        Mobiles oppo = new Mobiles(12000, 8, 50);
        Mobiles vivo = new Mobiles(10000, 12, 64);

        int res = oppo.compareTo(vivo);  // Compare using compareTo()

        if (res > 0) {
            System.out.println("Oppo is costlier than Vivo");
        } else if (res < 0) {
            System.out.println("Oppo is cheaper than Vivo");
        } else {
            System.out.println("Both have same price");
        }
    }

    // Compare based on price
    public int compareTo(Mobiles obj) {
        if (this.price > obj.price) {
            return 1;
        } else if (this.price < obj.price) {
            return -1;
        }
        return 0;
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Oppo is costlier than Vivo
Enter fullscreen mode Exit fullscreen mode

Top comments (0)