In this article, we will discuss String’s valueOf() method which converts primitive data-types & objects into its equivalent string representation
1. String’s valueOf() method:
- This String method is used to return string representation of primitive data-types & Objects
- Note: There are 9 variants or overloaded valueOf() method, those are,
1.1 Method Signature:
public static String valueOf(boolean b);
public static String valueOf(char c);
public static String valueOf(char[] data);
public static String valueOf(char[] data, int offset, int count);
public static String valueOf(int i);
public static String valueOf(long l);
public static String valueOf(float f);
public static String valueOf(double d);
public static String valueOf(Object obj);
1.2 Returns:
- Returns string representation of primitive data-types and Objects
- Primitive data-types are boolean , char, int, long, float, double
- It also returns string representation for char[] array
2. Examples on valueOf() method:
2.1 To get value in string format/representation for the specified boolean primitive data-type
StringValueOfMethodForBoolean.java
package in.bench.resources.string.valueof.methods;
public class StringValueOfMethodForBoolean {
public static void main(String[] args) {
// define boolean variable
boolean bTest = true;
boolean bTest2 = false;
// convert to String using valueOf(boolean) method
String str1 = String.valueOf(bTest);
String str2 = String.valueOf(bTest2);
// print to console
System.out.println("boolean true in String format : "
+ str1);
System.out.println("boolean false in String format : "
+ str2);
}
}
Output:
boolean true in String format : true
boolean false in String format : false
2.2 To get value in string format/representation for the specified char primitive data-type
StringValueOfMethodForChar.java
package in.bench.resources.string.valueof.methods;
public class StringValueOfMethodForChar {
public static void main(String[] args) {
// define char variable
char chTest = 'A';
char chTest2 = 'a';
// convert to String using valueOf(char) method
String str1 = String.valueOf(chTest);
String str2 = String.valueOf(chTest2);
// print to console
System.out.println("char 'A' in String format : " + str1);
System.out.println("char 'a' in String format : " + str2);
}
}
Output:
char 'A' in String format : A
char 'a' in String format : a
2.3 To get value in string format/representation for the specified char[] arrays
StringValueOfMethodForCharArrays.java
package in.bench.resources.string.valueof.methods;
public class StringValueOfMethodForCharArrays {
public static void main(String[] args) {
// define char[] variable
char[] chArrayTest1 = {'B', 'e', 'n', 'c', 'h'};
char[] chArrayTest2 = {'R', 'e',
's', 'o', 'u', 'r', 'c', 'e', 's'};
// convert to String using valueOf(char[]) method
String str1 = String.valueOf(chArrayTest1);
String str2 = String.valueOf(chArrayTest2);
// print to console
System.out.println("char[] in String format : " + str1);
System.out.println("char[] in String format : " + str2);
}
}
Output:
char[] in String format : Bench
char[] in String format : Resources
2.4 To get value in string format/representation for the specified double primitive data-type
StringValueOfMethodForDouble.java
package in.bench.resources.string.valueof.methods;
public class StringValueOfMethodForDouble {
public static void main(String[] args) {
// define double variable
double dblTest1 = 19.23456;
double dblTest2 = 258.589635;
// convert to String using valueOf(double) method
String str1 = String.valueOf(dblTest1);
String str2 = String.valueOf(dblTest2);
// print to console
System.out.println("double(19.23456) in String format :"
+ str1);
System.out.println("double(258.589635) in String format :"
+ str2);
}
}
Output:
double(19.23456) in String format : 19.23456
double(258.589635) in String format : 258.589635
2.5 To get value in string format/representation for the specified float primitive data-type
StringValueOfMethodForFloat.java
package in.bench.resources.string.valueof.methods;
public class StringValueOfMethodForFloat {
public static void main(String[] args) {
// define float variable
float fTest1 = 19.23456f;
float fTest2 = 258.589635f;
// convert to String using valueOf(float) method
String str1 = String.valueOf(fTest1);
String str2 = String.valueOf(fTest2);
// print to console
System.out.println("float(19.23456f) in String format :"
+ str1);
System.out.println("float(258.589635f) in String format :"
+ str2);
}
}
Output:
float(19.23456f) in String format : 19.23456
float(258.589635f) in String format : 258.58963
Q) Difference between float and double ?
- Both are used to represent floating numbers where
- Primitive float data-type is 32-bit and always followed with suffix ‘f’
- Primitive double data-type is 64-bit
2.6 To get value in string format/representation for the specified int primitive data-type
StringValueOfMethodForInteger.java
package in.bench.resources.string.valueof.methods;
public class StringValueOfMethodForInteger {
public static void main(String[] args) {
// define int variable
int intTest1 = 19;
int intTest2 = 333;
// convert to String using valueOf(int) method
String str1 = String.valueOf(intTest1);
String str2 = String.valueOf(intTest2);
// print to console
System.out.println("int(19) in String format : "
+ str1);
System.out.println("int(333) in String format : "
+ str2);
// after converting, concatenation of 2 string is possible
System.out.println("\nConcatenated String : "
+ str1 + str2);
}
}
Output:
int(19) in String format : 19
int(333) in String format : 333
Concatenated String : 19333
2.7 To get value in string format/representation for specified long primitive data-type
StringValueOfMethodForLong.java
package in.bench.resources.string.valueof.methods;
public class StringValueOfMethodForLong {
public static void main(String[] args) {
// define int variable
long lTest1 = 1912345;
long lTest2 = 333444999;
// convert to String using valueOf(long) method
String str1 = String.valueOf(lTest1);
String str2 = String.valueOf(lTest2);
// print to console
System.out.println("long(1912345) in String format : "
+ str1);
System.out.println("long(333444999) in String format : "
+ str2);
// after converting, concatenation of 2 string is possible
System.out.println("\nConcatenated String : "
+ str1 + str2);
}
}
Output:
long(1912345) in String format : 1912345
long(333444999) in String format : 333444999
Concatenated String : 1912345333444999
Q) Difference between int and long ?
- Primitive int data-type is 32-bit
- Primitive long data-type is 64-bit
2.8 To get value in string format/representation for the specified object
Let’s define Employee object with 2 attributes
Employee.java
package in.bench.resources.string.valueof.methods;
public class Employee {
// member variables
int empId;
String empName;
// 2-arg parameterized constructor
public Employee(int empId, String empName) {
super();
this.empId = empId;
this.empName = empName;
}
// getters and setters
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
// overriding toString() method
@Override
public String toString() {
return "Employee [empId=" + empId
+ ", empName=" + empName
+ "]";
}
}
StringValueOfMethodForObject.java
package in.bench.resources.string.valueof.methods;
public class StringValueOfMethodForObject {
public static void main(String[] args) {
// define 2 employee objects
Employee emp1 = new Employee(1001, "SJ");
Employee emp2 = new Employee(1002, "AK");
// convert to String using valueOf(long) method
String str1 = String.valueOf(emp1);
String str2 = String.valueOf(emp2);
// print to console
System.out.println("Employee object 1 in String format :"
+ str1);
System.out.println("Employee object 2 in String format :"
+ str2);
}
}
Output:
Employee object 1 in String format :
Employee [empId=1001, empName=SJ]
Employee object 2 in String format :
Employee [empId=1002, empName=AK]
Hope, you found this article very helpful. If you have any suggestions or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.
Related Articles:
- Java – String charAt(int index) method
- Java – String compareTo(String anotherString) method
- Java – String compareToIgnoreCase(String str) method
- Java – String concat(String str) method
- Java – String contains(CharSequence s) method
- Java – String contentEquals(StringBuffer sb) method
- Java – String copyValueOf(char[] data) method (2)
- Java – String endsWith(String suffix) method
- Java – String equals(Object anObject) method
- Java – String equalsIgnoreCase(Object anObject) method
- Java – String format(String format, Object… args) method
- Java – String getBytes() method (4)
- Java – String getChars() method
- Java – String hashCode() method
- Java – String indexOf() method (4)
- Java – String intern() method
- Java – String isEmpty() method
- Java – String join() method (2)
- Java – String lastIndexOf() method (4)
- Java – String length() method
- Java – String matches(String regex) method
- Java – String regionMatches() method (2)
- Java – String replace(char oldChar, char newChar) method
- Java – String replace(CharSequence target, CharSequence replacement) method
- Java – String replaceAll(String regex, String replacement) method
- Java – String replaceFirst(String regex, String replacement) method
- Java – String split(String regex) method
- Java – String split(String regex, int limit) method
- Java – String startsWith(String prefix) method
- Java – String startsWith(String prefix, int toffset) method
- Java – String subSequence(int beginIndex, int endIndex) method
- Java – String substring(int beginIndex) method
- Java – String substring(int beginIndex, int endIndex) method
- Java – String toCharArray() method
- Java – String toLowerCase() method (2)
- Java – String toUpperCase() method (2)
- Java – String toString() method
- Java – String trim() method
- Java – String valueOf() method (9)
References:
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Happy Coding !!
Happy Learning !!