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

Question from Jargon Mock Exam

 
jayashree murugan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic