Question

public class J {

     public Integer method(Integer x) 
        {
            Integer val = x;
            try 
            {

                    return val;
            } 
            finally 
            {
                     val = x + x;
            }
        }
        public static void main(String[] args) 
        {
            J littleFuzzy = new J();
            System.out.println(littleFuzzy.method(new Integer(10)));
        }

}

It will return "10".

Now I just replace Return type Integer to StringBuilder and Output was changed.

public class I {

     public StringBuilder method(StringBuilder x) 
        {
            StringBuilder val = x;
            try 
            {

                    return val;
            } 
            finally 
            {
                     val = x.append("aaa");
            }
        }
        public static void main(String[] args) 
        {
            I littleFuzzy = new I();
            System.out.println(littleFuzzy.method(new StringBuilder("abc")));
        }


}

OutPut is "abcaaa"

So, Anybody can explain me in detail.? what are the differences.?

Was it helpful?

Solution

Just because integer in immutable so after method returns even if value is changed in method it does not reflect, and does reflect in StringBuilder Object

EDIT:

public class J {
    public String method(String x) {
        String val = x;
        try {
            return val;
        } finally {
            val = x + x;
        }
    }

    public static void main(String[] args) {
        J littleFuzzy = new J();
        System.out.println(littleFuzzy.method("abc"));
    }
}

OTHER TIPS

The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.

For example, if z refers to a string builder object whose current contents are "start", then the method call z.append("le") would cause the string builder to contain "startle", whereas z.insert(4, "le") would alter the string builder to contain "starlet".

In general, if sb refers to an instance of a StringBuilder, then sb.append(x) has the same effect as sb.insert(sb.length(), x). Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.

Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.

In above method, finally block is calling everytime.

When an object is passed, the copy of its reference gets passed and you can change the contents if it is mutable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top