Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Please help and explain the output of following code.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class B
{
public static void main(String args[ ] )
{
String str = "true";
String str1 = new String("true");
StringBuffer sb = new StringBuffer("true");
Object obj_string = new String("true");
Object obj_boolean = new Boolean("true");
Object obj_sb = new StringBuffer("true");
Object obj = obj_sb.toString();
System.out.println("str=" + str+ " str1 =" + str1 + " str.equals(str1)=" +str.equals(str1));
System.out.println("str=" + str+ " sb =" + sb + " str.equals(sb)=" +str.equals(sb));
System.out.println("obj_string=" + obj_string+ " obj_sb =" + obj_sb + " obj_string.equals(obj_sb)=" +obj_string.equals(obj_sb));
System.out.println("obj_string=" + obj_string+ " obj =" + obj + " obj_string.equals(obj)=" +obj_string.equals(obj));

}
}

Output
==================
1) str=true str1 =true str.equals(str1)=true
2) str=true sb =true str.equals(sb)=false
3) obj_string=true obj_sb =true obj_string.equals(obj_sb)=false
4) obj_string=true obj =true obj_string.equals(obj)=true


Some one please explain the following points
1) Why in 3) and 4) in output "obj_string.equals(obj_sb)=false" BUT obj_string.equals(obj)=true

2) Why in 2) in o/p "str.equals(sb)=false"

3) When does a new String is created (and not picked up from the String Literal pool) so that == comparision will return false.

4) Is there any relation between String Pool and .equals. in String

5) String had .equals(object) and .equals(String)
So if you have something like
Object a = new String ("amit");
Object b = new String ("amit");
System.out.println(a.equals(b));

How are Strings or to say Objects compared here. ..... Please need some guidance in this topic. Getting many Questions wrong in the Practice Test concerning String Pools
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Agent Agent:
1) Why in 3) and 4) in output "obj_string.equals(obj_sb)=false" BUT obj_string.equals(obj)=true



If you try to compare a String to an object that isn't a String with equals, the result will be false.

2) Why in 2) in o/p "str.equals(sb)=false"



Again, when you compare a String to a StringBuffer with equals, the result is false.

3) When does a new String is created (and not picked up from the String Literal pool) so that == comparision will return false.



A new String will be created anytime you use the new operator to create a String. There are several methods in String that may produce a new String.

The String concatenation operator will also produce a new String if one of the operands is not a literal.

4) Is there any relation between String Pool and .equals. in String



The Strings in the String pool are just String objects.

5) String had .equals(object) and .equals(String)
So if you have something like
Object a = new String ("amit");
Object b = new String ("amit");
System.out.println(a.equals(b));

How are Strings or to say Objects compared here. ..... Please need some guidance in this topic. Getting many Questions wrong in the Practice Test concerning String Pools



In this case, the objects created are String objects. String overrides the equals method.

By polymorphism, a call to the equals method of a will be a call to the equals method associated with the String referred to by a.
[ July 09, 2006: Message edited by: Keith Lynn ]
 
singh amit
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a lot.

I missed out the panecea of Polymorphism
 
Rancher
Posts: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello "Agent Agent"-

Welcome to JavaRanch.

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.
 
Sheriff
Posts: 67582
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Amit S", Ulf's request that you change your display name to adhere to JavaRanch standards was not a suggestion. Valid display names are mandatory for participation on the Ranch. Please change your display name as instructed prior to your next post.

Your display name must be a first and a last name separated by a space character, and must not be obviously fictitious. Initials in place of a last name are not acceptable.

Be aware that accounts with invalid display names are removed.

bear
JavaRanch Sheriff
 
It looks like it's time for me to write you a reality check! Or maybe a tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic