• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getClass() and enums

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.The result of

List <String> l1 = new ArrayList<String>();
List<Integer> l2 = new ArrayList<Integer>();
System.out.println(l1.getClass() == l2.getClass());

is "true".

2. the result of

enum Operation {PLUS,MINUS,TIMES,DIVIDE }

public static void main(String args[]){
System.out.print(Operation.PLUS.getClass() == Operation.MINUS.getClass());
System.out.println(Operation.PLUS.getClass().equals(Operation.MINUS.getClass()));
}

is "falsefalse"

Can some one explain how we are getting the results 'true' in first case and 'falsefalse' in second case ?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try again, I'm getting "truetrue"

import java.util.*;

class Test {
enum Operation {PLUS,MINUS,TIMES,DIVIDE }
public static void main(String[] args) {
System.out.print(Operation.PLUS.getClass() == Operation.MINUS.getClass());
System.out.println(Operation.PLUS.getClass().equals
Operation.MINUS.getClass()));
}
}
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, (like Wes I see) I get truetrue in the second case. Are you sure you aren't running code a little different than what you showed? In particular, have you perhaps omitted some extra details in the Operation definition?

I recommend you try running this:

And after that, replace the Operation definition with
to see what difference it makes.
[ November 26, 2006: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the enum Operation { PLUS, MINUS, TIMES, DIVIDE } is set like this I get the below output:

PLUS class: class scjp.GetClass$Operation
MINUS class: class scjp.GetClass$Operation
true
true

When the enum Operation { PLUS {}, MINUS {}, TIMES {}, DIVIDE {} } is set like this I get the below output:

PLUS class: class scjp.GetClass$Operation$1
MINUS class: class scjp.GetClass$Operation$2
false
false

I see that the difference is the class. In the first case the class is Operation but in the second case the class is Operation.1 and Operation.2. Why is that?

Thanks
 
Reghuram Variyam
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct. I tried it out and I got truetrue as the answer. I got this question from a mock exam. It looks like the answer they provided is not correct. Thanks for the reply..
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Who are "they"? Please always mention the source of any mock exam question you post.
[ November 27, 2006: Message edited by: Jim Yingst ]
 
Reghuram Variyam
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://home.wanadoo.nl/mrzljak/en/biz/exam.html

Questio 12 on scjp 5
[ November 27, 2006: Message edited by: Reghuram Variyam ]
 
Greg L Tonn
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me what the difference is between these two enums?




I know that they provide different output when you print getClass() for a specific element (see my post above) but I'm not sure why?

Thanks
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the second case, each enum instance is actually an anonymous subclass instance - all different. It's a bit silly to do this here, since none of those anonymous subclasses have any useful functionality. But it's possible to imagine a more useful, complex version, such as this:

Here, again, each enum instance is a different anonymous subclass. with a different implementation of perform().
[ November 28, 2006: Message edited by: Jim Yingst ]
 
Greg L Tonn
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim. I was thinking they were classes but it wasn't clear to me how it was all working since the output to the getClass() was Operation.1 and so on.

Thanks again and do you or anyone else know if that sort of thing will be on the exam?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you probably need to be able to recognize the syntax for defining methods for an enum, which is essentially the same syntax as for anonymous classes. But note the use of commas and semicolon, and the fact that the abstract method declaration must come after the instance definitions, because the list of instance definitions must be the first thing inside the enum. I doubt you need to knowhow to figure the exact class names for the enum instances. (Class names of the form OuterClassName$1, OuterClassName$2 are typical for anonymous classes.)
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic