aspose file tools*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""toString()" question" Watch ""toString()" question" New topic
Author

"toString()" question

simpu ch
Greenhorn

Joined: Jun 21, 2004
Posts: 10
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

Joined: Jul 02, 2002
Posts: 1865
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.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Aj Mathia
Ranch Hand

Joined: Apr 11, 2003
Posts: 478
Try
b1.toString().equals(b1.toString())
and see the difference
Cheers


You think you know me .... You will never know me ... You know only what I let you know ... You are just a puppet ... --CMG
simpu ch
Greenhorn

Joined: Jun 21, 2004
Posts: 10
Thanks Dan for explaining me with that detail...
I could figure out the tricks of scjp exam

simpu.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Aug 01, 2004
Posts: 62
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

Joined: Apr 04, 2004
Posts: 815
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.


I've heard it takes forever to grow a woman from the ground
Weij Yin
Greenhorn

Joined: Aug 02, 2004
Posts: 9
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


AspectJ, an asepct-oriented extension to Java programming language.<br /><a href="http://www.aspectj.org" target="_blank" rel="nofollow">http://www.aspectj.org</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "toString()" question