How to Define Method to Take and Return String Data in Java
Is char[ ], String, StringBuffer, or StringBuilder the best way to take and return String data in Java? Take a look at this comparison.
Join the DZone community and get the full member experience.
Join For FreeIn Java, char[]
, String
, StringBuffer
, and StringBuilder
are used to store, take, and return string data. One question that may come to your mind is, "What is the best way for defining a method to take string data and return string data in Java?"
We have four ways to take and return string data:
1) char[]
/byte[]
2) String
3) StringBuilder
4) StringBuffer
The char[]
is not a better option because it works on an array. Taking and returning string data using char[]
is not a better option.
The second way is by using String class. The String class is immutable. On every modification, it creates a new object. In this way, we are creating many new objects which are not actually required for our program. So, it is not recommended to only work with the String class.
This Java program to reverse String shows that, for a small task that is for reversing “Hello” it creates five objects.
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
// take input
Scanner scan = new Scanner(System.in);
System.out.print("Enter string: ");
String str = scan.nextLine();
// reverse the string
String reverseStr = reverseStringData(str);
// display result string
System.out.println( "Reverse String: " +
reverseStr);
}
public static String reverseStringData(String s) {
String rev = new String();
for(int i=s.length()-1; i>=0; i--){
rev = rev + s.charAt(i);
// On every iteration new string
// object will be created
System.out.println("Address of rev: "+
System.identityHashCode(rev));
}
return rev;
}
}
Output:
xxxxxxxxxx
Enter string: Hello
Address of rev: 531885035
Address of rev: 1044036744
Address of rev: 1826771953
Address of rev: 1406718218
Address of rev: 245257410
Reverse String: olleH
For big tasks, it will create so many string class objects, which actually aren't needed in the program. In this way, we are wasting memory.
Using Only StringBuilder To Take and Return String Data
The third way is by using the StringBuilder
class. The StringBuilder
class is mutable. On every modification, it doesn’t create a new object, but it stores it in the same object. But the problem comes here: since we are using StringBuilder
to take an argument, the caller method should pass the value in the StringBuilder
form. Again, we are returning the string data using StringBuilder
due to this the caller method should store returning value as StringBuilder
.
The caller method developer needs to create a new StringBuilder
object and convert the string data to StringBuilder
just because, the called method taking argument is in StringBuilder
form not in the String form. Similarly, they need to create a StringBuilder
object just because the called method is returning the string data as StringBuilder
.
xxxxxxxxxx
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
// take input
Scanner scan = new Scanner(System.in);
System.out.print("Enter string: ");
String str = scan.nextLine();
// The reverseStringData() takes StringBuilder
// objects we need to convert
// string into StringBuilder
StringBuilder sb = new StringBuilder(str);
// Now, the return type is also StringBuilder
// So, create new StringBuilder class object to
// receive the return value
StringBuilder reversrSb = reverseStringData(sb);
// find reverse
String reverseStr = new String(reversrSb);
// display result
System.out.println("Reverse String: "+reverseStr);
}
public static StringBuilder reverseStringData(StringBuilder sb){
StringBuilder rev = new StringBuilder("");
for(int i=sb.length()-1; i>=0; i--){
rev.append(sb.charAt(i));
System.out.println("Address of rev: "+
System.identityHashCode(rev));
}
return rev;
}
}
Output:
xxxxxxxxxx
Enter string: Hello
Address of rev: 980546781
Address of rev: 980546781
Address of rev: 980546781
Address of rev: 980546781
Address of rev: 980546781
Reverse String: olleH
It doesn’t create a new StringBuilder object, so no memory is wasted but inside the caller method we need to write some extra logics for converting the string to StringBuilder and later StringBuilder to a string. So, it is also not the recommended way. We should simplify this things.
Using Only StringBuffer to Take and Return String Data in Java
The fourth way is by using the StringBuffer class. The StringBuffer class is similar to the StringBuilder class. In implementation no different will be there. StringBuffer class is synchronized so it is better for a multi-thread model application, not for the single thread model application. The StringBuffer class also will create the only object as StringBuilder, but we need to write extra logic for converting StringBuffer to String and String to StringBuffer.
xxxxxxxxxx
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
// take input Scanner scan = new Scanner(System.in);
System.out.print("Enter string: ");
String str = scan.nextLine();
// The reverseStringData() takes StringBuffer
// object so converting string into StringBuffer
StringBuffer sb = new StringBuffer(str);
// Now, the return type is also StringBuffer
// So, create new StringBuffer class object
// to receive the return value
StringBuffer reversrSb = reverseStringData(sb);
// find reverse
String reverseStr = new String(reversrSb);
// display result
System.out.println("Reverse String: "+reverseStr);
}
public static StringBuffer reverseStringData(StringBuffer sb){
StringBuffer rev = new StringBuffer("");
for(int i=sb.length()-1; i>=0; i--){
rev.append(sb.charAt(i));
System.out.println("Address of rev: " +
System.identityHashCode(rev));
}
return rev;
}
}
Enter string: Hello
Address of rev: 980546781
Address of rev: 980546781Address
of rev: 980546781
Address of rev: 980546781
Address of rev: 980546781
Reverse String: olleH
This is also not recommended.
Then how We Should Write the Method?
We should take and return the argument as a string class object. In this way, in the caller method, we don’t need to write any additional logic in the caller method.
In the method, we should convert the String class object to the the StringBuilder
class, process the logic on the StringBuilder class, and then convert the StringBuilder class object to string class object and return it.
Take argument as String class object => convert String class object to StringBuilder class object => process the business logic => convert the StringBuilder class object to String class object => return String class object.
For converting String to StringBuilder class there is only one way, by using the StringBuilder(String) constructor.
For converting StringBuilder to String class object there are three ways are there:-
- toString()
- valueOf()
- String(StringBuilder)
Among these three ways, the first ways i.e. using the toString() method is better than the remaining two.
xxxxxxxxxx
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
// take input
Scanner scan = new Scanner(System.in);
System.out.print("Enter string: ");
String str = scan.nextLine();
// find reverse
String reverseStr = reverseStringData(str);
// display result
System.out.println("Reverse String: " +
reverseStr);
}
public static String reverseStringData(String sb){
// Converting string class object to
// StringBuilder class object
StringBuilder rev = new StringBuilder("");
// iterate loop
for(int i=sb.length()-1; i>=0; i--){
rev.append(sb.charAt(i));
System.out.println("Address of rev: "+
System.identityHashCode(rev));
}
// converting stringBuilder class object
// to string class object
// and returning it to caller method
return rev.toString();
}
}
Enter string: Hello
xxxxxxxxxx
Address of rev: 980546781
Address of rev: 980546781
Address of rev: 980546781
Address of rev: 980546781
Address of rev: 980546781
Reverse String: olleH
Opinions expressed by DZone contributors are their own.
Comments