Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7915907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:37:02+00:00 2026-06-03T14:37:02+00:00

I am analyzing the following piece of code using a static analysis tool called

  • 0

I am analyzing the following piece of code using a static analysis tool called FindBugs.

if(str.equals(enum.SOMEVALUE)) {// do something};

where str is a String and enum is an enumeration. The tool generates the following warning for this code, and states

This method calls equals(Object) on two references of different class types with no common subclasses. According to the contract of equals(), objects of different classes should always compare as unequal; therefore, according to the contract defined by java.lang.Object.equals(Object), the result of this comparison will always be false at runtime.

if I replace the above line of code with this:

if(str.equals(enum.SOMEVALUE.toString())) {// do something};

then the warning disappears.But I am not sure if the warning that the tool generates is really true and whether I am fixing it the right way ? because I’ve seen such comparisons before and it appears to be working correctly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:37:04+00:00Added an answer on June 3, 2026 at 2:37 pm

    Your first comparison is basically wrong. You are comparing objects of completely different types (String and Enum) and they can never be equal. intellij even gives me a warning here. It compiles only because equals() accepts Object, not a String.

    The second comparison is correct.

    Although JavaDoc is a bit harsh on name() method, I would actually advice using it in case given enum has toString() overriden:

    if(str.equals(FooEnum.SOMEVALUE.name()))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following Ruby code analyzing a three-byte UTF-8 string: #encoding: utf-8 s =
I am using CakePHP with Apache2. Analyzing my pages with the tool PageSpeed from
I am trying to play dynamically few mp3 sounds using AVAudioPlayer. The following code
I am interested in analyzing balance sheets and income statements using R. I have
I've written a custom task for analyzing code coverage results. While building the entire
I'm trying to solve the following problem: I'm analyzing an image and I obtain
I'm currently playing with linked lists, and I've put together the following code for
I'm analyzing some old SQL code in a stored procedure. Declare @Var1 money, @Var2
How to I dispose the File.OpenRead() correctly. I am currently using the below code?
I'm using the Moq framework to do my unit testing. I'm following some really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.