• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

why this code is incorrect

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public double as()
{
byte ass=2000;
return ass;
}
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The range of byte datatype is -128 to 127.
You are trying to assign 2000 to it, which is beyond the allowed range. That is what it incorrect.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When you attempt to compile the above code you'll get one error:
ByteExample.java:10: Incompatible type for declaration. Explicit cast needed to convert int to byte.
byte abb=2000;

from A Programmer's Guide to Java Certification by Mughal and Rasmussen:
...implicit narrowing primitive conversions on assignment can occur in cases where the souce is an int constant expression whose value can be determined to be in the range of the destination type at compile time; the destination type is either byte, short, or char type.
...all other narrowing primitive conversions will produce a compile error on assignment and will explicity require a cast.


With those rules in mind -- lets look at your statement again:
byte abb = 2000;
* The RHS (right hand side) is a constant int expression
* The LHS (left hand side) is a byte
* The RHS is not within range of the LHS.
(a byte's range is -128 to 127)]
So.... that means you need to do an explicit cast: byte abb = (byte)2000;
... of course, when you do that because 2000 is out of the range of a byte the number will spill over to the negative side and you end up with -48!!
[ November 22, 2002: Message edited by: Jessica Sant ]
 
author
Posts: 8995
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Feng - Being trusting souls we have to assume that there is a good reason for your rather unfortunate selection of variable names, especially considering the type of primitive involved...
 
High Plains Drifter
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems to me if you byte ass hard enough, you'll end up with double.
 
feng bin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone-)
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
why the out put is -48. can you pl. explain.
thanks in advance
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you explicitly cast an int or long to a byte or short or char, all what happens is that the number gets trimmed, for instance
0x7FFFA7CB changed to short will be 0xA7CB
to byte will be 0xCB
so the output is not mathematically calcualted, represent 2000 in HD and trim it and you will get the -48
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudha,
Very simplistically...
A byte can have values between -128 and 127.
To make a byte of a specific number, Java starts at -128 and works its way up to 127.
Because 2000 is bigger than 256 (value between -128 and 127), when it gets up to 127 Java finds it hasn't fitted all of 2000 in the byte, so it goes back to -128 then back up to 127 and so on until it has added all of 2000. The byte value at this point happens to be -48.
(Even simpler, keep subtracting 256 from 2000 and the first value you get between -128 and 127 is -48)
Linda
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!