Chapter 4 Mathematical Functions
Chapter 4 Mathematical Functions
• Java provides many useful methods in the Math class for performing
common mathematical functions.
• The min and max methods return the minimum and maximum numbers of two
numbers (int, long, float, or double). For example, max(4.4, 5.0) returns 5.0, and
min(3, 2) returns 2.
• The abs method returns the absolute value of the number (int, long, float, or
double). For example
• Math.max(2, 3) returns 3
• 'a' < 'b' is true because the Unicode for 'a' (97) is less than the Unicode for 'b'
(98).
• 'a' < 'A' is false because the Unicode for 'a' (97) is greater than the Unicode for
'A' (65).
• '1' < '8' is true because the Unicode for '1' (49) is less than the Unicode for '8'
• For example, the following code tests whether a character ch is an
uppercase letter, a lowercase letter, or a digital character.
Method Description
charAt(index) Returns the character at the specified index from this string
concat(s1) Returns a new string that concatenates this string with string s1
trim() Returns a new string with whitespace characters trimmed on both sides
Getting String Length
• You can use the length() method to return the number of characters in a
string. For example, the following code
displays
• The augmented += operator can also be used for string concatenation. For
example, the following code appends the string "and Java is fun" with the
string "Welcome to Java" in message.
• The trim() method returns a new string by eliminating whitespace characters from
both ends of the string. The characters ' ', \t or \n are known as whitespace
characters. For example,
• String s = input.nextLine();
Reading a Character from the Console
• To read a character from the console, use the nextLine()
method to read a string and then invoke the charAt(0)
method on the string to return a character. For example,
the following code reads a character from the keyboard:
• Scanner input = new Scanner(System.in);
• System.out.print("Enter a character: ");
• String s = input.nextLine();
• char ch = s.charAt(0);
• System.out.println("The character entered is " + ch);
Comparing Strings
• The String class contains the methods as shown in Table 4.8 for comparing
two strings
Method Description
equalsIgnoreCase(s1) Returns true if this string is equal to string s1; it is case insensitive
compareTo(s1) Returns an integer greater than 0, equal to 0, or less than 0 to indicate whether
this string is greater than, equal to, or less than s1
startsWith(prefix) Returns true if this string starts with the specified prefix
endsWith(suffix) Returns true if this string ends with the specified suffix
if (string1 == string2)
else
• However, the == operator checks only whether string1 and string2 refer to the same
object; it does not tell you whether they have the same contents. Therefore, you cannot
use the == operator to find out whether two string variables have the same contents.
Instead, you should use the equals method. The following code, for instance, can be used
if (string1.equals(string2))
else
For example, the following statements display true and then false.
System.out.println(s1.equals(s2)); // true
• The compareTo method can also be used to compare two strings. For
example, consider
s1.compareTo(s2)
• The method returns the value 0 if s1 is equal to s2, a value less than 0 if
s1 is lexicographically (i.e., in terms of Unicode ordering) less than s2,
and a value greater than 0 if s1 is lexicographically greater than s2.
• The actual value returned from the compareTo method depends
on the offset of the first two distinct characters in s1 and s2 from
left to right. For example, suppose s1 is abc and s2 is abg, and
s1.compareTo(s2) returns -4. The first two characters (a vs. a) from
s1 and s2 are compared. Because they are equal, the second two
characters (b vs. b) are compared. Because they are also equal,
the third two characters (c vs. g) are compared. Since the
character c is 4 less than g, the comparison returns -4.
• public class StringCompareToDemo {
• public static void main(String[] args){
• String name1 = "Bob";
• String name2 = "Bob";
• String name3 = "David";
• if(name2.compareTo(name3)==0)
• System.out.println("The names are equal");
• else if(name2.compareTo(name3) >0)
• System.out.println(name2+" is greater than "+ name3);
• else
• System.out.println(name2+" is less than "+ name3);
• } }
• Note
• For example,
lastIndexOf(ch) Returns the index of the last occurrence of ch in the string. Returns -1 if not
matched.
lastIndexOf(ch, fromIndex) Returns the index of the last occurrence of ch before fromIndex in this string.
Returns -1 if not
matched.
lastIndexOf(s) Returns the index of the last occurrence of string s. Returns -1 if not matched.
lastIndexOf(s, fromIndex) Returns the index of the last occurrence of string s before fromIndex. Returns -1 if
not matched.
• For example,
• "Welcome to Java".indexOf('W') returns 0.
• "Welcome to Java".indexOf('o') returns 4.
• "Welcome to Java".indexOf('o', 5) returns 9.
• "Welcome to Java".indexOf("come") returns 3.
• "Welcome to Java".indexOf("Java", 5) returns 11.
• "Welcome to Java".indexOf("java", 5) returns -1.
• "Welcome to Java".lastIndexOf('W') returns 0.
• "Welcome to Java".lastIndexOf('o') returns 9.
• "Welcome to Java".lastIndexOf('o', 5) returns 4.
• "Welcome to Java".lastIndexOf("come") returns 3.
• "Welcome to Java".lastIndexOf(“java", 5) returns -1.
• "Welcome to Java".lastIndexOf("Java") returns 11
• Suppose a string s contains the first name and last name separated by a space. You can
use the following code to extract the first name and last name from the string
int k = s.indexOf(' ');
• String firstName = s.substring(0, k);
• String lastName = s.substring(k + 1);
• For example, if s is Kim Jones, the following diagram illustrates how the first name and
last name are extracted.
Conversion between Strings and Numbers
• You can convert a numeric string into a number. To convert a string into an
int value, use the Integer. parseInt method, as follows:
• You can convert a number into a string, simply use the string
concatenating operator as follows: