-2

I am working on a Android project that includes Arrays.I know that Java is a passByValue kind of language but there are some exceptions for objects, and arrays are objects.Here is what I want to do : Firstly I am going to check if EditText is null or not, if is null I will put also null for my float array if not I want to put value from EditText.

EditText acDifSp;
EditText deScMoSp;
EditText toReStVa;
EditText fePuStToVa;
EditText deShDoToVa;
EditText deBoSeSp;
EditText beTem1;
EditText beTem2;
EditText vibVale;
EditText poPuPa;
EditText SlWeinSlPu;

Float   acDifSpFl;
Float   deScMoSpFl;
Float   toReStVaFl;
Float   fePuStToVaFl;
Float   deShDoToVaFl;
Float   deBoSeSpFl;
Float   beTem1Fl;
Float   beTem2Fl;
Float   vibValeFl;
Float   poPuPaFl;
Float   SlWeinSlPuFl;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.setting_page);

    acDifSp=(EditText)findViewById(R.id.editText1);
    deScMoSp=(EditText)findViewById(R.id.editText2);
    toReStVa=(EditText)findViewById(R.id.editText3);
    fePuStToVa=(EditText)findViewById(R.id.editText4);
    deShDoToVa=(EditText)findViewById(R.id.editText5);
    deBoSeSp=(EditText)findViewById(R.id.editText6);
    beTem1=(EditText)findViewById(R.id.editText7);
    beTem2=(EditText)findViewById(R.id.editText8);
    vibVale=(EditText)findViewById(R.id.editText9);
    poPuPa=(EditText)findViewById(R.id.editText10);
    SlWeinSlPu=(EditText)findViewById(R.id.editText11);

EditText[] fields={acDifSp,deScMoSp,toReStVa,fePuStToVa,deShDoToVa,deBoSeSp,beTem1,beTem2,vibVale,poPuPa,SlWeinSlPu};
        Float[] values={acDifSpFl,deScMoSpFl,toReStVaFl,fePuStToVaFl,deShDoToVaFl,deBoSeSpFl,beTem1Fl,beTem2Fl,vibValeFl,poPuPaFl,SlWeinSlPuFl};
        for (int i = 0; i < fields.length; i++) {
            EditText currentField=fields[i];
            if(currentField.getText().toString().length()>0){
                values[i]=Float.parseFloat(currentField.getText().toString());
            }
        }

}

I tried this because, I don't want to check every EditText with if. Is this way correct ? or can anyone suggest another way ?

Mete
  • 31
  • 6

1 Answers1

0

In Java you always pass by value. There are no exceptions. The issue that you see is that, when you pass a reference to an object, you pass by value the reference, not the object. So, you get a copy of the reference but both the original and the copy point to the same object.

So, if you do

private void addOne(List<Integer> list2) {
    // Point 1
    list2.add(1);
    list2 = null;
}

public void test() {
    List<Integer> list1 = new ArrayList<Integer>();
    addOne(list1);
    System.out.println(list1.size()); // point 2
}

In Point 1 you have 2 different variables/references (list1 and list2) that point to the same list object. And, if you were passing by reference the parameter, since you nullify list2, you would have a NullPointerException in Point 2 (which does not happen in Java).

Pshemo
  • 122,468
  • 25
  • 185
  • 269
SJuan76
  • 24,532
  • 6
  • 47
  • 87