learn java
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 The Software Craftsman this week in the Agile forum!
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.
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.

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.

Have you tried LearnNowOnline? http://www.learnnowonline.com/
subject: int and char