user7
user7

Reputation: 525

what differnce this method of passing parameter mean to jvm

when we are passing 123 to display method then why its giving error whereas passing the same value using variable by1 is accepted by the JVM.

WHATS THE DIFFERENCE THESE FOLLOWING TWO STATEMENTs MAKE 1.serv.display(123); 2.serv.display(by1);

public class Lab1 {

public static void main(String[] args) {
    MethodService serv=new MethodService();
    byte by1=123;
    serv.show(123);
    serv.show(by1);
    serv.display(123);
    serv.display(by1);
    }
}

class MethodService{
void show(int ab){
    System.out.println("---show(int)\t:"+ab);
}
void display(byte by1){
    System.out.println("display(byte)\t:"+by1);
}
}

Upvotes: 1

Views: 122

Answers (5)

bvdb
bvdb

Reputation: 24760

You need to add some casting.

serv.display((byte)123); // requires a cast

Sometimes casting is required and sometimes it is not. Here follows a bit of explanation to illustrate when and why you need it.

Java has several primitive types:

  • byte (8-bit)
  • short (16-bit)
  • int (32-bit)
  • long (64-bit)

This is important, because if you assigned an 32-bit (int) value to a 8-bit (byte) variable, you would in fact lose 24-bits of your number. This type of conversion is called a narrowing primitive conversion. For safety, the compiler will only accept this if you explicitly specify a cast ( such as (byte)123 ).

So, simply put, whenever you are passing a value or variable of a bigger type (more bits) to one of a smaller type (less bits) you will need a cast.

(The other way round) If you need to pass a byte (8-bit) to an int (16-bit) then there is no problem. The compiler knows that no data will be lost and the code compiles without explicit cast. So, that is why you do not need casting for the following line:

serv.show(by1); // does not require casts.

Now, you might also wonder about your first line of code, where you actually assigned an integer to a byte. So theoretically some bits could get lost.

byte by1 = 123; // assignments do not require casts.

However, the assignment conversions in java are more tolerant (or just smarter) than the method invocation conversion. The compiler knows that the above value (123) fits inside a byte, and will tolerate it.

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

serv.display(123);

will throw an error as there is no method with name display that accepts int value.

The reason why serv.show works with both byte and int input is because the method input type is int. This allows the method to accept the input types that can be auto converted by compiler to int. So any data type that takes less memory bits(32 or less) such as byte(8 bits) will be auto converted to int value.

But serv.display takes the input as byte which means this method can take input that can be auto converted to byte i.e any input with 8 bits or less. And when you pass an int to it, which takes 32 bits cannot be auto converted as it is out of byte no of bits range. Hence compiler throws an error.

Upvotes: 6

SpringLearner
SpringLearner

Reputation: 13854

display method accepts arguments of byte type and you are passing integer 123

Upvotes: 0

Iren Patel
Iren Patel

Reputation: 757

serv.display(123);

The method display(byte) in the type MethodService is not applicable for the arguments (int)

Upvotes: 0

Prasad Kharkar
Prasad Kharkar

Reputation: 13566

The display(byte b1) method accepts a byte. You are passing 123 which is treated as int by compiler

Upvotes: 2

Related Questions