• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

String literal comparison inside a System statement with a String

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. Help much appreciated. This unexpected behaviour is not making sense. Line 11 is expected. Line 9 for example is expected to be 'a==c true', but comes out false?

1. String a = "fluffy";
2. String b = new String("fluffy");
3. String c = "fluffy";
4. String d = new String("duffy");
5. System.out.println("Here:"+"\n");
6. System.out.println("a==b");
7. System.out.println("a==b" + a==b);
8. System.out.println("a==c");
9. System.out.println("a==c" + a==c);
10. a=c;
11. System.out.println(a==c);

-------------------------------------------------------------------------------------------------
output:Here:

a==b
false
a==c
false
true

 
Rancher
Posts: 1049
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java String is an object and cannot be compared like an Atomic and get the right answer, you need to use the String Object comparison operator equals. When you use a "==" for comparing Strings, you are literally asking if hey are the same String--same address space and etc. Use the String comparison equals and you'll be fine.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please QuoteYourSources.

Henry
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

johnny smithy wrote:Line 11 is expected. Line 9 for example is expected to be 'a==c true', but comes out false?



Hint: Which operator has the higher precedence? is it the concat (+) or the equal to operator (==)?

Henry
 
Les Morgan
Rancher
Posts: 1049
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here ya go, API quote:

public boolean equals(Object anObject)

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
Overrides:equals in class ObjectParameters:anObject - The object to compare this String againstReturns:true if the given object represents a String equivalent to this string, false otherwiseSee Also:compareTo(String), equalsIgnoreCase(String)
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Les Morgan wrote:Here ya go, API quote:

public boolean equals(Object anObject)



Although, admittedly, in the majority of cases, the error is caused by the used of the "==" operator versus the equals() method -- this isn't one of those cases. It is using the "==" on two string references which is assigned via a string literal, and with the string pool, these two strings should refer to the same string object.

As mentioned, in my hint, this situation is related to the OP expecting a different precedence between the "+" and "==" operators.

Henry
 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic