• Register
1 vote
23 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

Please log in or register to answer this question.

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 3 views
3 views
Problem If i write in a function ‘*&’ , how its work or what will be output , if anyone knows the answar,please let me know
asked Apr 7 Subhendu 4.5k points
1 vote
1 answer 2 views
2 views
Problem: Why I got the error : what does the & mean in c++
asked Apr 6 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 12 views
12 views asked Jan 28 khanboy 2.1k points
0 votes
1 answer 2 views
2 views
Problem What does "\n" do? I tried searching on Google, but since it's such a small bit of code I couldn't find much. Anybody help me out.
asked 6 days ago neeraj 10.5k points
0 votes
1 answer 1 view
1 view
Problem I understand what public, static, and void mean, but what does (String[] arguments) mean? Can anyone please suggest
asked 6 days ago neeraj 10.5k points
0 votes
1 answer 3 views
3 views
Problem Implements vs extends: When to use? What's the difference? Can anyone please suggest
asked Apr 9 neeraj 10.5k points
0 votes
1 answer 4 views
4 views
Problem: i don&rsquo;t understand about this problem please help? what does << mean in java
asked Mar 28 Rohit kr 16.2k points
0 votes
1 answer 3 views
3 views
Problem : Any ideas on what I need to do to fix this? Thanks!-What Does Cannot Find Symbol Mean In Java?
asked Mar 15 sadi1982 36.3k points
0 votes
1 answer 30 views
30 views
Problem: My guess is that it's something related to Java 8 vs Java 7, but I have no idea what specifically.
asked Feb 15 TeamScript 13.5k points
0 votes
1 answer 10 views
10 views
Solve this problem.
asked Feb 8 TeamScript 13.5k points