This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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
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
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: int and char
 
It's not a secret anymore!