Compiler does not object to add an int value to a byte because it performs an explicit cast : Cast Operators « Operators « SCJP

Home
SCJP
1.Java Source And Data Type
2.Operators
3.Modifiers
4.Type Casting
5.Statements
6.Object Oriented
7.Thread
8.Utility Classes
9.File
SCJP » Operators » Cast Operators 
2.4.3.Compiler does not object to add an int value to a byte because it performs an explicit cast
public class MainClass {
  public static void main(String[] argv) {
    byte b = 0;
    b += 27;
    b = b + 27;
    b = (byte) (b + 27);

  }
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Type mismatch: cannot convert from int to byte

	at MainClass.main(MainClass.java:5)
2.4.Cast Operators
2.4.1.The Cast Operator (type) is for explicit conversion of a type.
2.4.2.Cast Operator converts numeric values from one type to another or to change an object reference type.
2.4.3.Compiler does not object to add an int value to a byte because it performs an explicit cast
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.