Java char value assignments

Posted on 2009-04-15
Last Modified: 2013-11-23

I am porting a Java project which makes some strange assignments of int values in a function.  I have been Googling this for a while but have not figured it out.

The char c is being passed to the setX function which has a single int parameter.  So why has the original coder used a char instead of int?  The 'else' segment seems to assign an invalid unicode character to the char.  I don't know what c = '\210' does (decimal 210 is Ò).

Sorry the code fragment is so terse, there's not a lot of surrounding code to post.
char c;

if (xPos < 100) {

  c = '\210';


else {

  c = '\uFFF8';



// this is the definition of obj.setX

public void setX(int i)

Open in new window

Question by:falmon
    LVL 1

    Expert Comment

    Java will automatically cast the int value of a character.
    Are you sure the int value of '\210' is what you want?

    Author Comment

    Well - I am trying to work out what the code does so that I can replicate its behavior in AS3.  So I'm not sure that I want the int value of '\210'.  I am still baffled as to why he decided to create a char, assign it odd values, and then pass it into a function which will cast it as int.  The char isn't used elsewhere.

    My assumption was that since "char c = '\uFFF8';" will create a single Unicode character ( - possibly... it doesn't even seem to be a valid char) his '\210' assignment would perhaps assign a Unicode char by decimal value?

    I guess my question is - what values would I expect as the integer argument to setX for each part of the conditional, given the char values set, and is there any clear reason that the original coder decided to use a char in this way?
    LVL 1

    Accepted Solution

    Forget about the char. Replace it for an int.
    '\210' replace it with 136 and '\uFFF8' with 65528.
    Those are the int values that are being passed to that function.
    Now, your question about why those two values is a whole different issue.
    Can't be answered without some more reference about the application.

    Author Comment

    OK, that's very useful in terms of how the casting works and basically answers the question!!
    Never would have expected those int castings coming from a PHP/JS/AS background.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now