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 8543775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:23:19+00:00 2026-06-11T12:23:19+00:00

I was talking with my CompSci professor and he suggested that all String .equals

  • 0

I was talking with my CompSci professor and he suggested that all String .equals methods be written as:

"Hello World".equals(fooString);

rather than:

fooString.equals("Hello World");

Both of these lines compile, but I was wondering what are the benefits of the first way? I’ve always done it the latter way. Is this wrong? What is common/conventional?

  • 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-11T12:23:21+00:00Added an answer on June 11, 2026 at 12:23 pm

    The first way ensures that you will not receive a NullPointerException when you perform the comparison. This Exception is thrown (occurs) when you try to invoke a method on an object that doesn’t exist.

    Somewhat Relevant Tangent Below: Peruse At Own Risk

    As a note, though, I have very, very rarely actually seen this technique or coding style used in a legitimate production environment. There are two reasons for this.

    First, programs you write will almost always want to do something special or different in the case that your variable contains null. This is because it usually indicates an error in some other part of the code’s function or a special case that needs to be addressed. As such, null checking should always occur before any comparisons or method calls on the object (in this case, your String fooString).

    Second, fooString.equals("Hello World") just tends to make your code slightly more readable. The logical significance of the first technique in your code takes a few seconds to process correctly, and it is easy to make an error of judgment when you are skimming over hundreds or thousands of lines of code. This is actually much less trivial than it sounds, because in the working world, more often than not, there are very large and very old programs that will be patched by people who knew nothing about them before glancing at them to solve the problem. Code readability contributes in a huge way to a company’s ability to perform fast triage and solve long term problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im talking about performing a deep recursion for around 5+ mins, something that you
I am talking about some sort of function that can combine an array of
I was talking with my teacher, and she mentioned that an object variable (she
Im talking about the menu that shows up when you select a block of
I'm talking about methods like this: $object->method()->method1('param')->method2('param'); Those are created by returning the object
When talking about relational database the concepts I've learned are that there exists schema,
When talking about the STL, I have several schoolmates telling me that vectors are
I'm talking about a library that would allow me to log events from different
I'm talking about something like GreaseMonkey but that would accept the script just as
Assuming that I'm talking about an ASP.NET MVC 3 application, the scenario is something

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.