1
int value;
public int calculateStuff(int integer){
    value = integer + otherNumbersAndMathStuffs;

    //more math
    return mathResults
}


public int calculateStuff(int integer){
    int value = integer + otherNumbersAndMathStuffs;

    //more math
    return mathResults
}

Is one way better than another in terms of using processor power and or memory?

Gummby8
  • 85
  • 1
  • 2
  • 9

1 Answers1

0

The former can create problems in multi-threaded execution, whereas the latter wont.

  int value;
public int calculateStuff(int integer){
    value = integer + 1;

    System.out.println(value+100);
    return mathResults
} enter code here

Say, thread-1 and thread-2 are executing concurrently, calling the method with arguments 2,3 resp.

Say, Thread-1 enters the method and sets value to 3 and yield to thread-2

Thread-2 enters the method and sets the value to 4 and yield to thread-1

Thread-1 will then print 104

Thread-2 will print 104

user207421
  • 305,947
  • 44
  • 307
  • 483
Rimmy Mathew
  • 157
  • 7