File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int and char

 
Thomas Markl
Ranch Hand
Posts: 192
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
ch is an integer variable. Why is it possible to say ch != 'x'? ch is an int and 'x' is a char.
import java.io.*;
class MyInputStream1 {
public static void main (String args[ ]) throws IOException {
int ch;
do {
ch = System.in.read();
} while (ch != 'x');
System.out.println("Input was x; Programm termination");
}
}
 
Bosun Bello
Ranch Hand
Posts: 1510
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because thew char will be promotied to an int and then the comparison will be done. You can check out the API for the promotion rules for more details.
 
inam paki
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thomas;
Remember one thumb rule while dealing with any one of these JAVA primitives
*byte
*short
*char
when ever you perform any opertation on these types they are promoted to int or the other superior type u r assigning to.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic