• 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

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 14 views
14 views
Problem: I have been told several definitions for it, looked on Wikipedia, but as a beginner to Java I'm still not sure what it means. Anybody fluent in Java? public class Foo { public static void doStuff(){ // does stuff } } So, instead of creating an instance of Foo and then calling doStuff like this:
asked Dec 3, 2020 Mashhoodch 6.1k points
0 votes
1 answer 31 views
31 views
Please answer this. I am new to this. I was doing that but I got into some problems.
asked Nov 7, 2020 TeamScript 10.3k points
0 votes
1 answer 43 views
43 views
I was working on that but I got into some problems.
asked Nov 6, 2020 TeamScript 10.3k points
0 votes
1 answer 25 views
25 views
Problem: Which \n is an escape character for strings that is replaced with the new line object. ? How to Writing \n in a string that prints out will print out a new line instead of the \n. Java Escape Characters.
asked Nov 6, 2020 Mashhoodch 6.1k points
0 votes
1 answer 32 views
32 views
Problem: % How Percent sign(%) acts as a modulus operator in java programming language?Modulus operator is an operator that works on integers and yields the remainder when one number is divided by another. Thus x = A%B means that x will carry the value of the remainder when dividing A by B.
asked Nov 6, 2020 Mashhoodch 6.1k points
0 votes
1 answer 55 views
55 views
I was working on that. I just want to know how to do that?
asked Nov 5, 2020 TeamScript 10.3k points
0 votes
1 answer 22 views
22 views
I was working on that. I just want to know how to do that?
asked Nov 5, 2020 TeamScript 10.3k points
0 votes
1 answer 11 views
11 views
Problem: IS != operator is a comparison operator,?where to used in conditional expressions. Isreads, “not equal”. If the compared values are not equal to each other than the expression returns true. An example of a program that uses both the && and !=
asked Nov 5, 2020 Mashhoodch 6.1k points
0 votes
1 answer 16 views
16 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, 2020 Code Learner 5.5k points
0 votes
1 answer 21 views
21 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, 2020 Code Learner 5.5k points