This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJP related .....

 
Prashant Kirtiwar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear all....
see this code..

public class seeit {
byte getNo(byte x,double y) {
return (short) ((byte)4/2.0 * 2) ;
}
public static void main(String as[]){
seeit st = new seeit();
st.getNo((byte)12,22);
}
}

It is going to work.compile and run successfully ..alright..but now see the real scenario
I changed a little bit code....


public class seeit {
byte getNo(byte x,double y) {
return (short) (x/y * 2) ;
}
public static void main(String as[]){
seeit st = new seeit();
st.getNo((byte)12,22);
//System.out.println(i);
}
}

I GOT AN COMPILE ERROR INDICATING POSSIBLE LOSS OF PRECISION....
Why do I get this error as the program remain the same as
the before one???
Pls remove my query....

regards
Prashant...
 
Debashree Halder
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it cannot convert from 'short' (16 bit) to 'byte'(8 bit)


-Debashree
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prashant Kirtiwar:


return (short) ((byte)4/2.0 * 2) ;

return (short) (x/y * 2) ;


Please, use [ CODE] [ /CODE] (without spaces).

The difference is that in first case you using literal constant and in second case not.

Think about this code (Note: 10 is int):
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic