Java Multicasting (Typecasting multiple times) Puzzle


Introduction

Java Multicasting known as typecasting multiple times. It is the process of converting a value from one data type to another and it involves multiple typecasting conversions. It allows us to change the data type of a variable to perform operations that would not be possible otherwise.

Let us discuss multicasting in Java. We will talk about numeric multicasting to convert between numeric data types. We will also discuss object multicasting to treat subclass objects as superclass objects. We will also provide simple programming examples to understand this topic easily.

Numeric Multicasting in Java

In this programming example we will see the numeric multicasting in java.

Example

public class first 
{
   public static void main (String args [])
   {
      // First we will do type cast int to byte
      int num1 = -1;
      byte num2 = (byte) num1;
      System.out.println ("Step 1 - int to byte:");
      System.out.println ("num1 (int) = " + num1);
      System.out.println ("num2 (byte) = " + num2);
      
      // second we will typecast byte to char
      char char1 = (char) num2;
      System.out.println("\nStep 2 - byte to char:");
      System.out.println("char1 (char) = " + (int) char1);
      
      // then we will typecast char to int
      int num3 = char1;
      System.out.println("\nStep 3 - char to int:");
      System.out.println("num3 (int) = " + num3);
      
      // then we will typecast int to short
      short num4 = (short) num3;
      System.out.println("\nStep 4 - int to short:");
      System.out.println("num4 (short) = " + num4);    
      
      // we do casting short to byte
      byte num5 = (byte) num4;
      System.out.println("\nStep 5 - short to byte:");
      System.out.println("num5 (byte) = " + num5);
      
      // Casting byte to long
      long num6 = (long) num5;
      System.out.println ("\nStep 6 - byte to long:");
      System.out.println ("num6 (long) = " + num6);
      
      // Casting long to double
      double num7 = (double) num6;
      System.out.println("\nStep 7 - long to double:");
      System.out.println("num7 (double) = " + num7);
   }
}

Output

Step 1 - int to byte:
num1 (int) = -1
num2 (byte) = -1

Step 2 - byte to char:
char1 (char) = 65535

Step 3 - char to int:
num3 (int) = 65535

Step 4 - int to short:
num4 (short) = -1

Step 5 - short to byte:
num5 (byte) = -1

Step 6 - byte to long:
num6 (long) = -1

Step 7 - long to double:
num7 (double) = -1.0

To do this at first, we have defined a class named first and inside this class we have called the main () function. Inside the main () function we have to type cast integer to byte. For this we have declared two variables named “num1” and “num2” respectively. “num1” is an integer type and “num2” is a byte type variable. -1 is assigned to the variable “num1” and in the next line we have type cast of “num1” to the byte type.

int num1 = -1;
byte num2 = (byte) num1;

Then we simply printed this value.

System.out.println ("Step 1 - int to byte:");
System.out.println ("num1 (int) = " + num1);
System.out.println ("num2 (byte) = " + num2);

After that we have type cast a character to an integer. For this we have declared a character named “char1” and typecast byte to char.

char char1 = (char) num2;

Then we simply printed this type casting value.

System.out.println("\nStep 2 - byte to char:");
System.out.println("char1 (char) = " + (int) char1 ");

After that we have type cast an integer to a short. For this we have declared an integer named “num4” and typecast integer to short.

short num4 = (short) num3;

Then we have simply printed this type casting value.

System.out.println("\nStep 4 - int to short:");
System.out.println("num4 (short) = " + num4);

After that we have type casted a short to a byte. For this we have declared a byte named “num5” and typecast short to byte.

byte num5 = (byte) num4;

Then we have simply printed this type casting value.

System.out.println("\nStep 5 - short to byte:");
System.out.println("num5 (byte) = " + num5);

After that we have type cast a short to a byte. For this we have declared a byte named “num5” and typecast short to byte.

byte num5 = (byte) num4;

Then we have simply print this type casting value.

System.out.println("\nStep 5 - short to byte:");
System.out.println("num5 (byte) = " + num5);

After that we have type casted a byte to a long. For this we have declare a long named “num6” and typecast short to byte.

long num6 = (long) num5;

Then we have simply printed this type casting value.

System.out.println ("\nStep 6 - byte to long:");
System.out.println ("num6 (long) = " + num6);

After that we have type cast a long to a double. For this we have declared a double named “num6” and typecast long to double.

double num7 = (double) num6;

Then we have simply printed this type casting value.

System.out.println("\nStep 7 - long to double:");
System.out.println("num7 (double) = " + num7

Then we have got the resulted output on our screen.

Object Multicasting in Java

In this programming example we will see the object multicasting in Java.

Example

class Zoo
{
   void makeSound()
   {
      System.out.println("Some random noise");
   }
}
class Dog extends Zoo 
{
   void makeSound() 
   {
      System.out.println("Bark!!");
   }
}
class Cat extends Zoo
{
   void makeSound() 
   {
      System.out.println("Meow!!");
   }
}
class Tiger extends Zoo 
{
   void makeSound() 
   {
      System.out.println("Roar!!");
   }
}
public class MulticastPuzz
{
   public static void main(String args [])
   {
      // Object multicasting using superclass reference
      Zoo zoo1 = new Dog();
      Zoo zoo2 = new Cat();
      Zoo zoo3 = new Tiger();
      
      // by using polymorphism we access subclass methods 
      Dog dog = (Dog) zoo1;
      dog.makeSound();
      ((Cat) zoo2).makeSound();
      ((Tiger) zoo3).makeSound();
      
      // Object multicasting
      Zoo[] zoos = new Zoo[3];
      zoos[0] = new Dog();
      zoos[1] = new Cat();
      zoos[2] = new Tiger();
   }
}

Output

Bark!!
Meow!!
Roar!!

To do this at first, we have defined a class named Zoo and inside this class we have called the makeSound () function. Now a class dog extends the class zoo. Inside the dog class we have called the makeSound () by the properties of inheritance.

class Dog extends Zoo 
{
   void makeSound() 
   {
      System.out.println("Bark!!");
   }

Now a class cat extends the class zoo. Inside the dog class we have called the makeSound () by the properties of inheritance

class Cat extends Zoo 
{
   void makeSound() 
   {
      System.out.println("Meow!!");
   }

Now a class tiger extends the class zoo. Inside the tiger class we have called the makeSound () by the properties of inheritance.

class Tiger extends Zoo 
{
   void makeSound() 
   {
      System.out.println("Roar!!”);
   }
}

Now a class tiger extends the class zoo. Inside the tiger class we have called the makeSound () by the properties of inheritance.

class Tiger extends Zoo 
{
   void makeSound() 
   {
      System.out.println("Roar!!”);
   }
}

Now we have defined a class named MultiCastPuzz and inside this class we have called the main () function. Inside the main () function we have Object multicasting using superclass reference.

Zoo zoo1 = new Dog();
Zoo zoo2 = new Cat();
Zoo zoo3 = new Tiger();

After that // by using polymorphism we access subclass methods.

dog.makeSound();
((Cat) zoo2).makeSound();
((Tiger) zoo3).makeSound();

Finally, we have multicast the object.

Zoo[] zoos = new Zoo[3];
zoos[0] = new Dog();
zoos[1] = new Cat();
zoos[2] = new Tiger();

Conclusion

In this article we learned a lot about how to multicast in Java or type casting in Java multiple times as you wish. This procedure helps the coder to reduce its energy and writing of codes. It is a unique feature of Java that makes it a smarter language compare to other language that does not support this kind of mechanism.

Updated on: 04-Oct-2023

82 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements