Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Please teach me this problem!!  RSS feed

 
EuSeok Lee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class StringTest {

/**
* @param args
*/
public static void main( String[] args ) {
byte b = 127;
byte c = 126;
byte d = c- b ;

System.out.println(d);

}

}

this is compile error.
I think "byte d = c-d ;" is error.
but byte type is -128~127. is right?
so c-b is '-1'.

why compile error?

byte d = (byte) c - d;
not error
compile completed....
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
before subtraction (or any numeric operation) byte is converted to int.
 
Crace Selvage
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you perform any operation on an int, or anything larger, the smaller data type is promoted. Bytes are slightly different in this regard, as they are automatically treated as integers.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!