title: which of the two is a better way of creating and destroying objects?
body: <p>i have a question on lines 26 &amp; 27:</p>
  
  <pre><code>String dumb = input.nextLine();
  output.println(dumb.replaceAll(REMOVE, ADD));
  </code></pre>
  
  <p>i was hoping that i'd be able to shrink this down to a single line and be able to save space, so i did:</p>
  
  <pre><code>output.println(new String(input.nextLine()).replaceAll(REMOVE, ADD));
  </code></pre>
  
  <p>but now i'm wondering about performance.  i understand that this program is quiet basic and doesn't need optimization, but i'd like to learn this.<br>
  the way i look at it, in the first scenario i'm creating a string object dumb, but once i leave the loop the object is abandoned and the JVM should clean it up, right?  but does the JVM clean up the abandoned object faster than the program goes through the loop?  or will there be several string objects waiting for garbage collection once the program is done?</p>
  
  <p>and is my logic correct that in the second scenario the String object is created on the fly and destroyed once the program has passed through that line?  and is this in fact a performance gain?</p>
  
  <p>i'd appreciate it if you could clear this up for me.</p>
  
  <p>thank you,</p>
  
  <p>p.s. in case you are wondering about the program (i assumed it was straight forward) it takes in an input file, and output file, and two words, the program takes the input file, replaces the first word with the second and writes it into the second file.  if you've actually read this far and would like to suggest ways i could make my code better, PLEASE DO SO.  i'd be very grateful. </p>
  
  <pre><code>import java.io.File;
  import java.util.Scanner;
  import java.io.PrintWriter;
  
  public class RW {
  
      public static void main(String[] args) throws Exception{
          String INPUT_FILE = args[0];
          String OUTPUT_FILE = args[1];
          String REMOVE = args[2];
          String ADD = args[3];
  
          File ifile = new File(INPUT_FILE);
          File ofile = new File(OUTPUT_FILE);
  
          if (ifile.exists() == false) {
              System.out.println("the input file does not exists in the current folder");
              System.out.println("please provide the input file");
              System.exit(0);
          }
  
          Scanner input = new Scanner(ifile);
          PrintWriter output = new PrintWriter(ofile);
  
          while(input.hasNextLine()) {
              String dumb = input.nextLine();
              output.println(dumb.replaceAll(REMOVE, ADD));
          }
          input.close();
          output.close();
  
  
      }
  }
  </code></pre>