aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "int and char" Watch "int and char" New topic
Author

int and char

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
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

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.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
inam paki
Greenhorn

Joined: Jul 30, 2002
Posts: 14
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.


inampaki
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: int and char