-1

In Java, the output of s is 0. I do not understand why and would it be possible to somehow get the correct value of s (1000 here)?

public static void main(String args) {
    int s = 0;
    List<Integer> list = getList(s);
    System.out.println("s = " + s);
}

public static List<Integer> getList(int s) {

    List<Integer> list = new ArrayList<Integer>();
    for (int i = 0; i < 1000; i++) {
        list.add(i); s++;
    }
}

In C# there were out descriptors to indicate that the variable is going to change if I'm not mistaken..

I'm not going to get the list.size() in general!

Sophie Sperner
  • 4,428
  • 8
  • 35
  • 55
  • You pass s (and primitives in general) by value in Java. So s in your main function wont be touched by the function getList() only its value gets passed to it. – Balázs Édes Aug 14 '12 at 19:42
  • java is pass by value: further readings: http://stackoverflow.com/questions/2824910/output-parameter-in-java http://www.yoda.arachsys.com/java/parameters.html http://stackoverflow.com/questions/2806545/does-java-have-something-like-cs-ref-and-out-keywords – Colin D Aug 14 '12 at 19:42
  • The output isn't `0`: it won't compile. There's no return statement. – Mechanical snail Aug 15 '12 at 22:58

4 Answers4

4

In Java, all method arguments are passed by value, i.e. copy. So, changes to the copy are not visible to the caller.

To address your second question, you can just use list.size() on the caller side.

Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67
2

I see two ways

1) Make 's' as static variable and move it to class level

2) Create class with getter/setter for list and int and return the object for getList call

public static MyWrapperObj getList(int s) {

   ......
return wrapperObj
}

class MyWrapperObj 
{
private List<Integer>;
private countS;
....
//getter/setters.
}
kosa
  • 65,990
  • 13
  • 130
  • 167
2

Java doesn't allow for passing parameters by reference - but you could wrap it in an object like this:

class IntHolder {
        private int s;
        IntHolder(int s){
            this.s = s;
        }

        public void setS(int s){
            this.s = s;
        }

        public int getS(){
            return s;
        }

        public void increment(){
            s++;
        }
}

class Test{

  public static void main(String[] args) {
    IntHolder s = new IntHolder(0);
    List<Integer> list = getList(s);
    System.out.println("s = " + s.getS());
  }

  public static List<Integer> getList(IntHolder s) {

    List<Integer> list = new ArrayList<Integer>();
    for (int i = 0; i < 1000; i++) {
        list.add(i); s.increment();
    }
    return list;
  }
}
jcern
  • 7,798
  • 4
  • 39
  • 47
0

In java, arguments passed to methods are passed by value.. you will need to make s a global or instance variable in order to modify it in other methods. This is just the way java works. e.g.

public class Test{
     private int s;

     public Test(){
          s=0;
          increment();
          //print now will be 1000.
     }

     private void increment(){
          s = 1000;
     }
}
Joel
  • 4,732
  • 9
  • 39
  • 54