Results 1 to 4 of 4
Thread: cast double to byte
- 09-26-2012, 06:23 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
cast double to byte
Hi
If some one asked me in an interview - Can we cast double value to byte. For that I have compiled an answer
Yes. Consider the example below
class MainClass
{
public static void main(String aa[])
{
double d=22.55;
byte b=(byte)d;
System.out.println(b);
}
}
Output
22
.55 will be lost
Is this answer fine?
- 09-26-2012, 07:08 AM #2
Re: cast double to byte
You haven't bothered to reply to the two responses on your earlier thread. Not the best way to continue to get help on a forum, that.
Also, go through BB Code List - Java Programming Forum and edit your post accordingly.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-26-2012, 07:09 AM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: cast double to byte
Yes, but if the double value bigger than the max value of a byte (+127) or smaller than the min value of a byte (-128) you will get an overflow!
Java Code:double d= -129; byte b=(byte)d; System.out.println(b); d= 128; b=(byte)d; System.out.println(b);
- 09-26-2012, 09:08 AM #4
Member
- Join Date
- Sep 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Double to Byte
By Javasubbu in forum New To JavaReplies: 3Last Post: 02-05-2012, 09:03 AM -
byte[] to double[][]
By kkGG in forum Advanced JavaReplies: 16Last Post: 01-23-2012, 03:54 AM -
convertir byte array "byte[]" to an image in j2me
By skon in forum New To JavaReplies: 1Last Post: 10-21-2011, 11:55 AM -
Read a bitmap byte by byte
By cbr400aero in forum Java 2DReplies: 2Last Post: 10-18-2010, 11:42 PM -
Streaming an image byte by byte (and similtaneosly rendering it on screen)
By ea25 in forum New To JavaReplies: 1Last Post: 04-21-2010, 02:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks