简体   繁体   中英

Why does the value not change by the method?

I am a new java self-learning. I'm try to across a practice question (couldn't find the answer), but I ran into the following problem: the passenger cannot be changed following the station change.

public class BusGame {
    
    static int station, passenger;

    public static void main(String[] args) {
        station = 0;
        passenger = 0;
        
        for(station = 0; station <=5; station++) {
            System.out.println("Station\tPassenger");
            passengerChange(station,passenger);
            System.out.println(station + "\t" + passenger);
        }
    }
    
    public static int passengerChange(int s,int p) {
        if(s==1 || s==5) {
            p++;
        }
        if(s==2 || s==3) {
            p=p+2-1;
        }
        if(s==4) {
            p--;
        }
        return p;
    }
}

and the output is:

Station Passenger
0       0
Station Passenger
1       0
Station Passenger
2       0
Station Passenger
3       0
Station Passenger
4       0
Station Passenger
5       0

Can someone tell me why the "passenger" value cannot be changed but the "station" value can changed?

You are not using the value returned from passengerChange . Try

System.out.println(station + "\t" + passengerChange(station,passenger));

You should try this.passenger++ instead of p++.
p is a new integer value created inside passengerChange.

if you used else in the static function you could even completely remove p and s.

my idea whould look like thi:

public class BusGame {

static int station, passenger;

public static void main(String[] args) {
    station = 0;
    passenger = 0;
    
    for(station = 0; station <=5; station++) {
        System.out.println("Station\tPassenger");
        passengerChange();
        System.out.println(station + "\t" + passenger);
    }
}

public static int passengerChange() {
    if(this.station==1 || this.station==5) {
        this.passenger++;
    }else{
        if(this.station==2 || this.station==3) {
            this.passenger=this.passenger+2-1;
        }else{
            if(this.station==4) {
                this.passenger--;
    }}}
    return p;
}

}

I feel like there are a few concept you will benefit from knowing. Those are:

  1. Primitive datatype and Wrapper class In object-oriented programming, a wrapper class is a class that encapsulates types, so that those types can be used to create object instances and methods in another class that need those types.

So that means that primitive datatypes are like int a =5; whereas using wrapper classes I actually create an object of class Integer .

This is important to know because we can now learn:

  1. Call by value vs call by reference Java is always Pass by Value and not pass by reference.

Call by value: Call by Value means calling a method with a parameter as value. Through this, the argument value is passed to the parameter.

Call by reference: While Call by Reference means calling a method with a parameter as a reference. Through this, the argument reference is passed to the parameter.


In case of call by value original value is not changed. Note: numbers below are line numbers for reference

1. class Operation{  
2.  int data=50;  
  
3.  void change(int num){  
4.  num=data+100;//changes will be in the local variable only  
 }  
     
5. public static void main(String args[]){  
6.   Operation op=new Operation();  
  
7.   System.out.println("before change "+op.data);  
8.   op.change(500);  
9.   System.out.println("after change "+op.data);  
  
 }  
} 

Output will be:

Output:before change 50
       after change 50  

This is because num is int which is primitive. So when I pass 500 in line 8, and the method in line 3 receives it, changes it and assigns it it remains in that function only! (Search about this more)

If num was an object of a wrapper class then things would have been different. I would suggest you first learn these concepts then I will teach you what happens if num was Integer.

  1. keyword Return

A return statement causes the program control to transfer back to the caller of a method.

When a method returns anything, the control is transferred back to the statement where the method was call. We need to have a variable to catch the value returned. So your passengerChange(station,passenger); should have been:

passenger = passengerChange(station,passenger);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM