Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string quest

 
Abdul Latif
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which methods can be legally applied to a string object?
a) equals(object)
b) equals(String)
c) trim()
d) round()
e) toString()
According to me the answer is a,c & e
but the solution says a,b,c & e
please explain because threr is no method in the string class which takes a String as an argument.
 
suresh seeram
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b is correct because String is subclass of Object..
String is automatically converted to Object type..
String s=new String();
Object o=new Object();
o=s; Valid .. so above answer b is also correct..

For more info refer to the following
API:
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 Object
Parameters:
anObject - the object to compare this String against.
Returns:
true if the String are equal; false otherwise.
 
Mr Iftikhar
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Latif b is also correct

try this example
boolean b;
String s = "asdf";
String s1 = "asdf";
b = s1.equals(s);
here equals(String) is taking s as String Object and compare with another String object.

Means equals(String) is valid method for String .

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic