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 char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "char" Watch "char" New topic


Kunal Choudhary

Joined: Feb 25, 2009
Posts: 14
public class testoperator

public static void main(String args[])

int y=3;
char x='a';


System.out.println("Value of x is " + x);
System.out.println("Value of y is " + y);


why does it give an error and how to solve it???
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
What compiler complains about? That's sufficient to understand the problem and to solve too.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Please make sure you enclose your any code with CODE tag. That gives ease in reading a post.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

Hi Kunal,

If you compile and run this, and it give you a compiler error, it GREATLY helps people figure out the problem if you post the EXACT text of that error message. It tells you all kinds of things, including where it thinks the error is (although it is sometimes wrong).

Without that, people have to really study the code, or copy it into their environment and compile it themselves. Even then they can't be sure, because it could be a problem with your environment.

so help us help you - tell us what the error is.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor

Joined: Oct 27, 2005
Posts: 20275

Kunal Choudhary wrote:char x='a';


The result of any mathematical operation where both operands are byte, char, short or int is always of type int.
In this case:
x: char
1: int
x + 1: int

There are three ways to solve this:

The first explicitly casts the int result back to a char, whereas the increment operators have an implicit casting internally.
I agree. Here's the link:
subject: char
It's not a secret anymore!