This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

compare two classes with .getSimpleName()

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a difference between this:




and that:



should I use getSimpleName to compare two classes?

 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getClass does a class comparison where as the other does a String comparison. Would't the getSimpleName() return the name without the package?(Am not sure)
If you are doing, what I think you are doing, I would suggest use the instanceof operator...unless I have misunderstood your question
 
Sheriff
Posts: 22567
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:Would't the getSimpleName() return the name without the package?(Am not sure)


Yes it would. Which can be a danger; consider java.util.List and java.awt.List. Comparing the full class name would really find those two unequal.

The only time when s.getClass().equals(MyClass.class) would return false even though s.getClass() is a MyClass Class object, is when two different class loaders were used. However, since MyClass.class is resolved at compile time, the class itself is loaded by the system class loader. That means that if s.getClass() would return a MyClass Class object as well, it will be found through the same class loader. Hence, s.getClass().equals(MyClass.class) will return true in that case.

Note that you can also use == for Class object comparison since Class is final and inherits equals from Object, which uses == for equality.

If you are doing, what I think you are doing, I would suggest use the instanceof operator...unless I have misunderstood your question


Using this type of class comparison is stricter. For instance, if s is a java.util.Stack, then "s instanceof Vector" will return true while "s.getClass() == Vector.class" will not.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay, thanks everybody!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic