开发者

How efficient it is to return a string in Java

开发者 https://www.devze.com 2023-02-14 16:30 出处:网络
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,

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 开发者_StackOverflowof 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);


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


String is a reference type - so when you return a string, you're really just returning a reference. It's dirt cheap. It's not copying the contents of the string.


In java most of the time you return something, you return it by reference. There's no object copying or cloning of any kind. So it is fast.

Also, Strings in Java are immutable. No need to worry about that either.

0

精彩评论

暂无评论...
验证码 换一张
取 消