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: Decrement operator help.

  1. #1
    Junior Member
    Join Date
    Sep 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Decrement operator help.

    public class Main {
    public static void main(String[] args){
     
    	int a = 10;
    	int b = (--a + 2)*3;
    	System.out.println(a);
    	System.out.println(b);
     
    	}
    }
    System.out.println(b) outputs 33 instead of 36. Could someone please explain this to me. Thanks.
    Last edited by Norm; September 17th, 2020 at 05:18 AM. Reason: Added ending code tag

  2. #2
    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: Decrement operator help.

    Why do you expect 36? (9+2)*3 = 33
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Decrement operator help.

    Okay I'm not quite sure why you are expecting 36 but I'll try to go step by step so you understand why its 33.


    int b = (--a + 2) * 3

    so considering a is 10, we have ( --10 + 2 ) * 3. The -- 10 part means its an PRE-decrement meaning that variable will be decreased by 1 before the other operations so than it becomes (9 + 2) * 3, brackets have "higher priority" than multiplication so its 11 * 3 = 33.

    Hope this help!

Similar Threads

  1. what is ... (triple dot) operator
    By hs82 in forum Threads
    Replies: 3
    Last Post: December 12th, 2017, 02:20 AM
  2. Ternary operator
    By JRoberto in forum Other Programming Languages
    Replies: 8
    Last Post: December 29th, 2013, 06:33 PM
  3. 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
  4. HELP ME HOW TO DECREMENT THIS PLS
    By jeremanalaotao in forum Loops & Control Statements
    Replies: 11
    Last Post: September 30th, 2011, 07:19 AM
  5. [SOLVED] Help with prefix and postfix(increment&decrement)
    By Lokesh in forum Object Oriented Programming
    Replies: 1
    Last Post: February 12th, 2011, 10:20 AM