Special String Operations
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
- For each String literal in a program java creates a String object which is enclosed with a double quotes.
// Construct one String from another.
public class StringOperations
{
public static void main(String args[])
{
String s1= "Hello";
int length = "welcome".length();
System.out.println("String literals examples");
System.out.println("String s1 is::"+s1);
System.out.println("length of string literal //welcome// is::"+length);
}
}
Output:
String literals examples
String s1 is::Hello
length of string literal //welcome// is::7
String Concatenation
We can use only one operator on Strings which is + to concatenate two or more strings .
It is useful when we want to concatenate a long string by separating into pieces.
// Using concatenation to prevent long lines.
public class ConCat
{
public static void main(String args[]) {
String longStr = "This could have been " +
"a very long line that would have " +
"wrapped around. But string concatenation " +
"prevents this.";
System.out.println(longStr);
}
}
Output:
This could have been a very long line that would have wrapped around. But string concatenation prevents this.
String Concatenation with Other Data Types
- We can concatenate a String with other data types using + operator same as Strings
// Using concatenation with other data types.
public class ConCat1
{
public static void main(String args[])
{
int date=31;
String str="Today date is::"+date; //herer date is an integer value
System.out.println("concatanating int variable::"+str);
String str1="Five "+5; //direct concatenation of int value
System.out.println("direct concatenation of int value::"+str1);
String str2= "Five "+2+3; //when adding two integer values
System.out.println("when adding two integer values::"+str2);
String str3 = "Five "+(2+3); //to perform addition
System.out.println("To perform addition::"+str3);
}
}
Output:
concatanating int variable::Today date is::31
direct concatenation of int value::Five 5
when adding two integer values::Five 23
To perform addition::Five 5
String Conversion and toString( )
The toString( ) method has this general form is:String toString( )
It simply return a String object that contains the human-readable string that appropriately describes an object of a class.
We can override this method to execute that when a String is created.
// Override toString() for Box class.
class Box
{
double width;
double height;
double depth;
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
public String toString()
{
return "Dimensions are " + width + " by " + depth + " by " + height + ".";
}
}
public class toStringDemo
{
public static void main(String args[])
{
Box b = new Box(10, 12, 14);
String s = "Box b: " + b; // concatenate Box object
System.out.println(b); // convert Box to string
System.out.println(s);
}
}
Output:
Dimensions are 10.0 by 14.0 by 12.0.
Box b: Dimensions are 10.0 by 14.0 by 12.0.