• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Casting

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

I am a new member fo this group and preparing for SCJP 1.4

I have a question regarding Casting when we have a loss of precision when typecasted to low .


Say for eg:

byte b=(byte)128;

It wil give the output as -128 .

Again

byte b=(byte)129;

it will give the output as -127.

I have seen in the K&B book , they have explained it very well.

But it seems to be time consuming process....

(Taking two's compliment then change the value etc .... )

Are these type of question are asked in the exams .???

If yes , then do we have any short cut to crack the question...

Thanks in advance !!!

Cheers

Yogesh

(No need to shout in topic titles)
[ August 31, 2005: Message edited by: Barry Gaunt ]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Yogesh"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's important to understand the behavior of a Java integral when you try to assign a number greater than it can story. That's the only lesson point here.

I'm not sure what kind of shortcut you're looking for. Learning the values ranges and overflow behavior is pretty straightforward. What's annoying are questions that turn these kinds of issues into brain-teasers, I agree, but if problems in actual convoluted code were easy to spot we wouldn't need certifications to begin with, would we?
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yogesh,

The shortcut which i used to remember is "once it exceeds the range it will wrap over the opposite range".If u want u can remember this simple rule.(even i hate of doin those two's complement operations )

-128 -127 -126 ....0...... 126 127

For example

byte b=(byte)128(max_value + 1) will assign b to -128(one wrap over)

byte b=(byte)129(max_value + 2) will assign b to -127(two wrap overs)

So no of wrap over depends on the number u give.if u give say 130 then u'll get -126!!.I cant assure you that these kind of questions wont be there at all in the exam.May be, one or more questions of this kind may peek out.So be prepared for that.

Good luck!

Regards,
Priya.
 
Yogesh Yadav
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Priya !!!

I guess that wud be the best way to do things fast .......

cheers

-Y
 
If you look closely at this tiny ad, you will see five bicycles and a naked woman:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic