Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Increment operators on char

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Increment operators on char

    i am trying to understand increment operators on char:

     
    public class Ch8 {
     
    	public static void main(String[] args) {
                 char c = 'a';
                 c++;
                 c = c+1;  
          }
    }

    why does c++ lead to c = 'b' ? i understand why c =c +1 is a error but how come c++ gets away with that error??


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Increment operators on char

    c = c + 1; isn't necessarily in error per se. It's just when you add a char to an int, you get an int, so for this to work you must cast the result as a char:

    c = (char) (c + 1); // this works

    c++ does this automatically for you.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Increment operators on char

    i understand why c =c +1
    Why is it an error? I'm asking because it might shed light on why "c++;" is valid.

    Exactly what happens when you say "c++;" involves all sorts of fussy details. They are described in the Java Language Specification 15.14.2. Postfix Increment Operator ++. Pay attention to what is says about the primitive narrowing conversion. You might want to compare that with what happens with simple assignment (5.2).

Similar Threads

  1. Processing of increment and decrement operators in Java
    By fredyeboah in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 22nd, 2012, 11:03 AM
  2. [SOLVED] Char Array Increment + 1
    By Nuggets in forum What's Wrong With My Code?
    Replies: 38
    Last Post: April 13th, 2012, 05:35 AM
  3. Help with logical operators
    By BiaxialPainter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2012, 12:35 AM
  4. Logical Operators
    By truebluecougarman in forum Java Theory & Questions
    Replies: 3
    Last Post: January 22nd, 2011, 06:26 PM
  5. Need Help with Operators/ logic
    By codekiller in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 3rd, 2010, 09:25 AM