Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CHAR

 
Prasad Narasimha
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Friends, I wrote the following program

public class CHAR
{
public static void main(String args[])
{
char a = 'a';

char c = 12344;

char d = (char)12345565;

System.out.println(a + c + d);

System.out. println(d);

System.out. println(c);

System.out. println(a);
}
}

OUTPUT

37238
?
?
a

I am unable to find the reason about the output can any help me the reason behind the following output.
 
Uday Bhaskar Kopalle
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Here you need to understand the behaviour of the println or print method. In print/println method you can use the + operator in two ways:
1. As a string concatenation operator
2. As an Arithmatic operator

And If the argument starts with a string, then + is considered as String concatenation operator like:
System.out.println("uday" + 1 + 2 ); //prints uday12
System.out.println(" " + 1 + 2 ); //prints 12
System.out.println(s + 1 + 2 ); //prints abc12 if s is a string of "abc"

And If the argument doesnt start with the String, then the + is considered as arithmatic operator like:
System.out.println(1 + 2 ); //prints 3 NOT 12

In your first println statement, the + is used as an arithmatic operator.

Regards,
Uday Bhaskar
[ July 01, 2007: Message edited by: Uday Bhaskar Kopalle ]
 
Prafulla N. Patil
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Thillakan Saba
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this :::
class CharTest
{
public static void main(String args[])
{
char a = 'a';
char c = 12344;
char d = (char)12345565;
System.out.println(a + c + d);
System.out. println((int)d);
System.out. println((int)c);
System.out. println(((int)a));
}
}


OUTPUT ::

37238
24797
12344
97
 
Thillakan Saba
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reason for ? --
the value may be out of char range.
 
Prasad Narasimha
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks A lot. And i understood it very well
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sabanayakam Thillakan:
reason for ? --
the value may be out of char range.


The range of a character is from 0 to 65535. Possible reasons for the question mark:
  • The value is not assigned to an character.
  • There is no available font to display the character.
  • The character isn't displayable on the output device.
  •  
    Deepak Vadgama
    Greenhorn
    Posts: 29
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Above post by Manfred Klug gives the exact reason for display of ?

    As we already know, the range of a character in Java is from '\u0000' to '\uFFFF',where each unicode represents a symbol.

    To display a symbol (for a particular unicode),obviously,JRE needs help of OS.For this JRE needs to know what character set does the underlying Platform supports.
    http://java.sun.com/javase/technologies/core/basic/intl/faq.jsp#unicode-version

    By default JRE (on Windows Platform) supports Windows-1252 character set.
    http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT

    This format has symbols having unicode from '\u0000' to '\u00FF'.
    So any unicode you specify outside this range, JRE fails to find a symbol for it. Thus instead it displays "?" to signify that "I dont know what this unicode stands for..."
     
    Don't get me started about those stupid light bulbs.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic