0% found this document useful (0 votes)
56 views5 pages

String Operations: Figure 3.21 Shows The Most Frequently Used Methods at This Level of Programming

The String class has 49 methods for examining, comparing, searching, extracting, copying, and concatenating strings. The Math class contains methods for basic numeric operations like exponents, logs, square roots, and trig functions. It has no public constructor and all methods are static, so they are called using ClassName.method() like Math.PI to get the constant Pi value.

Uploaded by

Joel Perez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views5 pages

String Operations: Figure 3.21 Shows The Most Frequently Used Methods at This Level of Programming

The String class has 49 methods for examining, comparing, searching, extracting, copying, and concatenating strings. The Math class contains methods for basic numeric operations like exponents, logs, square roots, and trig functions. It has no public constructor and all methods are static, so they are called using ClassName.method() like Math.PI to get the constant Pi value.

Uploaded by

Joel Perez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

String Operations

In addition to the constructors, the class has forty-nine methods, many of which are overloaded. That is,
the class String provides methods for:

 Examining individual characters of the sequence

 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.

Methods Purpose Example

char charAt(int) Returns the character at the specified index. str.charAt(14)

int compareTo(String) Compares two strings lexicographically. str.compareTo(“java”)

int
compareToIgnoreCase Compares two strings lexicographically, str.compareToIgnoreCase
(String) ignoring case considerations. (“JavA”)

String str.concat(String) Concatenates the specified string to the end


of this string. str.concat(“Yes”);

boolean Tests if this string ends with the


endsWith(String) Specified suffix str.endsWith(“Java”);

boolean equals(Object) Compares this string to the specified object. “Java.equals(”Java”);

boolean
equalsIgnoewCase Compares this String to another String,
(String) ignoring case considerations. “JaVa”.equals(“java”)

Returns the length of this string; ie. The


int length() number of characters in the string. str.length()

String substring Returns a new string that is a substring of


(int beginIndex) this string. str.substring(5)

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:

 The value 0 if the argument is a string lexicographically equal to this string.


 A value less than 0 if the argument is a string lexicographically greater than this string
 A value greater than 0 if the argument is a string lexicographically less than the string.

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. }

Listing 3.9 Creating strings and operations on strings.

The class Math.java

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.

The following output shows the value of pi and E respectively.

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

double abs(double a) Returns the absolute value of a double value.

float abs (float a) Returns the absolute value of a float value.

int abs(int a) Returns the absolute value of an int value.

long abs(long a) Returns the absolute value of a long value.

Returns the arc cosine of an angle, in the range of 0.0


double acos(double a) through pi.

Returns the arc sine of an angle, in the range of -pi/2


double asin(double a) through pi/2.

Returns the arc tangent of an angle, in the range of -pi/2


double atan(double a) through pi/2.

double atan2(double a, double b) Converts rectangular coordinates (b, a) to polar (r, theta).

Returns the smallest (closest to negative infinity) double


value that is not less than the argument and is equal to a
double ceil(double a) mathematical integer.

double cos(double a) Returns the trigonometric cosine of an angle.

Returns the exponential number e (i.e., 2.718...) raised to


double exp(double a) the power of a double value.

Returns the largest (closest to positive infinity) double


value that is not greater than the argument and is equal
double floor(double a) to a mathematical integer.
double IEEEremainder(double f1, Computes the remainder operation on two arguments as
double f2) prescribed by the IEEE 754 standard.

double log(double a) Returns the natural logarithm (base e) of a double value.

double max(double a, double b) Returns the greater of two double values.

float max(float a, float b) Returns the greater of two float values.

int max(int a, int b) Returns the greater of two int values.

long max(long a, long b) Returns the greater of two long values.

double min(double a, double b) Returns the smaller of two double values.

float min(float a, float b) Returns the smaller of two float values.

int min(int a, int b) Returns the smaller of two int values.

long min(long a, long b) Returns the smaller of two long values.

Returns of value of the first argument raised to the power


double pow(double a, double b) of the second argument.
Returns a double value with a positive sign, greater than
double random() or equal to 0.0 and less than 1.0.

Returns the double value that is closest in value to a and


double rint(double a) is equal to a mathematical integer.

You might also like