File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from Jargon Mock Exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question from Jargon Mock Exam" Watch "Question from Jargon Mock Exam" New topic
Author

Question from Jargon Mock Exam

jayashree murugan
Greenhorn

Joined: Feb 10, 2000
Posts: 22
I've changed the question slightly :
public class Base {

void test() {
String aStr = "ONE";
String bStr = aStr;
String cStr = new String (aStr);
String temp = aStr.toUpperCase();
if(aStr == temp)
System.out.println("aStr == aStr.toUppercase()");
}

static public void main(String[] a) {
new Base().test();
}
}
The output is ("aStr == aStr.toUppercase()". How is this possible? The result of "aStr.toUppercase();" is stored in temp. When the two references (temp & aStr) are compared, the result should be false.
Tom P
Ranch Hand

Joined: May 06, 2000
Posts: 88
You are doing something wrong. I ran the code you provided and it did not print the "aStr == aStr.toUppercase()". The results is returned were exactly as you would predict. temp is not equal (==) to aStr.
jayashree murugan
Greenhorn

Joined: Feb 10, 2000
Posts: 22
I tried it again and it prints "astr == astr.toUpperCase". The mock exam also gives this answer as the right answer. That's where I got the doubt from. The exam does not put the result into temp but directly checks "if (astr == astr.toUpperCase())". I tried it both ways, directly and with a temp variable. In both cases the if evaluates to true.
bongadi
Greenhorn

Joined: Apr 17, 2000
Posts: 23
Hi!,
I am quoting from the language (JDK 1.2) api docs
<pre>

"toUpperCase
public String toUpperCase()
Converts all of the characters in this String to upper case using the rules of the default locale,
which is returned by Locale.getDefault.
If no character in this string has a different uppercase version, based on calling the
toUpperCase method defined by Character, then the original string is returned.

</pre>
So in this case ``astr == temp'' is true because your temp
is being set to aStr.toUpperCase() which is aStr.

Tom, I think you are running JDK1.1. I have found that on
JDK 1.1 (at leat on Solaris 2.6 x86) this test is false.
I have not checked the JDK1.1 api docs so I cannot comment on
the correctness of the behavior.
Try testing it on JDK1.2 (if you are not running it already) if
you can and let me know.
Originally posted by jayashree murugan:
I tried it again and it prints "astr == astr.toUpperCase". The mock exam also gives this answer as the right answer. That's where I got the doubt from. The exam does not put the result into temp but directly checks "if (astr == astr.toUpperCase())". I tried it both ways, directly and with a temp variable. In both cases the if evaluates to true.

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
the answer is that "aStr==astr.toUpperCase" is not printed on the console
this is because when u say "String temp=aStr.toUpperCase()" u r creating a new String object whose reference u r passing to temp variable. and the comparision operator "==" will definetly return false if the two objects are different.
Refering to the API. the toUpperCase Returns a new String Object in the following cases.
toUpperCase
public String toUpperCase()
Converts all of the characters in this String to upper case using the rules of the default locale, which is returned by Locale.getDefault.
"THE CONFUSION is here"
If no character in this string has a different uppercase version, based on calling the toUpperCase method defined by Character, then the original string is returned.
Otherwise, this method creates a new String object representing a character sequence identical in length to the character sequence represented by this String object and with every character equal to the result of applying the method Character.toUpperCase to the corresponding character of this String object.
however what i am confused about is since the aStr = "ONE";
the aStr.toUpperCase should return the same object as per the specification above and hence the result should be true. lets wait and see how things unfold
please refer to
http://www.javaranch.com/ubb/Forum24/HTML/001463.html
for further clarifications

[This message has been edited by rahul_mkar (edited May 16, 2000).]
joy
Greenhorn

Joined: May 15, 2000
Posts: 16
hi,
i'm using jdk1.2 and "aStr == aStr.toUppercase()" is printed to the console. The original String is returned since aStr is all caps. aStr.toUpperCase did not create a new String Object.
jayashree murugan
Greenhorn

Joined: Feb 10, 2000
Posts: 22
This behaviour of returning the original reference if there is no change effected on the string, is applicable to other functions of the String class like toLowerCase, replace, concat, trim etc.

[This message has been edited by jayashree murugan (edited May 16, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
jayashree sorry i am wrong. i am using Visual Java and hence maybe the result. i checked out with jdk and found out the result to be true.
hence the specification stands when i say
String a="ABC";
String s=a.toUpperCase;
since a is already in upper case no new object is created but the same object reference is returned
i again specify the api
If no character in this string has a different uppercase version, based on calling the toUpperCase method defined by Character, then the original string is returned
sanjivishyam
Greenhorn

Joined: May 16, 2000
Posts: 1
The value returned by if is truw because both aStr and temp are the same objects. If you create temp as
String temp = new String(aStr.toUpperCase());
then the value returned will be false because then temp and aStr will be different objects.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question from Jargon Mock Exam