byte to int Java


After knowing the Java rules of Data Type Casting (Type Conversion), let us cast byte to int. The byte takes 1 byte of memory and int takes 4 bytes of memory. Assignment 1 byte value to 4 bytes is done implicitly by the JVM.

byte –> short –> int –> long –> float –> double

The left-side value can be assigned to any right-side value and is done implicitly. The reverse requires explicit casting.

Example:

       short x = 10;
       int y = x;

       byte x = 10;
       short y = x;
Following program on byte to int explains the syntax of implicit casting in Java.
public class Conversions
{
  public static void main(String args[])
  {
    byte b1 = 10;              // 1 byte  
    int i1 = b1;               //  one byte to int 4 bytes

    System.out.println("byte value: " + b1);             //  prints 10
    System.out.println("Converted int value: " + i1);    //  prints 10
  }
}


byte to int
Output screenshot of byte to int Java

1 byte value b1 is assigned to 4 bytes i1 and is type casted implicitly. This is known as implicit casting or widening conversion.

View all for 65 types of Conversions

Pass your comments and suggestions of how far this is helpful to you and also for the improvement of this tutorial "byte to int Java"

4 thoughts on “byte to int Java”

  1. Hi sir,
    Thanks for providing java concepts with detailed explanation. Here my question is:
    int a=130;
    byte b=(byte) a;
    Ans. -126
    I can’t understand this program……. please explain this in detail……

  2. Hi sir,
    Thanks for providing java concepts with detailed explanation. Here my question is: The range of java byte is -128 to 127. If we given the value like byte b=129; we are getting an error called “possible loss of precision”. But at the same time it allows the below code:
    int a=143;
    byte b=(byte) a;
    How it allows byte range more than 127?

Leave a Comment

Your email address will not be published.