Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Compiler error...why? (http://www.programmingforums.org/showthread.php?t=12517)

amitabha Feb 6th, 2007 2:26 AM

Compiler error...why?
 
Byte b=new Byte(1); //<-here

ReggaetonKing Feb 6th, 2007 2:36 AM

The constructor for a Byte does not take an integer value but a String value instead. See below.
:

Byte b = new Byte("1");

hoffmandirt Feb 22nd, 2007 4:25 PM

Actually there are two constructors:

:

1.  Byte(byte value)
2.  Byte(String s)


Example of each:

:

public class ByteTest {
    public static void main(String[] args) {
        byte aByte = 1;
        Byte byte1 = new Byte( aByte );
        System.out.println( byte1 );

        String strByte = "1";
        Byte byte2 = new Byte( strByte);
        System.out.println( byte2 );
    }
}


So to answer your question, you could create a Byte from an integer with a simple cast like so:
:

Byte b = new Byte( (byte)1 ) );


All times are GMT -5. The time now is 1:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC