03 Math Characters Strings
03 Math Characters Strings
■ Today, we will briefly cover some of these methods toRadians(degrees) Converts an angle in degrees into radians
toDegrees(radians) Converts an angle in radians into degrees
■ Each method name is preceded by Math. asin(a) Returns inverse sine in radians
■ Reference: Liang, Chapter 4 (and sections 2.9.4 and 3.7) acos(a) Returns inverse cosine in radians
atan(a) Returns inverse tangent in radians
Exponent Methods Service Methods
Method Description Method Description
ceil(x) Rounds x up to its nearest integer
exp(x) Returns e to the power of x (ex)
floor(x) Rounds x down to its nearest integer
log(x) Returns the natural logarithm of x rint(x) Rounds x to its nearest integer.*
log10(x) Returns the base 10 logarithm of x round(x) Rounds x to the nearest integer
min(a, b) Returns the smaller of two values
pow(a, b) Returns a raised to the power of b (ab)
max(a, b) Returns the larger of two values
sqrt(x) Returns square root of x for x >= 0 abs(n) Returns the absolute value of n
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights
reserved. 11 reserved. 12
ASCII Code for Commonly Used Characters Escape Sequences for Special Characters
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights
reserved. 13 reserved. 14
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights
reserved. 15 reserved. 16
Casting between char and Comparing and Testing
Numeric Types Characters
int i = 'a'; // Same as int i = (int)'a'; if (ch >= 'A' && ch <= 'Z')
System.out.println(ch + " is an uppercase letter");
else if (ch >= 'a' && ch <= 'z')
char c = 97; // Same as char c = (char)97; System.out.println(ch + " is a lowercase letter");
else if (ch >= '0' && ch <= '9')
System.out.println(ch + " is a numeric character");
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights
reserved. 17 reserved. 18
Method Description
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights
reserved. 19
A string is just a sequence of characters
Strings can have 0 or more characters
String Methods
(an empty string is still a sequence)
The String class is probably the most String() — creates a new String
frequently used class in Java programming String s = new String(“Hello!”);
It’s automatically provided to all Java Shorthand: String s = “Hello!”;
programs (it lives in the java.lang
package) This shorthand only works for String!
Let’s take a quick look at the methods length() — returns the total number of
provided by the String class... characters in the String
21 22
Extracting Data
String Adjustments From Strings
trim() — returns a new String
with no leading or trailing whitespace The positions in a String are
numbered from 0 to (length - 1)
toLowerCase()/toUpperCase()
— return a new copy of the String in charAt() — returns the character at
lower/uppercase a given position (index)
All three methods leave the original indexOf(str) — returns the first
String unchanged index at which str occurs in the string
(or -1 if it isn’t there)
Java strings are immutable
23 24
Extracting Data Finding a Character or a Substring in a
String
From Strings, cont’d
Method Description
substring(start, end) — returns a indexOf(ch) Returns the index of the first occurrence of ch in the string. Returns -1 if not
matched.
new String containing the characters indexOf(ch, fromIndex) Returns the index of the first occurrence of ch after fromIndex in the string.
from position start up to (but not Returns -1 if not matched.
including) end indexOf(s) Returns the index of the first occurrence of string s in this string. Returns -1 if
not matched.
indexOf(s, fromIndex) Returns the index of the first occurrence of string s in this string after
You can also call substring() with fromIndex. Returns -1 if not matched.
lastIndexOf(ch) Returns the index of the last occurrence of ch in the string. Returns -1 if not
exactly one argument matched.
lastIndexOf(ch, Returns the index of the last occurrence of ch before fromIndex in this
In this case, it returns everything from fromIndex) string. Returns -1 if not matched.
lastIndexOf(s) Returns the index of the last occurrence of string s. Returns -1 if not matched.
the specified index through the end lastIndexOf(s, Returns the index of the last occurrence of string s before fromIndex.
fromIndex) Returns -1 if not matched.
25 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights
reserved. 26
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights
reserved. 27
28
Objects & Primitives Java is Shallow
Java performs shallow comparisons by
default (using the == operator)
Remember the difference between primitive
(built-in) types and objects A shallow comparison looks at the value
immediately associated with a variable
Primitive variables hold an actual value
This is okay for primitive types
Object variables (references) only hold the
address of an object! For objects, this means that we compare
their memory addresses, not their contents!
This causes problems when we try to
compare two objects Objects are only “equal” if they live at the
same memory address
29 30
31 32
Don’t Be So Two More
Sensitive! Comparisons
Problem: equals() and
compareTo() are case-sensitive
Sometimes, we only want to compare Use these methods to test the
two Strings by length and/or beginning or end of a String value:
characters startsWith(x)
Java has two more methods for this endsWith(x)
situation:
equalsIgnoreCase()
33 34
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights
reserved. 35 reserved. 36
Frequently-Used Specifiers
int count = 5;
items
double amount = 45.56;
System.out.printf("count is %d and amount is %f", count, amount);