A typical operation on a generic function - java

I was recently learning generics in Java and a question came in my mind that Python programmers can relate to. All I did was to create a generic Java function which should return a string and take two parameters, "p1 and p2" of different (or similar) generic types. But the tricky thing is I want to return the value of p1 if p1 is of type String, else a general String message... A simple Python implementation of this code will be:
def func1(a, b):
if type(a) == str:
return a
else:
return 'bye'
func1(2, 5) # Output: 'bye'
func1("hi", 5) # Output: 'hi'
I tried the same on a Java function, but it seems to give an error which by my observations can only be rectified if it is forcefully (rather unwantedly, since the obj1 is already checked for being a type of string) typecasted into String:
public static <T, T2> String func3(T obj1, T2 obj2) {
if (obj1.getClass().getSimpleName().equals("String")){
return obj1; // <--error // (String)obj1; <-- is working
}
else {
return "bye";
}
}
So, what is being wrong here? Is it somewhat related to the "ducktyping" feature of Python, and not possible in Java? Or am I using the wrong functions for getting the desired class name?

It is related to the fact that Python is dynamically typed.
Although you checked the type of obj1 here:
if (obj1.getClass().getSimpleName()=="String"){
The compiler forgets about this when it sees the return statement. It will insist that obj1 is of type T but not String, so obj1 can't be returned.
The solution for this is, as you pointed out, cast obj1 to String. Casting is like saying to the compiler, "I am sure this is a string!"
P.S.: You can check if an object is a string by simply doing:
if (obj1 instanceof String)

Usually in Java you can check the type of an object using instanceof:
someObject instanceof Type
is true if the object is a Type or a subclass of Type.
Otherwise, you can do something like this:
someObject.getClass().equals(Type.class)
is true only if the object is a Type
In your case you can use both:
obj1 instanceof String
or
obj1.getClass().equals(String.class)
But I suggest the first solution as in Java is the most used.
In your case I would write a method like this:
public static String func3(T obj1, T2 obj2) {
if(obj1 instanceof String) {
return (String)obj1;
}
else {
return "obj1 not a String";
}
}

Related

Finding out the type of a set [duplicate]

This question already has answers here:
Java generics type erasure: when and what happens?
(7 answers)
Closed 7 months ago.
Given a Set<X>, I want to write a method that acts differently depending on the class X is. In actuality, I'd have 2 cases: A or B.
The method looks like:
public<X> boolean myMethod(Set<X> mySet)
{
// if X is actually the class A
...
// if X is B
...
}
I am not sure how to check this. I tried X::getClass, but it wouldn't let me use equals method.
In a general sense, this is not possible
Due to type erasure, a Java Set does not have a generic type at runtime. Generics act as compile-time checks on the objects passed into methods, but consequently the specific values are not available at runtime. A Set<String> is the same class as a Set<Map<Integer, Thread>> and the same class as a bare Set.
Potential workaround with class parameter
If you really need to know this, you could change the signature of your method to be:
public <X> boolean myMethod(Set<X> mySet, Class<X> clazz)
Now you have a Class instance available at runtime, which is guaranteed to be compatible with X. This you can introspect (e.g. String.class.isAssignableFrom(clazz)).
There are two drawbacks to this approach: firstly, you'll have to explicitly pass in the extra parameter each time the method is called. Secondly, if A and B can be subclasses of one another, this is not going to work; you could pass in e.g. B.class even when the generic parameter was A.
However, this feels like a code smell
You shouldn't be trying to do fundamentally different things based on a generic parameter. Instead of switching behaviour in your method here, do something different within the classes A and B:
public<X> boolean myMethod(Set<X> mySet)
{
boolean result = false;
for (X x : mySet) {
result &= x.doSomething();
}
return result;
}
class A implements MyDoSomethingInterface {
public boolean doSomething() {
// Here you put the logic for "if X is A"
}
}
class B implements MyDoSomethingInterface {
public boolean doSomething() {
// Here you put the logic for "if X is B"
}
}
And if your reaction is that this won't work because A and B are built-in classes, you'll need to wrap them in your own domain objects (which is the correct approach anyway because you want to associate this extra behaviour with them).
I suggest you take one object from set and check class of single object
like below, please check
public<X> boolean myMethod(Set<X> mySet)
{
Object tmpObj = null;
for(Object obj : set){
tmpObj = obj;
break;
}
if(tmpObj instanceof A){
// if X is actually the class A
}else if(tmpObj instanceof B){
// // if X is B
}
}

How do I check if a given object is an instance of certain class when in Object[] array?

I was trying to find out how to determine which class a given object instantiate when it's in Object[] array. For example:
Object[] array = new Object[]{175, "sss", new Table(), true};
Object obj = array[0]; // hmm... can this be used as an integer or maybe as a string?
Is it even possible?
You can call getClass() to find out the class of a particular object, or you can use instanceof to check a specific type:
if (array[0] instanceof Integer) {
}
Normally having to do a lot of this indicates a weakness in your design though - you should try to avoid needing to do this.
You can try using instanceof or you can try getClass().isAssignableFrom(), whatever fits your needs
You can test whether it is an instance of a pre-known class (and cast it) like this:
if (obj instanceof String) {
String s = (String) obj; //casts the obj now you know it's a String
}
I like to think of this not as making any changes to the object but just as revealing its true character. For example, it's a bit like seeing a person and not knowing what language they speak - the person is still French, or Italian, just that you don't know which yet. The cast (i.e. (String) obj) is you telling the compiler the equivalent of "I know this person speaks French"
Or you can gets its class like this:
Class<?> clazz = obj.getClass();
A Class instance can be used to make the same check:
String.class.isInstance(obj) {
String s = String.class.cast(obj);
}

java why should equals method input parameter be Object

I'm going through a book on data structures. Currently I'm on graphs, and the below code is for the vertex part of the graph.
class Vertex<E>{
//bunch of methods
public boolean equals(Object o){
//some code
}
}
When I try to implement this equals method my compiler complains about not checking the type of the parameter and just allowing any object to be sent it. It also does seem a bit strange to me why that parameter shouldn't be a Vertex instead of an Object. Is there a reason why the author does this or is this some mistake or antiquated example?
#Override
public boolean equals(Object obj)
{
if (!(obj instanceof Vertex)) return false;
else return // blah blah
}
equals(Object) is the method defined in the root - Object. If you don't match the signature exactly, Object's version will be called when someone checks if two objects are equal. Not what you want.
You've probably seen other methods (like Comparator) where you can use the exact time. That's because those APIs were generic-ified with Java 5. Equals can't be because it is valid to call equals with two separate types. It should return false, but it is valid.
equals is a method inherited from Object, is defined to be flexible enough so that you can take any object and test if it is equal to any other object (as it rightfully should be able to do), so how could it be any other way?
Edit 1
Comment from jhlu87:
so is it not good form to write an equals method that has an input parameter of vertex?
You are welcome to create your own overload to any method, including equals, but doing so without changing the name could risk confusing many who would assume that your equals is the one that inherits from Object. If it were my code and I wanted a more specific equals method, I'd name it slightly different from just "equals" just to avoid confusion.
If your method doesn't take an argument of type Object, it isn't overriding the default version of equals but rather overloading it. When this happens, both versions exist and Java decides which one to use based on the variable type (not the actual object type) of the argument. Thus, this program:
public class Thing {
private int x;
public Thing(int x) {
this.x = x;
}
public boolean equals(Thing that) {
return this.x == that.x;
}
public static void main(String[] args) {
Thing a = new Thing(1);
Thing b = new Thing(1);
Object c = new Thing(1);
System.out.println(a.equals(b));
System.out.println(a.equals(c));
}
}
confusingly prints true for the first comparison (because b is of type Thing) and false for the second (because c is of type Object, even though it happens to contain a Thing).
It's because this method existed before generics, so for backward compatabitity it has to stay this way.
The standard workaround to impose type is:
return obj instanceof MyClass && <some condition>;
It is because the author is overriding equals. Equals is specified in java.lang.Object and is something that all classes inherrits from.
See the javadoc for java.lang.Object

Comparing Class Types in Java

I want to compare the class type in Java.
I thought I could do this:
class MyObject_1 {}
class MyObject_2 extends MyObject_1 {}
public boolean function(MyObject_1 obj) {
if(obj.getClass() == MyObject_2.class) System.out.println("true");
}
I wanted to compare in case if the obj passed into the function was extended from MyObject_1 or not.
But this doesn't work. It seems like the getClass() method and the .class gives different type of information.
How can I compare two class type, without having to create another dummy object just to compare the class type?
Try this:
MyObject obj = new MyObject();
if(obj instanceof MyObject){System.out.println("true");} //true
Because of inheritance this is valid for interfaces, too:
class Animal {}
class Dog extends Animal {}
Dog obj = new Dog();
Animal animal = new Dog();
if(obj instanceof Animal){System.out.println("true");} //true
if(animal instanceof Animal){System.out.println("true");} //true
if(animal instanceof Dog){System.out.println("true");} //true
For further reading on instanceof: http://mindprod.com/jgloss/instanceof.html
If you don't want to or can't use instanceof, then compare with equals:
if(obj.getClass().equals(MyObject.class)) System.out.println("true");
BTW - it's strange because the two Class instances in your statement really should be the same, at least in your example code. They may be different if:
the classes have the same short name but are defined in different packages
the classes have the same full name but are loaded by different classloaders.
It prints true on my machine. And it should, otherwise nothing in Java would work as expected. (This is explained in the JLS: 4.3.4 When Reference Types Are the Same)
Do you have multiple classloaders in place?
Ah, and in response to this comment:
I realise I have a typo in my
question. I should be like this:
MyImplementedObject obj = new MyImplementedObject ();
if(obj.getClass() == MyObjectInterface.class) System.out.println("true");
MyImplementedObject implements
MyObjectInterface So in other words, I
am comparing it with its implemented
objects.
OK, if you want to check that you can do either:
if(MyObjectInterface.class.isAssignableFrom(obj.getClass()))
or the much more concise
if(obj instanceof MyobjectInterface)
As said earlier, your code will work unless you have the same classes loaded on two different class loaders.
This might happen in case you need multiple versions of the same class in memory at the same time, or you are doing some weird on the fly compilation stuff (as I am).
In this case, if you want to consider these as the same class (which might be reasonable depending on the case), you can match their names to compare them.
public static boolean areClassesQuiteTheSame(Class<?> c1, Class<?> c2) {
// TODO handle nulls maybe?
return c1.getCanonicalName().equals(c2.getCanonicalName());
}
Keep in mind that this comparison will do just what it does: compare class names; I don't think you will be able to cast from one version of a class to the other, and before looking into reflection, you might want to make sure there's a good reason for your classloader mess.
Comparing an object with a class using instanceOf or ... is already answered.
If you have two objects and you want to compare their types with each other, you can use:
if (obj1.getClass() == obj2.getClass()) {
// Both have the same type
}
If you had two Strings and compared them using == by calling the getClass() method on them, it would return true. What you get is a reference on the same object.
This is because they are both references on the same class object. This is true for all classes in a java application.
Java only loads the class once, so you have only one instance of a given class at a given time.
String hello = "Hello";
String world = "world";
if (hello.getClass() == world.getClass()) {
System.out.println("true");
} // prints true
Hmmm... Keep in mind that Class may or may not implement equals() -- that is not required by the spec. For instance, HP Fortify will flag myClass.equals(myOtherClass).
Check Class.java source code for equals()
public boolean equals(Object obj) {
return this == obj;
}

Check if an object belongs to a class in Java [duplicate]

This question already has answers here:
How to determine an object's class?
(13 answers)
Closed 9 years ago.
Is there an easy way to verify that an object belongs to a given class? For example, I could do
if(a.getClass() = (new MyClass()).getClass())
{
//do something
}
but this requires instantiating a new object on the fly each time, only to discard it. Is there a better way to check that "a" belongs to the class "MyClass"?
The instanceof keyword, as described by the other answers, is usually what you would want.
Keep in mind that instanceof will return true for superclasses as well.
If you want to see if an object is a direct instance of a class, you could compare the class. You can get the class object of an instance via getClass(). And you can statically access a specific class via ClassName.class.
So for example:
if (a.getClass() == X.class) {
// do something
}
In the above example, the condition is true if a is an instance of X, but not if a is an instance of a subclass of X.
In comparison:
if (a instanceof X) {
// do something
}
In the instanceof example, the condition is true if a is an instance of X, or if a is an instance of a subclass of X.
Most of the time, instanceof is right.
If you ever need to do this dynamically, you can use the following:
boolean isInstance(Object object, Class<?> type) {
return type.isInstance(object);
}
You can get an instance of java.lang.Class by calling the instance method Object::getClass on any object (returns the Class which that object is an instance of), or you can use class literals (for example, String.class, List.class, int[].class). There are other ways as well, through the reflection API (which Class itself is the entry point for).
Use the instanceof operator:
if(a instanceof MyClass)
{
//do something
}
I agree with the use of instanceof already mentioned.
An additional benefit of using instanceof is that when used with a null reference instanceof of will return false, while a.getClass() would throw a NullPointerException.
Try operator instanceof.
The usual way would be:
if (a instanceof A)
However, there are cases when you can't do this, such as when A in a generic argument.
Due to Java's type erasure, the following won't compile:
<A> boolean someMethod(Object a) {
if (a instanceof A)
...
}
and the following won't work (and will produce an unchecked cast warning):
<A> void someMethod(Object a) {
try {
A casted = (A)a;
} catch (ClassCastException e) {
...
}
}
You can't cast to A at runtime, because at runtime, A is essentially Object.
The solutions to such cases is to use a Class instead of the generic argument:
void someMethod(Object a, Class<A> aClass) {
if (aClass.isInstance(a)) {
A casted = aClass.cast(a);
...
}
}
You can then call the method as:
someMethod(myInstance, MyClass.class);
someMethod(myInstance, OtherClass.class);

Categories