This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes under what conditions can an int be assigned to a byte? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "under what conditions can an int be assigned to a byte?" Watch "under what conditions can an int be assigned to a byte?" New topic
Author

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

Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
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


SCJP 5 95%
SCJP FAQ | SCJP Mock Tests | SCJP Tipline | Generics
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
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".


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Tom Kowalski
Ranch Hand

Joined: Feb 17, 2009
Posts: 72
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:







SCJP, SCWCD, OCUP
Ryan Anderson
Greenhorn

Joined: May 18, 2009
Posts: 21
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

Joined: Jul 28, 2003
Posts: 1844

You need explicit conversion for the int to byte


Krishna Srinivasan
Spring Tutorials, OCAJP Mock Questions, 400+ OCPJP Mock Questions
Ryan Anderson
Greenhorn

Joined: May 18, 2009
Posts: 21
straight out of eclipse

Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

In method invocation narrowing conversion is not possible see the JLS for details.


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
thanks ppl
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: under what conditions can an int be assigned to a byte?
 
Similar Threads
Method specific call.
Casting between int and byte
Casting
int value cast to byte
about a Math.sqrt()