Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7956853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:07:06+00:00 2026-06-04T04:07:06+00:00

i have a question on lines 26 & 27: String dumb = input.nextLine(); output.println(dumb.replaceAll(REMOVE,

  • 0

i have a question on lines 26 & 27:

String dumb = input.nextLine();
output.println(dumb.replaceAll(REMOVE, ADD));

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:

output.println(new String(input.nextLine()).replaceAll(REMOVE, ADD));

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.
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?

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?

i’d appreciate it if you could clear this up for me.

thank you,

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.

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();


    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:07:08+00:00Added an answer on June 4, 2026 at 4:07 am

    The very, very first thing I’m going to say is this:

    Don’t worry about optimizing performance prematurely. The Java compiler is smart, it’ll optimize a lot of this stuff for you, and even if it didn’t you’re optimizing out incredibly tiny amounts of time. The stream IO you’ve got going there is already running for orders of magnitude longer than the amount of time you’re talking about.

    What is most important is how easy the code is to understand. You’ve got a nice code style, going from your example, so keep that up. Which of the two code snippets is easier for someone other than you to read? That is the best option. 🙂

    That said, here are some more specific answers to your questions:

    1. Garbage collection will absolutely pick up objects which are instantiated inside the scope of a loop. The fact that it’s instantiated inside the loop means that Java will already have marked it for clean up as soon as it fell out of scope. The next time GC runs, it will clean up all of those things which have been marked for clean up.

    2. Creating an object inline will still create an object. The constructor is still called, memory is still allocated… Under the hood, they are really, really similar. It’s just that in one case that object has a name, and in the other it doesn’t. You’re not going to save any real resources by combining two lines of code into one.

    3. “input.nextLine()” already returns a String, so you don’t need to wrap it in a new String(). (So yes, removing that actually will result in one less object being instantiated!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This Question arises from a Q&A here I have some doubts that i think
This question is along the lines of HTML table horizontal spacing We have an
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have the following string representing a date & time NSString *shortDateString = @Sat28Apr2012
I have another SQL/access 2007 question that seems really basic but I'm not sure
I have a function that should be passed a string with two numbers (as
Hi quick question about Jayrock... I have a Jayrock JSON-RPC web service that generally
I have two String.printable mysteries in the one question. First, in Python 2.6: >>>
I have a question. My input XML looks like <?xml version=1.0 encoding=UTF-8?> <Text> <Message>this
Trying to use jquery to output a string that contains a hyphen like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.