Implicit typecasting does not work if an integer value is passed as an argument in java

In the following code, implicit typecasting for integer 9 takes place and is assigned to a byte data type variable of size 8 bits.

class Demo1
{
   public static void main(String args[])
    {

    byte b=9;
    System.out.println(b);

    }

}

The code is happily compiled and executed.

but when I wrote the following code, it gave me a compilation error

class Demo2 
{
   void func1(byte b)
    {
        System.out.println(b);
    }

   public static void main(String[] args) 
    {
       Demo2 d1=new Demo2();
       d1.func1(9);
    }
}

please explain to me why implicit (auto typecasting) does not occur in the last code?

Thanks to everyone pending.

0
source share
2 answers

byte (8 ) , int (32 ), int byte, . :

    int a = 150;
    byte b = (byte) a;
    System.out.println(b); 

-106, 150 (-128 - 127).

int byte, , , .

+3

, , .

, , .

void func1(int i)
    {
        byte b = (byte)i;
        System.out.println(b);
    }
+1

Source: https://habr.com/ru/post/1546099/


All Articles