0% found this document useful (0 votes)
15 views14 pages

CompareTo EqualityObjects

Uploaded by

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

CompareTo EqualityObjects

Uploaded by

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

More

Strings
String Objects
String objects are immutable.

The String class does not contain


any modifier methods.

new String("uiltcea");
"statechamps"
"alligator"

© A+ Computer Science - www.apluscompsci.com


String References
A String reference variable can be
changed, but the String object the
variable refers to cannot be changed.
s
String s = "uil";
out.println(s);
s = "tcea"; "uil" "tcea"
out.println(s);

© A+ Computer Science - www.apluscompsci.com


String References
A String reference variable can be changed,
but the String object the variable refers
to cannot be changed.

String s = "compsci ";


out.println(s); OUTPUT
s.toUpperCase(); compsci
out.println(s); compsci
s=s.toUpperCase(); COMPSCI
out.println(s);
© A+ Computer Science - www.apluscompsci.com
String References
String one = new String("compsci");
String two = new String("compsci");

if(one==two)
System.out.println("=="); OUTPUT
else
System.out.println("!=="); !==

== compares the String references which are the


memory addresses of the actual String objects.

© A+ Computer Science - www.apluscompsci.com


Comparing Objects

Object references can be compared


with ==.

The actual object contents can be


compared using equals() or
compareTo()

© A+ Computer Science - www.apluscompsci.com


Comparing Objects
Integer one = 90;
Integer two = 75;
OUTPUT
out.println(one.compareTo(two)); -1
out.println(two.compareTo(one));
1
two = 90; true
out.println(two.equals(one)); 0
out.println(two.compareTo(one));

compareTo() returns a negative value when A is


less than B and a positive value when A is
greater than B.
0 is returned with the A and B are the same.
© A+ Computer Science - www.apluscompsci.com
Comparing Strings

String references can be compared


with ==.

The actual String contents can be


compared using equals() or
compareTo()

© A+ Computer Science - www.apluscompsci.com


String
frequently used methods

Name Use

equals(s) checks if this string has same chars as s


compareTo(s) compares this string and s for >,<, and ==
trim() removes leading and trailing whitespace
replaceAll(x,y) returns a new String with all x changed to y
toUpperCase() returns a new String with uppercase chars
toLowerCase() returns a new String with lowercase chars

© A+ Computer Science - www.apluscompsci.com


The equals( ) method
String one = new String("compsci");
String two = new String("compsci");

if(one.equals(two))
OUTPUT
System.out.println("equal");
else equal
System.out.println("!equal");

equals() compares the values stored in the


actual String objects.

© A+ Computer Science - www.apluscompsci.com


The compareTo() method
String one = "region"; OUTPUT
String two = "uilstate"; -3
3
out.println(one.compareTo(two));
out.println(two.compareTo(one));
0

two = "region";
out.println(two.compareTo(one));

compareTo() returns the difference in ASCII


value when comparing Strings.
© A+ Computer Science - www.apluscompsci.com
The trim() method
OUTPUT
String s = " 100 ";
String trimmed = s.trim(); 100
out.println(trimmed); 900

out.println(Integer.parseInt(trimmed)*9);

trim() returns a new String with all leading and


trailing white space removed.

© A+ Computer Science - www.apluscompsci.com


Case Changing Methods
String s = "compsci"; OUTPUT
out.println(s.toUpperCase()); COMPSCI
out.println(s); compsci
out.println(s.toLowerCase()); compsci

toUpperCase() and toLowerCase() return new


Strings with the changes requested.

© A+ Computer Science - www.apluscompsci.com


The replaceAll() method
String s = "abcdef1xyzabf1";
s = s.replaceAll("1", "#");
out.println( s );
OUTPUT
abcdef#xyzabf#

replaceAll() returns a new String with all number


1s changed to # signs.

© A+ Computer Science - www.apluscompsci.com

You might also like