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.
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>
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 ]
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 ]
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:
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.
I've heard it takes forever to grow a woman from the ground