I always knew that characters could be represented as numbers. So when my little brother wanted to learn his alphabets I thought of writing up a program just to get all of them neatly printed. I started with this … but something went wrong. Can you help me out?
package com.twister;
public class TestOperator{
public static void main(String args[]) {
char x='a';
x=x+1;
System.out.println("Value of x is " + x);
}
}
Got an answer? Why don’t you leave it here.
Glad to see you're using BlogTrog Codehighlighter! Also, this is a neat blog. I'm going to send it to all my fellow programmers.
ReplyDeleteWhen the code reaches x=x+1, in order to do the arithmetic (since the + operator is not overloaded for char) it is converted to an integer. The compiler will try to convert the answer back to a char, which will fail because that is a narrowing conversion and can only be done explicitly.
ReplyDeleteFYI: This is the same Joe Smith that is on the leaderboard.
Go java go....:-)
ReplyDelete