String Operations: Figure 3.21 Shows The Most Frequently Used Methods at This Level of Programming
String Operations: Figure 3.21 Shows The Most Frequently Used Methods at This Level of Programming
In addition to the constructors, the class has forty-nine methods, many of which are overloaded. That is,
the class String provides methods for:
Comparing strings
Searching strings
Extracting sub-strings
Creating a copy of a string, and
Concatenate strings
Figure 3.21 shows the most frequently used methods at this level of programming.
int
compareToIgnoreCase Compares two strings lexicographically, str.compareToIgnoreCase
(String) ignoring case considerations. (“JavA”)
boolean
equalsIgnoewCase Compares this String to another String,
(String) ignoring case considerations. “JaVa”.equals(“java”)
String substring
( int beginIndex, Returns a new string that is a substring of Substring (5, 8)
int endIndex ) this string.
Figure 3.21 The most frequently used methods in the class String
The Lexicographical order of two words is defined as follows:
Listing 3.9 shows two ways of creating a string object. See Lines 5 and 6. In addition Lines 8 –
11 show various methods being called.
1. class test_strings
2. {
3. public static void main(String [] arg)
4. {
5. String str1 = "Hello! I am learning Java";
6. String str2 = new String ("Hi, I am about to learn strings
in Java");
7.
8. System.out.println("str1 has " + str1.length() + "
characters");
9. System.out.println("The first six characters of str1 is: "
+ str1.substring(0,6));
10. System.out.println("The 16th character is str1 is: " +
str1.charAt(15));
11. System.out.println(
str1.substring(0,6).concat(str2.substring(3)));
12. }
13. }
The class Math contains methods for performing basic numeric operations such as the elementary
exponential, logarithm, square root, and trigonometric functions. This class is regarded as a service class.
It does not have any public constructor, it has only one private constructor, and hence you cannot create
a Math object. All of the methods in the class are class methods. The class comes with two class
constants. They are shown in Figure 3.22.
The double value that is closer than any other to e, the base of the
static double E natural logarithms.
The double value that is closer than any other to pi, the ratio of the
static double PI circumference of a circle to its diameter.
Figure 3.22 Shows the two class constants in the class java.lang.Math.
Listing 3.10 shows how these methods are called, and Figure 3.23 shows the value for each constants.
1. class E_PI
2. {
3. public static void main(String[] arg)
4. {
5. System.out.println("The value of pi is: " + Math.PI);
6. System.out.println("The value of E is: " + Math.E);
7. }
8. }
Listing 3.10 The class methods are called using the class name.
Figure 3.23 Shows the output for both values pi and E respectively.
As was mentioned earlier, the class Math contains only class methods. To use them you simply use the
pattern:
Math.method(parameter);
Where Math is the name of the class, method represents any of the class methods, and parameter
represents the argument list of each method. Figure 3.24 shows the more frequently used methods.
Static methods from class Math Their return type and parameter listing