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

char

Kunal Choudhary
Greenhorn

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

public static void main(String args[])
{

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

x=x+1;
y=y+1;

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
Bartender

Joined: Oct 02, 2003
Posts: 11313
    
  16

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
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Kunal Choudhary wrote:char x='a';

x=x+1;

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: http://aspose.com/file-tools
 
subject: char