Special String Oprations
These Operations include the automatic creation of new String instances from string literals, concatenation of multiple String objects by use of the "+" operator, and the conversion of other data types to a string representation.
String Literals: There is an easier way to do this using a String literal. For each String literal in your program, java automatically constructs a String object. Thus, you can use a string literal to initialize a String object. For Example:
A String object is created for every string literal, you can use a string literal any place you can use a String object.For Example, you can call methods directly on a quoted string as if it were an object reference, as the following statement shows. It calls the leangth() method on the string "abc". As expected, it prints "3".
String Concatenation: In java does not allow operators to applied to String objects. The one exception to this rule is the + operator, which concatenates two strings, producing a String object as the result. this allows you to chain together a series of + operations. for Example:
This display the string "he is 27 years old."
String Concatenation with Other Data Type:
In this case, age is an int rather then another String, but the output is display "he is 9 years old.". This is because the int value in age is automatically converted into its String representation within a String object. This String is then concatenated as before. the compiler will convert an operand to its string equivalent whenever the other operand of the + is an instance of String.
Be careful when you mix other types of operations with string concatenation expression, however. you might get surprising results. consider the following:
This fragment displays four: 22 rather then the four: 4.
That you probably expected. Here's way. Operator precedence causes the concatenation of "four" with the string equivalent of 2 to take please first. This result is then concatenated with the string equivalent of 2 a second time. to complete the integer addition first, you must use parentheses, like this:
Out put is four: 4.
String Conversion and toString(): When java converts data into its string representation during concatenation, it does so by calling one of the overloaded versions of the String conversion method valueOf() defined by String. valueOf() is overloaded for all the primitive types and for type object. For the Primitive types, valueOf() returns a string that contains the human-readable equivalent of the value with which it is called. for objects. valueOf() call the toString() method on the object. Here, let's examine the toString() method, because it is the means by which you can determine the string representation for objects of classes that you create.
Every class implements toString() because it is defined by Object. However, the default implementation of toString() is seldom sufficient. For Most important classes that you create, you will want to override toString() and provide your own string representations. fortunately, this is easy to do. the toString() method has this general form:
String tiString()
To implement toString(), simply return a String object that contains the human-readable string that appropriately describes an object of your class.
For Example:
Output :
Box b:Dimentions are 10.0 By 12.0 By 14.0
Dimentions are 10.0 By 12.0 By 14.0
As you can see, Box's toString() method os automatically invoked when a Box object is used in a concatenation expression or in a call to Println().
String Literals: There is an easier way to do this using a String literal. For each String literal in your program, java automatically constructs a String object. Thus, you can use a string literal to initialize a String object. For Example:
A String object is created for every string literal, you can use a string literal any place you can use a String object.For Example, you can call methods directly on a quoted string as if it were an object reference, as the following statement shows. It calls the leangth() method on the string "abc". As expected, it prints "3".
String Concatenation: In java does not allow operators to applied to String objects. The one exception to this rule is the + operator, which concatenates two strings, producing a String object as the result. this allows you to chain together a series of + operations. for Example:
This display the string "he is 27 years old."
String Concatenation with Other Data Type:
In this case, age is an int rather then another String, but the output is display "he is 9 years old.". This is because the int value in age is automatically converted into its String representation within a String object. This String is then concatenated as before. the compiler will convert an operand to its string equivalent whenever the other operand of the + is an instance of String.
Be careful when you mix other types of operations with string concatenation expression, however. you might get surprising results. consider the following:
This fragment displays four: 22 rather then the four: 4.
That you probably expected. Here's way. Operator precedence causes the concatenation of "four" with the string equivalent of 2 to take please first. This result is then concatenated with the string equivalent of 2 a second time. to complete the integer addition first, you must use parentheses, like this:
Out put is four: 4.
String Conversion and toString(): When java converts data into its string representation during concatenation, it does so by calling one of the overloaded versions of the String conversion method valueOf() defined by String. valueOf() is overloaded for all the primitive types and for type object. For the Primitive types, valueOf() returns a string that contains the human-readable equivalent of the value with which it is called. for objects. valueOf() call the toString() method on the object. Here, let's examine the toString() method, because it is the means by which you can determine the string representation for objects of classes that you create.
Every class implements toString() because it is defined by Object. However, the default implementation of toString() is seldom sufficient. For Most important classes that you create, you will want to override toString() and provide your own string representations. fortunately, this is easy to do. the toString() method has this general form:
String tiString()
To implement toString(), simply return a String object that contains the human-readable string that appropriately describes an object of your class.
For Example:
Output :
Box b:Dimentions are 10.0 By 12.0 By 14.0
Dimentions are 10.0 By 12.0 By 14.0
As you can see, Box's toString() method os automatically invoked when a Box object is used in a concatenation expression or in a call to Println().