• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

under what conditions can an int be assigned to a byte?

 
Aakash Goel
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class test1 {

public static void main(String[] args) {
System.out.println(getPrimitive(12));
}

public static int getPrimitive(byte b) {
return 1;
}
}


gives a compiler error.

but surprisingly 'byte b=12;' works.

when exactly is assigning a int to a bye allowed and when not?

Thanks
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Casting from an int to a byte is a narrowing conversion and therefore you have to do it explicitly. Essentially you are telling the compiler that you are aware that you are narrowing it and that you might lose precision. So instead of:

int number = 12;
byte smaller = number;

You would explicitly cast as such:

int number = 12;
byte smaller = (byte) number;

This way the compiler knows you realize you are making the primitive type a smaller type than it was before. However, if you were going from a byte to an int you would not need the cast:

byte answer = 42;
int biggerAnswer = answer;

That would compile fine, or you could cast it explicitly if you wanted to and it wouldn't hurt anything.

For more info, search this site for casting, or Google "java primitive casting".
 
Tom Kowalski
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you declar a new variable (byte), compiler automatically narrows the literal value to a byte. In other words, the compiler puts the 'cast' syntex.



this is identical to the following



But when you have got function which parameter is byte, this kind of thing don't accure, and it's up to you to ensure compiler that you're sending correct variable.

There is one more time, when you can assing int variable to a byte variable without casting it:





 
Ryan Anderson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ex) small into big

int a = 100;
long b = a; //implicit cast, int always fits into a long

ex) big into small [illegal]

long m = 100l;
int x = m; //COMPILER ERROR

ex) big into small [legal]

long m = 100L;
int x = (int)m;

ex) float to int [illegal]

float y = 29.9f;
int x = y;

ex) float to int [legal]

float y = 29.9f;
int x = (int)y;

ex) float literal to int [illegal]

int x = 22.4449;

ex) float literal to int [legal]

int y = (int)22.4499;
 
Krishna Srinivasan
Ranch Hand
Posts: 1862
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need explicit conversion for the int to byte
 
Ryan Anderson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
straight out of eclipse

 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In method invocation narrowing conversion is not possible see the JLS for details.
 
Aakash Goel
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ppl
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic