Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
}
Java String valueOf example.
This Java String valueOf example describes how various java primitives and
Object
are converted to Java String object using String valueOf method.
*/
public class JavaStringValueOfExample{
public static void main(String args[]){
/*
Java String class defines following methods to convert various Java
primitives to
Java String object.
1) static String valueOf(int i)
Converts argument int to String and returns new String object
representing
argument int.
2) static String valueOf(float f)
Converts argument float to String and returns new String object
representing
argument float.
3) static String valueOf(long l)
Converts argument long to String and returns new String object
representing
argument long.
4) static String valueOf(double i)
Converts argument double to String and returns new String object
representing
argument double.
5) static String valueOf(char c)
Converts argument char to String and returns new String object
representing
argument char.
6) static String valueOf(boolean b)
Converts argument boolean to String and returns new String object
representing
argument boolean.
7) static String valueOf(Object o)
Converts argument Object to String and returns new String object
representing
argument Object.
*/
int i=10;
float f = 10.0f;
long l = 10;
double d=10.0d;
char c='a';
boolean b = true;
Object o = new String("Hello World");
/* convert int to String */
System.out.println( String.valueOf(i) );
/* convert float to String */
System.out.println( String.valueOf(f) );
/* convert long to String */
System.out.println( String.valueOf(l) );
/* convert double to String */
System.out.println( String.valueOf(d) );
/* convert char to String */
System.out.println( String.valueOf(c) );
/* convert boolean to String */
System.out.println( String.valueOf(b) );
/* convert Object to String */
System.out.println( String.valueOf(o) );
}
}
/*
OUTPUT of the above given Java String valueOf Example would be :
10
10.0
10
10.0
a
true
true
Hello World
*/
1. /*
2. Check if string contains valid number example.
3. This example shows how to check if string contains valid number
4. or not using parseDouble and parseInteger methods of
5. Double and Integer wrapper classes.
6. */
7.
8. public class CheckValidNumberExample {
9.
10. public static void main(String[] args) {
11.
12. String[] str = new String[]{"10.20", "123456",
"12.invalid"};
13.
14. for(int i=0 ; i < str.length ; i ++)
15. {
16.
17. if( str[i].indexOf(".") > 0 )
18. {
19.
20. try
21. {
22. /*
23. * To check if the number is
valid decimal number, use
24. * double parseDouble(String
str) method of
25. * Double wrapper class.
26. *
27. * This method throws
NumberFormatException if the
28. * argument string is not a
valid decimal number.
29. */
30. Double.parseDouble(str[i]);
31. System.out.println(str[i] + " is
a valid decimal number");
32. }
33. catch(NumberFormatException nme)
34. {
35. System.out.println(str[i] + " is
not a valid decimal number");
36. }
37.
38. }
39. else
40. {
41. try
42. {
43. /*
44. * To check if the number is
valid integer number, use
45. * int parseInt(String str)
method of
46. * Integer wrapper class.
47. *
48. * This method throws
NumberFormatException if the
49. * argument string is not a
valid integer number.
50. */
51.
52. Integer.parseInt(str[i]);
53. System.out.println(str[i] + " is
valid integer number");
54. }
55. catch(NumberFormatException nme)
56. {
57. System.out.println(str[i] + " is
not a valid integer number");
58. }
59. }
60. }
61.
62. }
63. }
64.
65. /*
66. Output would be
67. 10.20 is a valid decimal number
68. 123456 is valid integer number
69. 12.invalid is not a valid decimal number
70. */
1. /*
2. Java String compare example.
3. This Java String compare example describes how Java String is compared
with another
4. Java String object or Java Object.
5. */
6.
7. public class JavaStringCompareExample{
8.
9. public static void main(String args[]){
10.
11. /*
12. Java String class defines following methods to compare Java String
object.
13. 1) int compareTo( String anotherString )
14. compare two string based upon the unicode value of each character in
the String.
15. Returns negative int if first string is less than another
16. Returns positive int if first string is grater than another
17. Returns 0 if both strings are same.
18. 2) int compareTo( Object obj )
19. Behaves exactly like compareTo ( String anotherString) if the
argument object
20. is of type String, otherwise throws ClassCastException.
21. 3) int compareToIgnoreCase( String anotherString )
22. Compares two strings ignoring the character case of the given
String.
23. */
24.
25. String str = "Hello World";
26. String anotherString = "hello world";
27. Object objStr = str;
28.
29. /* compare two strings, case sensitive */
30. System.out.println( str.compareTo(anotherString) );
31. /* compare two strings, ignores character case */
32. System.out.println( str.compareToIgnoreCase(anotherString) );
33. /* compare string with object */
34. System.out.println( str.compareTo(objStr) );
35.
36. }
37.
38. }
39.
40. /*
41. OUTPUT of the above given Java String compare Example would be :
42. -32
43. 0
44. 0
45. */