In: Computer Science
Write java statements that compare objects, obj1 and obj2, using the getClass() method.
import java.io.*;
public class Main
{
public static void main(String[] args) {
try {
// Create object1
Writer writer1 = new OutputStreamWriter(System.out);
//create object2
Writer writer2 = new OutputStreamWriter(System.out);
// Get the String to be written in the stream
String string1 = "first";
String string2 = "second";
// Write the the string to this writer using write() method
writer1.write(string1);
writer2.write(string2);
// Compare both objects using getClass()
if (writer1.getClass() == writer2.getClass())
{
System.out.println("Both have the same type");
}
else
System.out.println("Both have different type");
}
catch (Exception e) {
System.out.println(e);
}
}
}