September 29, 2023

Java String valueOf() Method With Examples

Java string valueOf() method is used to convert values of different data types into their String representations.

valueOf() methods in String class

valueOf() method in Java String class is overloaded and has following variants-

  • String valueOf(boolean b)- Returns the string representation of the boolean argument. If the argument is true, a string equal to "true" is returned; otherwise, a string equal to "false" is returned.
  • String valueOf(char c)- Returns the string representation of the char argument.
  • String valueOf(int i)- Returns the string representation of the int argument.
  • String valueOf(long l)- Returns the string representation of the long argument.
  • String valueOf(float f)- Returns the string representation of the float argument.
  • String valueOf(double d)- Returns the string representation of the double argument.
  • String valueOf(char[] data)- Returns the string representation of the char array argument.
  • String valueOf(char[] data, int offset, int count)- Returns the string representation of a specific subarray of the char array argument.
  • String valueOf(Object obj)- Returns the string representation of the Object argument.

Java string valueOf() examples

valueOf() methods are handy utility methods to convert different data types to String so let’s see examples by passing different data types.

String.valueOf(boolean b)

This variant of valueOf() method takes a boolean value as argument and returns a String.

public class StringValueOf {
  public static void main(String[] args) {
    boolean flag1 = true;
    boolean flag2 = false;
    String str1 = String.valueOf(flag1);
    String str2 = String.valueOf(flag2);
    System.out.println(str1);
    System.out.println(str2);
  }
}
Output
true
false

String.valueOf(char c)

This variant of valueOf() method takes a character as argument and returns its String representation.

public class StringValueOf {
  public static void main(String[] args) {
    char c = 'a';
    String str = String.valueOf(c);
    System.out.println(str);
  }
}
Output
a

String.valueOf(int i)

In this variant of valueOf() method int is passed as argument and its String representation is returned.

public class StringValueOf {
  public static void main(String[] args) {
    int i = 89;
    String str = String.valueOf(i);
    System.out.println(str);
  }
}
Output
89

String.valueOf(long l)

In this variant of valueOf() method long value is passed as argument and its String representation is returned.

public class StringValueOf {
  public static void main(String[] args) {
    long l = 8978L;
    String str = String.valueOf(l);
    System.out.println(str);
  }
}
Output
8978

String.valueOf(float f)

In this variant of valueOf() method float value is passed as argument and its String representation is returned.

public class StringValueOf {
  public static void main(String[] args) {
    float f = 237.897f;
    String str = String.valueOf(f);
    System.out.println(str);
  }
}
Output
237.897

String.valueOf(double d)

In this variant of valueOf() method double value is passed as argument and its String representation is returned.

public class StringValueOf {
  public static void main(String[] args) {
    double d = 135.6814d;
    String str = String.valueOf(d);
    System.out.println(str);
  }
}
Output
135.6814

String.valueOf(char[] data)

This variant of valueOf() method returns the String representation of the passed char array argument.

public class StringValueOf {
  public static void main(String[] args) {
    char[] c = {'a', 'b', 'c', 'd'};
    String str = String.valueOf(c);
    System.out.println(str);
  }
}
Output
abcd

String.valueOf(char[] data, int offset, int count)

In this variant of the valueOf() method you can specify the range for the subarray that is converted to the String. The offset argument is the index of the first character of the subarray. The count argument specifies the length of the subarray.

public class StringValueOf {
  public static void main(String[] args) {
    char[] c = {'a', 'b', 'c', 'd'};
    String str = String.valueOf(c, 1, 2);
    System.out.println(str);
  }
}
Output
bc

String.valueOf(Object obj)

class Employee{
  int id;
  String name;
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  @Override
  public String toString() {
    return "id= " + getId() + " Name= " + getName();
  }	
}

public class StringValueOf {
  public static void main(String[] args) {
    Employee emp = new Employee();
    emp.setId(1);
    emp.setName("Stuart");
    String str = String.valueOf(emp);
    System.out.println(str);
  }
}
Output
id= 1 Name= Stuart

That's all for the topic Java String valueOf() Method With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment