• Register
1 vote
20 views

Problem:

Hello good people,

I am learning java academically. I just started a few days ago. Recently, I figured out something ++ and -- in a sample program in my book. Based on that program I wrote a program as well as being myself. My code is compiling and running successfully but I don’t know how it’s working and why? Your effort will be remembered. Thanks.

My sample snippet is attached below:

 

class Operator {
    public static void main(String[] args){
          int n=5;    
          System.out.println(n++);          
          System.out.println(n--);    
         }
    }

 

12 7 7
15,250 points

1 Answer

0 votes

Solution:

In programming, the ++ and -- are known as increment and decrement.

Suppose you declare a variable named var and put the ++ & -- sign as following:

var++      ------ This is post-increment

++var      ------ This is pre-increment

var--       ------ This is post-decrement and

--var       ------ This is pre-decrement

In your program, you must be  getting the value of

5
6

It means the post-increment adding your number by 1 on the first line and then it reduced by 1 because of the post-decrement operator.

I hope you get it clearly.

13 9 6
94,240 points

Related questions

0 votes
1 answer 8 views
8 views
Problem: I am confused about operators in Java and unable to understand the use of *= operator. Kindly explain the working of *= in detail.
asked Oct 11 Sheeza 2.9k points
0 votes
1 answer 7 views
7 views
Problem: Hi there, I am new to the world of programming. I have a question that what does “/n” mean and what is its purpose?
asked Oct 10 Sheeza 2.9k points
0 votes
1 answer 5 views
5 views asked Oct 10 Sheeza 2.9k points
0 votes
1 answer 12 views
12 views
What does the <E> syntax mean in Java? I searched for answers quickly, but I can't find or think about the exact search parameters. I am teaching Java myself, but I can't seem to find the meaning of certain syntax. public class Node<E>{     E ... vaguely remembering the feeling that they are related to enums based on the code above. Any help or explanation would be greatly appreciated. Thank you.
asked Aug 29 sasha 1.2k points
0 votes
1 answer 5 views
5 views
What is the difference between & and && in java?
asked Oct 9 sikandar 2.4k points