2

I was learning wrapper class, here I learned how to convert int to Interger wrapper class.

But I want to convert int to Byte using Byte wrapper class.

I have tried

int a =10;
Byte c = Byte; //(Not getting suggestion in eclipse)

for example, I know how to convert int to Interger refer code below.

int a =10;
Integer b = Integer.valueOf(a);
Acha Bill
  • 1,255
  • 1
  • 7
  • 20
Vinay Pandey
  • 113
  • 5

4 Answers4

2

You could add an additional cast to the integer like this:

int a = 10;
Byte c = Byte.valueOf((byte)a);

Try it online.

Btw, going from a primitive to an Boxed value is done implicitly by the compiler. So the code above doesn't necessary need the .valueOf:

int a = 10;
Byte c = (byte)a;

Try it online.

But, when you're explicitly casting values, always keep in mind that it can hold unexpected results when doing it wrong. In this case for example, the size of a a byte is smaller than an integer (the range of a byte is [-128, 127] and of an int is [-2147483648, 2147483647]), so something like this wouldn't give the expected result:

int a = 200;
Byte c = Byte.valueOf((byte)a); // Results in -56

Try it online.

This is also the reason why you usually can't go to a smaller type, since it might not fit, but you can go to a larger one without needing the cast:

byte a = 10;
Integer c = Integer.valueOf(a);

Try it online.

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • Can we do it without type casting? – Vinay Pandey Aug 07 '19 at 09:38
  • 1
    @VinayPandey I'm afraid not, unless you do some kind of weird/nonsensical work-around, like convert the `byte` to a `String` and than from `String` back to a numeric value as `Integer` ([Try it online](https://tio.run/##dY4xD4IwEIV3fsWFqcQIumpc3BwMA6NxONpKiqWQ9qghht@OFVxMdLrL@@69dzV6XNfiPk1co3NwRmWeEUDXl1pxcIQUhm@VgCYgVpBVprpcAW3lkvclQDmQBIQDbDf7WTgZkpW0wIP22VOPupf5jRn5gGMwMExSapc4liSLsRgcySZte0q7AEgbxgOaWZZBbgE1SWvCU17qYffVJn604SqO/2eLGY3ROE0v)), which is not something I would recommend.. – Kevin Cruijssen Aug 07 '19 at 09:43
0

You can cast the same way you normally do with Integer wrapper.

int a =10;
Byte b = Byte.valueOf((byte)a);
//b = 10

An int is 4 bytes. So you can't convert an integer to a byte without losing some data.

int a =1000;
Byte b = Byte.valueOf((byte)a);
//b = -24

You can, however, convert an int to an array of bytes.

Java - Convert int to Byte Array of 4 Bytes?

Acha Bill
  • 1,255
  • 1
  • 7
  • 20
0

Same you can do for Byte too:

1. autoboxing

Byte b =  (byte) a;

2. valueOf(byte b)

Byte b =  Byte.valueOf((byte)a)

Returns a Byte instance representing the specified byte value. If a new Byte instance is not required, this method should generally be used in preference to the constructor Byte(byte), as this method is likely to yield significantly better space and time performance since all byte values are cached.

From JLS 5.0-2. Conversions In Various Contexts so you can't perform 5.1.3. Narrowing Primitive Conversion without cast operator.

    // Casting conversion (5.4) of a float literal to
    // type int. Without the cast operator, this would
    // be a compile-time error, because this is a
    // narrowing conversion (5.1.3):
    int i = (int)12.5f;
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
0
int x=30;
Byte c = (byte)x; // Java has auto boxing for wrapper class Byte to byte and opposite automatically 
Paul P Joby
  • 713
  • 7
  • 17