LemonyFresh
LemonyFresh

Reputation: 15

Decrement operation in Java

So I've just started an IT course and as part of it we're learning to code in Java; I have an assignment for next week, and while I have it figured out, I just have a question as to why it works :P

The objective was to write a piece of code that reads a number, decrements it, turns it negative and then outputs it.

This is what I had originally:

  import java.util.Scanner;
  // imports the Scanner utility to Java

  public class Question3 {

public static void main(String[] args) {

    Scanner s = new Scanner(System.in);
    // defines the scanner variable and sets it to recognize inputs from the user

    System.out.println("Please enter a number: ");
    //prompts captures a number form the screen

    int a = s.nextInt();
    // defines an integer variable('a') as to be  set by input from the scanner

    --a;
    // decrement calculation( by 1)
    -a;     
    //inverts the value of a 

    System.out.println("Your number is: " + a );
    // outputs a line of text and the value of a

However, Eclipse(the IDE I'm using) wouldn't recognise the unary minus operator ( '-'), so it didn't work. I got it working by tweaking it to be written as follows:

 import java.util.Scanner;
// imports the Scanner utility to Java

 public class Question3 {

public static void main(String[] args) {

    Scanner s = new Scanner(System.in);
    // defines the scanner variable and sets it to recognize inputs from the user

    System.out.println("Please enter a number: ");
    //prompts captures a number form the screen

   int a = s.nextInt();
    // defines an integer variable('a') as to be  set by input from the scanner

    --a;
    // decrement calculation( by 1)

    System.out.println("Your number is: " + (-a) );
    // outputs a line of text and the inverse of the variable 'a' 

My question here is, why would the unary minus work in the second instance but not the first?

Upvotes: 1

Views: 622

Answers (4)

Suresh Atta
Suresh Atta

Reputation: 121998

-   Unary minus operator; negates an expression

In your case

 -a;  

That's an statement.

"Your number is: " + (-a) 

That's an expression.

Upvotes: 0

Pshemo
Pshemo

Reputation: 124215

--a

is similar to

a = a - 1;

which means at first it calculates value of a-1 and then with a = ... it assigns that value back to a.

But in case of -a you are just calculating negative value, but it doesn't doesn't reassign it back to a. So since you are not doing anything with that calculated value it will be lost so compiler informs you that your code doesn't do what you may think it does.

Try explicitly assign that result back to a with

a = -a;

After this instruction a will hold new value which you can use anywhere.


This problem disappears when you are using

System.out.println("Your number is: " + (-a) );

since now compiler sees that calculated value -a is being used (as part of value passed to println method).

Upvotes: 1

Mage Xy
Mage Xy

Reputation: 1803

As Elliott Frisch explained, you have to use the negation operator (-) to reassign the value back to the original variable before you can access it.

But why does the decrement operator (--) not require you to do this? It's because a-- is more or less syntactic sugar for a = a - 1. It's just quicker to write, and it's common enough that everyone knows what it means.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201399

Because you didn't assign the result of the unary minus. The pre-decrement includes an assignment.

 a = -a; // <-- like this.

In the second use (printing), you are using the value in the print routine (and not updating a).

Upvotes: 1

Related Questions