Results 1 to 4 of 4
  1. #1
    karang is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default 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?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default 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);

  4. #4
    karang is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default Re: cast double to byte

    Hi DarrylBurke

    I tried to find the way to mark the response as answer. So I liked the comment which solved my problem. Please tell me the way to mark it as answer

Similar Threads

  1. Double to Byte
    By Javasubbu in forum New To Java
    Replies: 3
    Last Post: 02-05-2012, 09:03 AM
  2. byte[] to double[][]
    By kkGG in forum Advanced Java
    Replies: 16
    Last Post: 01-23-2012, 03:54 AM
  3. Replies: 1
    Last Post: 10-21-2011, 11:55 AM
  4. Read a bitmap byte by byte
    By cbr400aero in forum Java 2D
    Replies: 2
    Last Post: 10-18-2010, 11:42 PM
  5. Replies: 1
    Last Post: 04-21-2010, 02:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •