Question & Answer: (Java Programming)…..

(Java Programming)

Question 3:

1. Why should you use StringBuffer objects instead of String objects in a program that makes a lot of changes to strings?

2. Write a code snippet to demonstrate auto boxinng.

Expert Answer

 

Answer 1) String object are read only and immutable which means that string classes are used to manipulate those strings which can not be changed. Whereas in StringBuffer, the same can be modified at times. The StringBuffer is also considered faster if concatenation is to be considered. Therefore, StringBuffer objects are to be used in a program in which a lot of changes are to be made.

Answer 2)

class Somesh

{

public static void somi(Integer number)

{

System.out.println(number);

}

public static void main(String[] args) {

somi(2);

/* here we have passed int (primitive type), it would be converted to Integer object at Runtime which is autoboxing */

}

}

Still stressed from student homework?
Get quality assistance from academic writers!