• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Need help on Questions of wrapper classes

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi , Below are the code questions from Dan's Mock... Can anyone please provide a general rule need to be followed/checked before answering these questions.



 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might help if you actually present the questions.
 
aishwarya mathur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry for responding,
What is the result of attempting to compile and run the program? is the question to the above codes.


I feel so helpless when I get my answers wrong,the main reason is I have wrong assumptions about things.The wrapper class codes what I provided are getting me messed up related to the primitive types and their conversions.

Is there any rule to be followed to check errors in these types of codes.
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

didn't try it on my compu but i guess //1 and //2 would be wrong. //3 is correct.

U didn't provide the answers, did you.
[ September 21, 2004: Message edited by: Ankur Bhatt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having passed my SCJP 1.4 over a year ago I have forgotten lots of stuff. So taking the first example I look up the API for Byte. I see it has two constructors, one taking a byte the other a String. The first two variables b1 and b2 are candidates for the constructor which takes a byte, and the third variable b3 is a candidate for the constructor which takes a String. Now 1 and '2' are int literals and I know that these are not going to compile without a cast to byte. The API for the constructor which takes a String says that the method to parse the string is the same as the parseByte method, which requires decimal digits with an optional minus sign, and a numerical value that fits into a byte. 3 is in the range [-128, 127] so we are OK, that will compile and parse at runtime.

The last part is the assignment. So check the API again and I see that byteValue() returns a byte. The result of the additions will be an int which needs a cast back to a byte for the assignment to work.

So I think we have 3 lines giving a compile error.

My next step is to put the code into my testing IDE (BlueJ) and try to compile it. Yup three errors. I correct those errors by inserting the required casts and run the program. Yes, it runs and produces 54. 54? Ah yes the character literal '2' is the reason, it has value 50 = 0x32 (16) = 062 (8). A quick look at this ASCII table verifies this.

That's my method for Dan Chisholm's problems. I (re)learnt and reinforced this stuff in my tired old brain. There's no easier way. If in doubt, use the compiler to sort you out. Roll on SCJP 1.5
[ September 21, 2004: Message edited by: Barry Gaunt ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic