0

I've decided to learn some java. I've come across something that doesn't seem to make much sense. An array gets permanently modified after being passed to a method.

This little project works pretty good. The project passes an array having a middle name to a method which corrects the middle name. The middle names getting passed to the method have only a middle initial. The method replaces the middle initial with the full middle name.

A name can have different lengths from 3 to more elements. A name might include titles (Dr. Mr. Mrs, etc ), or the name might include a hyphened last-name, or a name might include a suffix (Jr, II, IV, etc .. ). But the name being passed will always include at least (first name, middle initial, last name). The method only replaces the middle initial with the full middle name.

There are 3 parameters passed to the method (the name array, a string holding the full middle name, and an integer 'position' that represents the index position of the middle-initial).

The method returns a Boolean value. If the user passes a bad position that is outside the length of the array, then the method immediately returns, false.

So ... all this said to ask one simple question: since the method only returns a Boolean value, how does the original array become permanently changed after it is passed to the method, yet oddly, the insert position does not get permanently change by the method ?

About the code: There are multiple redundant and unnecessary lines of code to print the code as the code gets executed. The java code is in two different java files (the main method file, the method file ).

Code main method:

import java.util.Scanner;

public class MiddleName {

    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    

    System.out.println("Enter array length:");
    int len = scan.nextInt();
    scan.nextLine();
    String[] wordList = new String[len];
    
    System.out.println("Enter values:");
    for(int i = 0; i < len; i++){
      wordList[i] = scan.nextLine();
    }

    System.out.println("\n" + "The values entered were: ");
    for( String e: wordList) {
        System.out.println(e);
    }
    System.out.println("\n");
// ============================================================

    System.out.println("Enter the replacement String:");
    String replace = scan.nextLine();

    System.out.println("Enter the replacement position:");
    int pos = scan.nextInt();

   // method call with parameters);   
    System.out.println("Method return: " + other.insert(wordList, replace, pos));
    
    // Prints the returned array (with the changes)
    System.out.print("Array contents: {");
    for(int i = 0; i < len; i++){
      System.out.print(wordList[i] + ", ");
    }
    System.out.println("}");

    System.out.println();
    for(String e: wordList) {
      System.out.println(e);
    }

    System.out.println("The returned pos value is: " + pos);

    scan.close();
}   // end main

} // end class

Code: Other

public class other {

// fields
    
public static boolean insert(String words [], String newWord, int place) {
    boolean goNOgo = false;
    int len = words.length;
  
    System.out.println("\n" + "In the constructor method" + "\n");
 
    for(String e: words) {
        System.out.print(e + ", ");
    }
    System.out.println("\n" + "The insertion point is: " + place + ", and the replacement value is: " + newWord);

    if(place > len) {       
        return false;     // if the place > length of array, then all fails and return as: false 
    } else {                                   // if the place value is not greater than array.lenght
        for(int i = 0; i < place; i ++) {
        }       
    }

    words[place] = newWord;    // insert the replacement word

    System.out.println("\n");        // print the updated array
    for(String e: words){
        System.out.println(e);
    }

    System.out.println("\n" + "In method, the updated array value(s) are: " + "\n");
        for(String e: words) {
            System.out.print(e + " ");
        }
    System.out.println("\n");

    place = 50;

    goNOgo = true;

    return goNOgo;

     }   // method
}  // class

The code entered and the resulting output:

Enter array length:
4
Enter values:
Mr
John
D
Doe

The values entered were:     
Mr
John
D
Doe


Enter the replacement String:
David
Enter the replacement position:
2

In the constructor method

Mr, John, D, Doe,
The insertion point is: 2, and the replacement value is: David


Mr
John
David
Doe

In method, the updated array value(s) are:

Mr John David Doe

Method return: true
Array contents: {Mr, John, David, Doe, }

Mr
John
David
Doe
The returned pos value is: 2
Gray
  • 1,164
  • 1
  • 9
  • 23
  • 1
    In java, you pass references when dealing with objects. So when you pass an array to myMethod(), the reference in myMethod still points to the exact same array. `int` is a primitive and those behave differently (a copy is made of the value that was passed on the methods stack, so the two int references point to two different ints in memory) – RobOhRob Jan 20 '21 at 03:58
  • Hey, thanks for that answer. That answer solves my mystery of the array and the integer being changed and not. It's a pisser than my question was immediately flagged as "duplicate" because I looked long and hard for any similar questions. And in reality, 99.9% of the questions asked on Stack are poor quality questions that have definitely been asked before. – Gray Jan 20 '21 at 04:25

0 Answers0