Re: Why is this happening?
you must use the method "Object.equals" istead of "=="
becuase "==" compares values to references
Re: Why is this happening?
You could also do something like this :
Code:
String classname=new String(P.getClass().getName());
if (classname.compareTo("Student")==0) {
...
Re: Why is this happening?
Quote:
Originally Posted by manavo11
You could also do something like this :
Code:
String classname=new String(P.getClass().getName());
if (classname.compareTo("Student")==0) {
...
There are so many ways to do this but yours is the worst
Congratulations on the promotion by the way
Re: Why is this happening?
Quote:
Originally Posted by ComputerJy
There are so many ways to do this but yours is the worst
Congratulations on the promotion by the way
Where did I say that it's the best way to do it? All I know is that it will work, I'm sure there are better ways to do it. Why is it the worst? And what promotion?
Re: Why is this happening?
Quote:
Originally Posted by manavo11
You could also do something like this :
Code:
String classname=new String(P.getClass().getName());
if (classname.compareTo("Student")==0) {
...
Looks good to me. You could actually override the compareTo() method and make it even more powerful in this case (in my opinion). It returns an int so you could deal with several different cases and it can also raise error messages that .equals() would simply shun aside by returning false.... I don't know if that's a plus or a minus ;)
Re: Why is this happening?
Quote:
Originally Posted by manavo11
Where did I say that it's the best way to do it? All I know is that it will work, I'm sure there are better ways to do it. Why is it the worst?
I know you didn't say, I just notified you it's the worst way
because your not considering Time :confused: (Added unnecessary operations) and Space :confused: (Defined unnecessary variables)