/**
This program demonstrates the Java String class.
It shows the character index methods and also the conversion from integers to strings.
*/
class MoreStringsJava2
{
public static void main(String[] args)
{
int iFirstIndex;
int iNextIndex;
int iLastIndex;
//Index operations
System.out.println("Note the \\ character is only one char length long");
String str1 ="The \\index\\ methods provide the positions of characters.";
System.out.println(str1);
//Note how the return value from a method, in this case the length() method,
//can be used directly without assignment to an integer variable.
System.out.println("The number of characters in the string str1 is :"+str1.length());
System.out.println("The first character is :"+str1.charAt(0));
System.out.println("The last character is :"+str1.charAt(str1.length()-1));
iFirstIndex=str1.indexOf(' ');
System.out.println("The first space is at index :"+iFirstIndex);
iNextIndex=str1.indexOf(' ',iFirstIndex);
System.out.println("The next space is at index :"+iFirstIndex+1);
iLastIndex=str1.lastIndexOf('\\');
System.out.println("The last \\ character is at :"+iLastIndex);
System.out.println("The next last \\ character is at :"+str1.lastIndexOf('\\',iLastIndex-1));
System.out.println("Seaching beyond the range of the string returns -1:"+str1.indexOf('\\',199));
System.out.println("As does search for non-existent characters :"+str1.indexOf('?'));
}
}
$ java MoreStringsJava2 Note the \ character is only one char length long The \index\ methods provide the positions of characters. The number of characters in the string str1 is :56 The first character is :T The last character is :. The first space is at index :3 The next space is at index :31 The last \ character is at :10 The next last \ character is at :4 The next last \ character is at :-1 $ javac MoreStringsJava2.java $ java MoreStringsJava2 Note the \ character is only one char length long The \index\ methods provide the positions of characters. The number of characters in the string str1 is :56 The first character is :T The last character is :. The first space is at index :3 The next space is at index :31 The last \ character is at :10 The next last \ character is at :4 Seaching beyond the range of the string returns -1:-1 As does search for non-existent characters :-1 $
/**
This program demonstrates the Java String class.
It shows the difference between the equals method and the == operator.
*/
class StringEqulityJava
{
public static void main(String[] args)
{
//First we declare 4 strings.
String str1 ="I am a string";
String str2 ="I am a string";
String str3 ="I am a string as well";
String str4 = new String ("I am a string");
//We show them.
System.out.println("str1 is:"+str1);
System.out.println("str2 is:"+str2);
System.out.println("str3 is:"+str3);
System.out.println("str4 is:"+str4);
//The equals method works by comparing each characters in the strings.
System.out.println("Is str1 equal to str1?:"+str1.equals(str1));
System.out.println("Is str1 equal to str2?:"+str1.equals(str2));
System.out.println("Is str2 equal to str1?:"+str2.equals(str1));
System.out.println("Is str1 equal to str3?:"+str1.equals(str3));
System.out.println("Is str1 equal to \'I am a string\'?:"+str1.equals("I am a string"));
System.out.println("Is str1 equal to \'I am a string\'?:"+str1.equals("I am a"+" string"));
System.out.println("Is str1 equal to \'Iamastring\'?:"+str1.equals("Iamastring"));
System.out.println("Is str1 equal to str4?:"+str1.equals(str4));
//The object == operator tests whether the two objects are the same.
System.out.println("Is str1 the same object as str1:?"+(str1==str1));
System.out.println("Is str1 the same object as str2:?"+(str1==str2));
System.out.println("Is str1 the same object as str3:?"+(str1==str3));
System.out.println("Is str1 the same object as \'I am a string\':?"+(str1=="I am a string"));
System.out.println("Is str1 the same object as \'I am a string\':?"+(str1=="I am a"+" string"));
System.out.println("Is str1 the same object as str4:?"+(str1==str4));
}
}
$ javac StringEqulityJava.java $ java StringEqulityJava str1 is:I am a string str2 is:I am a string str3 is:I am a string as well str4 is:I am a string Is str1 equal to str1?:true Is str1 equal to str2?:true Is str2 equal to str1?:true Is str1 equal to str3?:false Is str1 equal to 'I am a string'?:true Is str1 equal to 'I am a string'?:true Is str1 equal to 'Iamastring'?:false Is str1 equal to str4?:true Is str1 the same object as str1:?true Is str1 the same object as str2:?true Is str1 the same object as str3:?false Is str1 the same object as 'I am a string':?true Is str1 the same object as 'I am a string':?true Is str1 the same object as str4:?false $
System.out.println("Is str1 the same object as \'I am a string\':?"+(str1=="I am a string"));and even smarter
System.out.println("Is str1 the same object as \'I am a string\':?"+(str1=="I am a"+" string"));The compiler does this by evaluating every expressions that it has enough data to do so.