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 8 of 8

Thread: Operators

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Operators

    I could use some help understanding operators. ++, --, +=, -=, *=, /=, and %=. There's something about them that I'm just not getting. I would really appreciate your assistance!


  2. #2
    Member vigneshwaran's Avatar
    Join Date
    Nov 2012
    Location
    Chennai, TamilNadu
    Posts
    35
    My Mood
    Cheerful
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Operators

    those operators are used just immediate to any variables.
    like
    k++; //k=k+1  but post increment
    ++k; //pre increment the value of k
    similarly all functions as.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Operators

    something about them that I'm just not getting.
    Please give some details on your problem. Take a look at the tutorial:
    Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Operators

    Thanks for the help. Here is an excerpt from the book I'm reading about operators and which doesn't seem to be explained by the Oracle Java tutorial. This is where my confusion comes from:
    -------------
    int x = 10;
    int y = 20;
    What value is z assigned? The answer is 220. The third assignment increments
    x to 11 and decrements y to 19, but in computing the value of z, it uses the new
    value of x (++x) times the old value of y (y– –), which is 11 times 20, or 220.
    --------------------
    I understand that ++x equals 10+1, which is 11. But I don't understand how --y equals 20. Does --y mean "y minus a minus" or y subtracting (or minus) a minus? That's what is confusing me.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Operators

    ++ means increment (add 1).
    -- means decrement (subtract 1).

    There's no mathematical intuition or basis for why this is, this is just some idea that the designers decided a long time ago was nifty.

  6. #6
    Junior Member
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Operators

    That's what I thought. But how is ++y different from y++? And --y from y--? I ask because in the example above, where int y=20, y-- should mean 20-1 = 19. But the book has the answer as being 20.

    Thanks for answering my newbie questions.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Operators

    Google postfix operator for lots of discussions about how the operators work.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Operators

    Thanks. I was entering Java and operators, but it never occurred to me to try just try that. Google is giving me a lot of C++ pages, so I'll look at those. Nice indeed! Thanks!

Similar Threads

  1. Increment operators on char
    By ueg1990 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 19th, 2012, 12:26 AM
  2. Help with logical operators
    By BiaxialPainter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2012, 12:35 AM
  3. Evaluation of operators in expressions.
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 7
    Last Post: September 23rd, 2011, 07:23 PM
  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