Converting Values in Java

In Java, you can convert the value of one type to another type using type casting.

Type casting is the process of converting a value from one type to another. In Java, you can use type casting to convert a value from one primitive type to another, or to convert a value from a class type to a superclass or subclass type.

Primitive Type Casting:

Java allows you to convert values from one primitive type to another using type casting. You can use type casting to convert a value from a larger type to a smaller type, or from a smaller type to a larger type.

To convert a value from a larger type to a smaller type, you can use a narrowing primitive conversion. A narrowing primitive conversion involves losing precision or range, and it requires an explicit cast. For example, you can use a narrowing primitive conversion to convert a long value to an int value, or a double value to a float value.

long l = 100000L;
int i = (int) l; // i will be 100000

double d = 3.141592653589793238;
float f = (float) d; // f will be 3.1415927

To convert a value from a smaller type to a larger type, you can use a widening primitive conversion. A widening primitive conversion does not involve any loss of precision or range, and it does not require an explicit cast. For example, you can use a widening primitive conversion to convert an int value to a long value, or a float value to a double value.

int i = 100;
long l = i; // l will be 100

float f = 3.14f;
double d = f; // d will be 3.14

It's important to note that type casting can result in loss of precision or data, depending on the types involved and the values being converted. For example, converting a large long value to an int will result in the loss of the most significant bits, and converting a floating-point value to an integer will truncate the fractional part.

Class Type Casting:

In addition to converting values between primitive types, you can also use type casting to convert a value from a class type to a superclass or subclass type.

When you cast an object from a subclass type to a superclass type, it is called an upcast. Upcasting is always safe, because a subclass is always a type of its superclass. For example, you can upcast an object of type Dog (a subclass of Animal) to Animal (the superclass).

Animal a = new Dog(); // upcast

When you cast an object from a superclass type to a subclass type, it is called a downcast. Downcasting is not always safe, because an object of a superclass type may not necessarily be an instance of the subclass. For example, if you downcast an object of type Animal (a superclass) to Dog (a subclass), it will result in a ClassCastException if the object is not actually an instance of Dog.

Animal a = new Animal();
Dog d = (Dog) a; // ClassCastException

To avoid a ClassCastException when casting an object from a superclass type to a subclass type in Java, you can use the instanceof operator to check if the object is an instance of the target subclass before attempting the cast.

Here's an example of how to use the instanceof operator to avoid a ClassCastException:

Animal a = new Animal();

if (a instanceof Dog) {
    Dog d = (Dog) a;
    // do something with d
} else {
    // a is not an instance of Dog
}

The instanceof operator returns true if the object being tested is an instance of the specified class, and false otherwise. By using the instanceof operator, you can ensure that the object being cast is actually an instance of the target subclass, and avoid a ClassCastException.

It's important to note that the instanceof operator only works for objects, and it cannot be used with primitive types. To test the type of a primitive value, you can use the typeof operator, which is similar to the instanceof operator but works with primitive types.

int i = 100;
if (i instanceof Integer) { // invalid, cannot use instanceof with primitive types
    Integer ii = (Integer) i; // ClassCastException
}