File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"toString()" question

 
simpu ch
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
Here's another question from majji's that i've messed up with...

Question 9



The following code will give

1: Byte b1 = new Byte("127");
2:
3: if(b1.toString() == b1.toString())
4: System.out.println("True");
5: else
6: System.out.println("False");

A) Compilation error, toString() is not avialable for Byte.
B) Prints "True".
C) Prints "False".

The answer is "C"...my thinking is that,the addresss of "b1" is the same..
so,when u convert it into a string,it yields the same value and so, the result should be "true" answer should be "B".
Someone help me clear this..
Thanks,
simpu.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simpu,

The answer is false, because the existing implementation of Byte.toString will cause a new String to be created each time Byte.toString is invoked.

That question could fall into a category that Kathy Sierra refers to as "Java Triva". The SCJP exam attempts to test your knowledge of basic concepts that you will find in the Java Language Specification. The exam does cover the APIs of selected classes in java.lang and java.util, but those questions are seldom designed to test your knowledge of the internal implementation of those methods. Instead, the questions typically focus on testing your knowledge of importantinformation that appears in the API specifications (javadoc). If the internal details of the method are not described in the javadoc, and if the internal details are not important to most users of the method, then the details of the internal implementation of the method are not likely to appear on the exam.

The javadoc for the StringBuffer class clearly indicates that StringBuffer does not override the Object.equals method. Since it is very important that a user of the StringBuffer class understands that StringBuffer uses the Object.equals method, it is not surprising that the exam contains questions that cover that implementation detail.

It is typically not important to know if the String returned from the Byte.toString method comes from the String constant pool, so the exam is unlikely to test a person's knowledge of that internal implementation detail.
 
Aj Mathia
Ranch Hand
Posts: 478
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try
b1.toString().equals(b1.toString())
and see the difference
Cheers
 
simpu ch
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dan for explaining me with that detail...
I could figure out the tricks of scjp exam

simpu.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Chisholm:
The answer is false, because the existing implementation of Byte.toString will cause a new String to be created each time Byte.toString is invoked. [my emphasis - PdH]
For that reason, a question like this has no place whatsoever in a Java exam, including practice exams. The Java language specification and public APIs do not specify the answer to this question. Dan is spot on: this is not a Java question, it's an implementation trivia question which has one answer for Sun J2SE 1.4, but might well have another answer for, say, the IBM JDK, or Sun J2SE 1.5. You can't even appeal to "common sense", as you could easily imagine an optimised Byte.toString() that uses 256 string constants.

The only correct answer is D) Either B or C depending on the implementation. This question ought to be removed IMHO.

If you still need convincing, compare the question with this:Go ahead. Run it.

- Peter
[ July 31, 2004: Message edited by: Peter den Haan ]
 
Jack Kay
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 == 4
The == are for comparing primitives & comparing if two objects are referring to the same object in memory - Thank you George

"hello".equals(myString)
The equals is for comparing objects

Primitives are NOT objects, String IS an object. An object is something that KNOWS(instance variables) something and can DO(methods) something.
Primitives can't DO something:
4.Nothing here!
Primitives don't Know something:
4.myValue

But Objects can DO something:
"Hello".subString(3, 5);
And an Object knows something:
"Hello2".length();

Hope that helps!
[ August 01, 2004: Message edited by: Jack Kay ]
 
Nick George
Ranch Hand
Posts: 815
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack, I saw you post a very similar message on another thread, and I would just like to point out one thing: == is NOT exclusively for primatives. It can be applied to Objects, but has a meaning when done so that must be understood. It would not test equivilency, but rather if the two references point to the VERY SAME OBJECT. To illustrate my point:

DingBat bob=new DingBat("Silly");
DingBat robert=bob;
System.out.println(bob==robert);

The references point to the same object, so this is true. This is extremely useful in large programs where an Object may have many different references spanning the project, and you want to determain if they refer to the same object.

-Joe.
 
Weij Yin
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

It may be useful for determining the result when using "=="......

Operator | Operand types | Operation performed
------------------------------------------------------------
== | primitive, primitive | equal(have identical values)
== | reference, reference | equal(refer to same object)

Weij
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic