valueOf() Stirng Example

valueOf() Stirng Example


Many methods exist in java.lang.String class used for different string manipulations like checking the availability of a character, to extract a part of a string or to convert a data type to string etc. One method boon to Developers is valueOf() overloaded 9 times taking different parameters.

String.valueof() converts all primitive data types and objects into string form and returns. Original primitive data type is not changed. The returned value is equivalent string representation to data type. See the notes after screenshot.

valueOf() method is overloaded 9 times in String class. All are static methods. All return type is string. Following are method signatures.

  1. public static String valueOf(boolean b): Returns a string representing boolean value b. Otherway to say, converts b into string and returns. If the parameter passed is boolean value true, true is returned as string and if false is passed, returns false as string.
  2. public static String valueOf(char ch): Converts char ch into string and returns. As the argument takes a single character, the returned string is of length of 1.
  3. public static String valueOf(int x): Converts int x into string and returns. To this method, byte and short values also can be passed. They are casted to int and evaluated.
  4. public static String valueOf(long x): Converts long x into string and returns.
  5. public static String valueOf(float x): Converts float x into string and returns.
  6. public static String valueOf(double x): Converts double x into string and returns.
  7. public static String valueOf(Object obj): Converts any Java object obj into string and returns.
  8. public static String valueOf(char ch[]): Converts the char array ch into string and returns. After returning, if the array elements are modified, the changes will not be reflected in the string.
  9. public static String valueOf(char ch[], int offset, int count): In the previous method, whole character array is converted into string. But here is an option. We can obtain a string by converting a few elements of the array (need not be full array). offset gives from what element and count gives number of elements. See the notes.
Following example illustrates the usage of valueOf() all the 9 overloaded methods.


valueOf()
Output screen of different ways of using valueOf() method

long y = 25L;
float k = 5.6F;

In our primitive data types discussion, we know a long value is to be suffixed with L (or lowercase l) but is not mandatory. But it is mandatory to suffix with F (or lowercase f) a float value.

char ch1[] = { 'A', 'B', 'C', 'D', 'E', 'F','G' };
System.out.println(“Few elements of char array ch1 in string form: ” + String.valueOf(ch1, 2, 4));

In the above overloaded char array argument method, 2 indicates starting index number in the array from which string representation is required. 2 indicates character C and 4 indicates number of characters starting from C; it is obviously CDEF.

Like this many methods of String class decreases your code of string maniupulation. Same in C/C++ languages require to write your own laborious code with many for loops and flags. String (with other classes like Math, Character, GregorianCalendar, Observer and Obervable etc.) makes you to feel happy that you are lucky in Java language coding.

One thought on “valueOf() Stirng Example

  1. Lochan kumar khuntia

    This is tutorial really very help full to me, it is really very deep concept of String class.

    Thanking you sir, for sharing your this tutorial with us.

Leave a Reply

Your email address will not be published. Required fields are marked *