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 4020008
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:10:29+00:00 2026-05-20T10:10:29+00:00

All sample function I’ve seen so far avoid, for some reason, returning a string.

  • 0

All sample function I’ve seen so far avoid, for some reason, returning a string. I am a total rookie as far as Java goes, so I am not sure whether this is intentional. I know that in C++ for example, returning a reference to a string is way more efficient than returning a copy of that string.

How does this work in Java?

I am particularly interested in Java for Android, in which resources are more limited than desktop/server environment.

To help this question be more focused, I am providing a code snippet in which I am interested in returning (to the caller) the string page:

public class TestHttpGet {
    private static final String TAG = "TestHttpGet";

    public void executeHttpGet() throws Exception {
    BufferedReader in = null;
    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI("http://www.google.com/"));
        HttpResponse response = client.execute(request);    // actual HTTP request

        // read entire response into a string object
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();

        String page = sb.toString();
        Log.v(TAG, page); // instead of System.out.println(page);
        }
    // a 'finally' clause will always be executed, no matter how the program leaves the try clause
    // (whether by falling through the bottom, executing a return, break, or continue, or throwing an exception).
    finally { 
            if (in != null) {
                try {
                    in.close();     // BufferedReader must be closed, also closes underlying HTTP connection
                } 
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

In the example above, can I just define:

    public String executeHttpGet() throws Exception {

instead of:

    public void executeHttpGet() throws Exception {

and return:

        return (page); // Log.v(TAG, page);
  • 1 1 Answer
  • 1 View
  • 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-05-20T10:10:30+00:00Added an answer on May 20, 2026 at 10:10 am

    A String in java corresponds more or less to std::string const * in c++. So, it’s cheap to pass around, and can’t be modified after it’s created (String is immutable).

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

Sidebar

Related Questions

Morning All, I was hoping someone could help or provide some sample code for
All, I am studying some sample code given in my web dev class as
I have some very simple sample code like this: $.ajax({ url: 'demo2.htm', success: function(loadeddata){
my sample code is here include 'simple_html_dom.php'; function get_all_links($url){ global $host; $html = new
So all I need is a simple function that sets it up (eating incoming
I use a simple JavaScript confirm function that is used by all pages in
I see all sample from nServiceBus to be a standalone process, so can it
I was trying to implement Spring AOP in web app. Unfortunately all the sample
I haven't been able to get this working and all of the sample code
As I know alpha composition uses straight alpha basically. But all GLES blending sample

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.