CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  1. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: JAva help, need to understand

    Quote Originally Posted by dstevens View Post
    I need to understand why this is giving me the result of 220
    Due to implicit conversion the chars are converted to their Unicode numbers which are 'D'=68, 'E'=69 and 'S'=83. The sum is 220.

    To get the wanted conversion you can give the compiler a hint to nudge it in the right direction, like

    System.out.println("" + first + middle + last );

    Now the compiler will interpret the + operators involved to mean String concatenation rather than integer addition. So the problem was that + is overloaded for both integers and String and you didn't get the implicit conversions you expected. To be very explicit about it you can do,

    System.out.println(Character.toString(first) + Character.toString((middle) + Character.toString(last));

    Now there's no doubt but it's kind of tedious so most people rely on implicit conversion with enougth information to make the compiler do the right thing.
    Last edited by nuzzle; January 19th, 2013 at 02:34 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured