• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

SCJP 1.5, Chapter 6 question 4

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everybody,
I am going through this book SCJP 1.5 by Kathy Sierra and Bert Bates. I came across this question no "4" in chapter 6.

public class TKO {
public static void main(String[] args){

String s = "-";
Integer x = 343;
long L343 = 343L;
if(x.equals(L343) ) s +=".e1";

if(x.equals(343)) s +=".e2";

Short s1 = (short)((new Short((short)343))/(new Short((short)7)));
if(s1 == 7) s +="=s";

if(s1 < new Integer(7+1)) s += "fly";


System.out.println( s);

}
}

When I am running this code in Netbeans IDE 6.1 with JDK 1.5.14 , my output is " -.e2" where as according to the answer given in the book, output string includes .e2,=s and fly too.
theoretically, I understand it should include =s and fly but why in my case its not including theses.
I appreciate if some one helps me.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Asterix Asterix,

How is Obelix Obelix doing?
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I see it,



This test fails because variable L343 is a long, hence promoted into a Long object. The Object.equals() comparison fails because the two objects are different -- Long != Integer



This test passes because value 343 is promoted into an Integer object. As with Strings, numerical values can get cached behind the scenes which is what happened here so the Object.equals() comparison is identical.



All this evaluates to 49. All the casting is meant to distract. The maximum short value in Java is 2^15-1, clearly large enough to hold value 343. All the Short objects will automatically unwind (auto-boxing) appropriately for doing the math. The result is 49.



So, what actually happens here?

The == operator requires object of the same type. Moving left to right, s1 is Short, thus convert 7 into Short(7). The object's are not the same, thus the test fails.



Same thing here. The < operator requires object of the same type. Precedence converts the expression into s1 < Integer(8), which gets further converted into s1 < Short(8) and like before the object's are not the same ones, thus the test fails (the math would fail the test too).

The String s remains: -.e2

I assert that your IDE answer is correct and the book is wrong.

If I am wrong, I am sure someone will point it out to me but I have found that IDE's tend not to lie.
-Fred
 
Mumtaz Khan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sorry for the typo mistake,
This line
,
should be
,

So now the value of s1= 7;

The book is correct .




 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was a typo. But you probably shouldn't be using a IDE, as the book assumes you are using the command line. Irrelevant in this case though. Also, there is a policy in Javaranch about using real names, you might to want to take care of that.
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Asterix, this is the policy in Javaranch to use meaningful, first name and last name. Please kindly
change your first and last name.

 
Danger, 10,000 volts, very electic .... tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic