File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes CHAR Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "CHAR" Watch "CHAR" New topic
Author

CHAR

Prasad Narasimha
Greenhorn

Joined: Apr 27, 2007
Posts: 7
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

Joined: Feb 22, 2007
Posts: 6
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

Joined: Mar 20, 2006
Posts: 106


Thanks, Prafulla Patil
SCJP, SCWCD
Thillakan Saba
Ranch Hand

Joined: May 15, 2007
Posts: 53
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


SCBCD, SCJP & MCP
HowToAskQuestions
Thillakan Saba
Ranch Hand

Joined: May 15, 2007
Posts: 53
reason for ? --
the value may be out of char range.
Prasad Narasimha
Greenhorn

Joined: Apr 27, 2007
Posts: 7
Thanks A lot. And i understood it very well
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
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

    Joined: Jul 02, 2007
    Posts: 29
    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..."


    -- deepak <br />SCJP 5.0, SCWCD 5.0
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: CHAR