Data Conversion Using valueOf( )

  • The valueOf( ) method converts data from its internal format into a human-readable form.
  • It is a static method that is overloaded within String for all of Java’s built-in types so that each type can be converted properly into a string.

  • valueOf( ) is also overloaded for type Object, so an object of any class type can also be used as an argument.

static String valueOf(double num)

static String valueOf(long num)

static String valueOf(Object ob)

static String valueOf(char chars[ ])

  • These methods are called when there is a need for other data types to be String type.
  • There is a special version of valueOf( ) that allows to specify a subset of a char array by using the following general form:

static String valueOf(char chars[ ], int startIndex, int numChars)

  • chars is the array that holds the characters,.

  • startIndex is the index into the array of character at which the desired substring begins.

  • numChars specifies the length of the substring.

//example for valueOf() method
public class ValueOfExample
{
  public static void main(String args[])
  {
    double d=1.2345;
    long l=13132345;
    char ch[] = {'a','b','c','d'};
    ValueOfExample ob= new ValueOfExample();
    System.out.println(String.valueOf(d));
    System.out.println(String.valueOf(l));
    System.out.println(String.valueOf(ch));
    System.out.println(String.valueOf(ob));
    System.out.println(String.valueOf(ch,1,3));
  }
}

Output:

1.2345
13132345
abcd
ValueOfExample

bcd

Changing the Case of Characters Within a String

  • toLowerCase( ) converts all the characters in a string from uppercase to lowercase.

  • toUpperCase( ) method converts all the characters in a string from lowercase to uppercase.

  • Nonalphabetical characters, such as digits, are unaffected.

The general forms are:

String toLowerCase( )

String toUpperCase( )

  • Both methods return a String object that contains the uppercase or lowercase equivalent of the invoking String.
// Demonstrate toUpperCase() and toLowerCase().
public class ChangeCase
{
 public static void main(String args[])
 {
 String s = "This is a test.";
 System.out.println("Original: " + s);
 String upper = s.toUpperCase();
 String lower = s.toLowerCase();
   System.out.println("Uppercase: " + upper);
 System.out.println("Lowercase: " + lower);
 }
}

Output:

Original: This is a test.

Uppercase: THIS IS A TEST.

Lowercase: this is a test.

Joining Strings

  • JDK 8 adds a new method to String called join( ).
  • It is used to concatenate two or more strings, separating each string with a delimiter, such as a space or a comma.

  • It has two forms.

static String join(CharSequence delim, CharSequence . . . strs)

  • delim specifies the delimiter used to separate the character sequences specified by strs.

  • The second form of join( ) join a list of strings obtained from an object that implements the Iterable interface.

  • Iterable is implemented by the Collections Framework.

// Demonstrate the join() method defined by String.
public class StringJoinDemo
{
 public static void main(String args[]) 
 {
 String result = String.join(" ", "Alpha", "Beta", "Gamma");
 System.out.println(result);

 result = String.join(", ", "John", "ID#: 569", "E-mail: [email protected]");
 System.out.println(result);
 }
}

Output:

Alpha Beta Gamma
John, ID#: 569, E-mail: [email protected]

results matching ""

    No results matching ""