• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how this works?

 
kavita s. kumar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
the following ode doesn't give any error..how byte accepts an int

public class AnotherDirtyOne
{
private final int i =10;
private byte k = i;
}

thans in advance
Kavita
 
hai zhang
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can assign an integer literal to a byte or short variable if the literal falls within the range of the variable.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here you don't assign a literal but a variable, Hai !
The only reason why this works is because the variable i is declared final and the compiler knows the value it holds. Try removing the final keyword and it doen't work anymore !
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic