File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes int and char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "int and char" Watch "int and char" New topic

int and char

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
ch is an integer variable. Why is it possible to say ch != 'x'? ch is an int and 'x' is a char.
class MyInputStream1 {
public static void main (String args[ ]) throws IOException {
int ch;
do {
ch =;
} while (ch != 'x');
System.out.println("Input was x; Programm termination");
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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.

So much trouble in the world -- Bob Marley
inam paki

Joined: Jul 30, 2002
Posts: 14
Hi Thomas;
Remember one thumb rule while dealing with any one of these JAVA primitives
when ever you perform any opertation on these types they are promoted to int or the other superior type u r assigning to.

I agree. Here's the link:
subject: int and char
It's not a secret anymore!