Sunday, March 29, 2009

Puzzle 11 – Alphabet Soup.

Language – Java | Type – Concept | Last date 01-Apr-2009 9:00 p.m. IST | Points 2

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.


3 comments:

  1. 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.

    ReplyDelete
  2. When 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.

    FYI: This is the same Joe Smith that is on the leaderboard.

    ReplyDelete

Solution for this question?