• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Enthuware Question Test ==, Equals()

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fill in the blank :
Given two non-null String object with reference name apples and oranges, if apples _____________ oranges evaluates to true, then apples ______________ oranges must also evaluate to true.

Option :
A. ==, equals()
B. !=, equals()
C. equals(), ==
D. equals(), ==

I choose the option C. but it is wrong Why. can explain it.
 
Saloon Keeper
Posts: 6757
63
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String apples = new String( "stuff" );
String oranges = new String( "stuff" );
System.out.println( apples.equals(oranges) ); // true
System.out.println( apples == oranges ); // false
 
Ng Sharma
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:String apples = new String( "stuff" );
String oranges = new String( "stuff" );
System.out.println( apples.equals(oranges) ); // true
System.out.println( apples == oranges ); // false



Please read the question carefully.
 
author & internet detective
Posts: 39789
797
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would go with A.

Suppose we define equality as any objects with the same color and I have two objects:
apple - red
tomato - red

Comparing these objects with return true for equals(), not false for ==.


Also, C and D look the same in your question
 
Marshal
Posts: 6639
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ng Sharma wrote:

Carey Brown wrote:String apples = new String( "stuff" );
String oranges = new String( "stuff" );
System.out.println( apples.equals(oranges) ); // true
System.out.println( apples == oranges ); // false



Please read the question carefully.


He did, and he provided you a counter-example for why your answer won't work.
 
Ng Sharma
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:I would go with A.

Suppose we define equality as any objects with the same color and I have two objects:
apple - red
tomato - red

Comparing these objects with return true for equals(), not false for ==.


Also, C and D look the same in your question



mam. please explain a more details.
 
Jeanne Boyarsky
author & internet detective
Posts: 39789
797
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose we define equality as any objects with the same color and I have two objects:
apple - red
tomato - red

If I call equals(), I get true because both are red. But If I use ==, I get false because they are different objects

If you need more detail, please explain what specifically you are confused about. Or try explaining it in your own words and show where you get stuck
 
Greenhorn
Posts: 2
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is as simple as if == returns true, that means the two objects referring to the same string in the pool as that is how String works if you defined it as follows:
String apple = "red";
String tomato = "red";

If == returns true, which it would in this case, equals() also will return true.

If you defined it as follows:
String apple = new String("red");
String tomato = new String("red");

Each reference will get a separate object in the memory and == will return false, but equals() will still return true and that is why your answer is not correct.

 
Marshal
Posts: 67437
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

The == operator has nothing to do with the String pool; it tests whether the two memory locations are the same for any reference types.Neither s0 nor s1 is interned in the String pool, but that test will still print true. KeyboardInputs is a utility class of mine.
 
Wael Hassan
Greenhorn
Posts: 2
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you. The == has nothing to do with the String pool, defining the String as String obj = "obj" does.
Doing so will set the value in the pool and will be available for any other String initialization using only = "obj".
The == will come next as it will be able to test that the two reference objects pointing to the same memory location, which is the value in the pool.
 
Campbell Ritchie
Marshal
Posts: 67437
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not certain, but I think String literals and other String compile‑time constants are put into the String pool when the class is loaded. As I said, not certain.
 
A tiny monkey bit me and I got tiny ads:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!