byte Constructor

Discussion in 'Java' started by Bharat Bhushan, Aug 23, 2003.

  1. Hi,

    I have a byte type variable and I need to create something like :

    abc = new byte(String Values)
    or even a
    abc = new byte(byte[] Values)

    There isn't a constructor that can do this. Can you please recomend the best
    way to do this?

    Also, what is the differnce between a Byte and byte datatype.


    Many Thanks for your time,

    - Bharat.
     
    Bharat Bhushan, Aug 23, 2003
    #1
    1. Advertising

  2. "Bharat Bhushan" <> writes:

    > abc = new byte(String Values)


    byte abc = Byte.parseByte(Values);

    > abc = new byte(byte[] Values)


    What should this do?

    > Also, what is the differnce between a Byte and byte datatype.


    Byte is a combined Immutable and Utility for the byte primitive type.
     
    Tor Iver Wilhelmsen, Aug 23, 2003
    #2
    1. Advertising

  3. Bharat Bhushan:

    >I have a byte type variable and I need to create something like :
    >
    >abc = new byte(String Values)
    >or even a
    >abc = new byte(byte[] Values)


    byte does not have a constructor, it's a primitive type.

    Byte has a constructor that takes a String. Check out the API docs of
    java.lang.Byte. You can get the byte value of a Byte object by calling
    byteValue() on it.

    If the values in the second example of yours are supposed to be
    characters of a String - first create a String from them (String has a
    constructor with a byte[] argument), then give the String to the Byte
    constructor.

    >There isn't a constructor that can do this. Can you please recomend the best
    >way to do this?


    >Also, what is the differnce between a Byte and byte datatype.


    byte is primitive, Byte is a class. Check out any introductory Java
    text for the difference.

    Regards,
    Marco
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
     
    Marco Schmidt, Aug 23, 2003
    #3
  4. Bharat Bhushan

    Jon A. Cruz Guest

    Marco Schmidt wrote:
    >
    > If the values in the second example of yours are supposed to be
    > characters of a String - first create a String from them (String has a
    > constructor with a byte[] argument),


    You forgot that he *needs* to use the constructor with both a byte[]
    argument, and String argument that's the encoding to be used for
    converting bytes to characters.

    Otherwise he might get incorrect characters.
     
    Jon A. Cruz, Aug 23, 2003
    #4
  5. Bharat Bhushan

    Neomorph Guest

    On Sat, 23 Aug 2003 13:21:52 +0100, "Bharat Bhushan" <>
    two-finger typed:

    >Hi,
    >
    >I have a byte type variable and I need to create something like :
    >
    >abc = new byte(String Values)
    >or even a
    >abc = new byte(byte[] Values)
    >
    >There isn't a constructor that can do this. Can you please recomend the best
    >way to do this?


    What type did you want abc to be ?

    If abc is supposed to be an array of bytes representing the string:
    byte [ ] abc = values.getBytes("UTF-8");

    To copy a byte array called values into another, new byte array:
    byte [ ] abc = new byte [values.length] ;
    System.arraycopy(values,0,abc,0,values.length);

    Assuming values is a String that you want to disect.
    "UTF-8" represents an encoding that code from the 16-bit Unicode characters
    in the String to an 8-bit byte sequence.


    If you expect the String values to be a decimal representation of a value
    between 0 and 255 (which can be represented in a single byte), then you can
    use:
    byte abc = Byte.parseByte(values);

    And if you read such a value as bytes from an InputStream and want the same
    result:
    byte abc = Byte.parseByte(new String(values, "UTF-8"));

    Here you also see the encoding representation of Unicode as bytes.
    There are other encodings which work closer to the ASCII standard, where
    each byte actually represents a single character.

    >
    >Also, what is the differnece between a Byte and byte datatype.


    Byte is a class that has been defined to give a programmer information
    about the primitive byte datatype (such as Byte.MAXVALUE, which is 255) and
    provide extra functionality with respect to the byte primitive.

    You can also create an immutable Object that contains a byte value, so that
    you can mix different primitives and any other objects in an array or Vecor
    class:
    Object [ ] anything = new Object [5] ;
    anything[0] = new Byte(128);
    anything[1] = new Integer(32767);
    anything[2] = "Some string" ;
    anything[3] = new Character('\\');
    anything[4] = new java.awt.Frame("Hello World!");

    anything[1] = 32767 ; would not work, since the array is not of the
    primitive type int.

    The Javadoc documentation of the JSDK is a very good reference to see what
    the Byte, Integer and other primitive representing classes can do.

    >Many Thanks for your time,
    >
    >- Bharat.
    >


    Cheers.
     
    Neomorph, Aug 25, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas
    Replies:
    1
    Views:
    906
    Jonathan Bromley
    May 4, 2004
  2. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,407
    Roedy Green
    Aug 5, 2003
  3. Jean-Daniel Gamache
    Replies:
    0
    Views:
    435
    Jean-Daniel Gamache
    Jul 14, 2004
  4. Peter
    Replies:
    3
    Views:
    741
    Michael Borgwardt
    Aug 5, 2004
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,323
Loading...

Share This Page