string class in java

28
String Class in Java java.lang Class String java.lang.Object java.lang.String We do not have to import the String class since it comes from java.lang. An object of the String class represents a string of characters. “abcde”;

Upload: lot

Post on 12-Jan-2016

168 views

Category:

Documents


2 download

DESCRIPTION

String Class in Java. java.lang Class String java.lang.Object java.lang.String We do not have to import the String class since it comes from java.lang. An object of the String class represents a string of characters. “abcde”;. Two ways to create a String object. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: String Class in Java

String Class in Java

• java.lang Class String

• java.lang.Object java.lang.String

• We do not have to import the String class since it comes from java.lang.

• An object of the String class represents a string of characters. “abcde”;

Page 2: String Class in Java

Two ways to create a String object

• Create a String literal:

String str = "abc";

• With the key word new since it is an object

String str = new String(“abc”);

Page 3: String Class in Java

String is a Class: When an object is created it creates an Object Reference

• When an object is created a reference to that object is stored in memory.

public class Reference

{

public static void main(String[]args)

{

Reference r = new Reference();

System.out.println(r);

}

}

10-3

Reference@83e35b memory location for object

Page 4: String Class in Java

Storing String objects

• Java has 2 types of memory for Strings.

1. Objects stored in a Pool:

String s = “Sun”;

2. Objects are stored in the Heap :

created with the word new.

String s = new String(“Sun”);

10-4

Page 5: String Class in Java

10-5

String s1 = "Sun"; String s2 = s1; String s3 = “Sun”;

String s4 = new String(“Sun“); String s5 = new String (“Sun”);

s1

s2s4

s5"Sun"

String reference

String reference

There is only one reference created. All objects share the same reference to the object “Sun”.

String reference

“Sun"

s3

These are created as new objects and do not have the same reference. Every object created is unique and has its own reference.

When created this way, Java looks in the pool to see if there is already a String with the same name. If so points to it and has the same reference as other objects with that content.

POOL

When created WITH THE WORD NEW objects go on the heap. Each object is brand new and has its own reference. It not shared.

HEAP

Page 6: String Class in Java

10-6

String s1 = "Hello"; // String literal

String s2 = "Hello"; // String literal

String s3 = s1; // same reference

String s4 = new String("Hello"); // String object

String s5 = new String("Hello"); // String object

Page 7: String Class in Java

Why does this matter?

String comparison:

There are three ways to compare String objects:

1.By equals() method (inherited from Object)

2.By == operator

3.By compareTo() method

10-7

Page 8: String Class in Java

By equals() method:

• Equals() method compares the original content of the String. Does it have the same content.

public boolean equals(Object another)

public boolean equalsIgnoreCase(String another)

10-8

Page 9: String Class in Java

Equality using equals(Object o)

• String n = "Computer";

• String s = "Computer";

• String t = new String("Computer");

• String u = new String("Computer");

• String v = new String (“computer”);

boolean b = n.equals(s); true

boolean b = n.equals(t); true

boolean b = n.equals(u); true

boolean b = n.equalsIgnoreCase(v); true 10-9

Page 10: String Class in Java

By == operator• String n = "Computer";

• String s = "Computer";

• String t = new String("Computer");

• String u = new String("Computer");

n

s

“Computer"

String reference

POOL

t

u

String reference

String reference

“Computer"

HEAP

Page 11: String Class in Java

== equality

• String n = "Computer";

• String s = "Computer";

• String t = new String("Computer");

• String u = new String("Computer");

n == s true

n == t false

t == u false

10-11

Page 12: String Class in Java

10-12

If you create a String using new it creates a new String and will not reference the same object.

String s1 = new String(“Sun”);

String s2 = new String(“Sun”);

String Reference

String Reference Sun

System.out.println(s1 == s2) ; // false two references created

System.out.println(s1.equals(s2)); // true says same thing

Sun

Page 13: String Class in Java

compareTo method parameters

10-13

int compareTo(Object o) // Object o is the object to be compared

or

int compareTo(String anotherString) // String to be compared

What is returned 1.if the two strings are equal to each other returns 0 . 2.if argument is > than String return value < 0 (negative number) 3.if the argument is < than the string return > 0 (positive number) .

Page 14: String Class in Java

Lexicographic order:

• Lexicographic order is a generalization of alphabetical order.

• In this ordering, numbers come before letters and capital letters come before lower case letters. Lexicographic comparison is like alphabetizing the Strings.

• numbers

• uppercase

• lower case 10-14

http://ss64.com/ascii.html

Page 15: String Class in Java

Lexicographical • Upper case characters are regarded as less than lower case

characters.

• "APPLE".compareTo("apple") returns a negative integer. -32 Argument is greater than String A ascii code of 65 < a ascii code of 97

• “apple”.compareTo(“APPLE”) returns positive 32 argument less than String a ascii code of 97 > A ascii code of 65

• “apple”.compareTo(“apple”) returns 0 equal to each other

1. if the two strings are equal to each other returns 0 .

2. if argument is > than String return value < 0 (negative number)

3. if the argument is < than the string return > 0 (positive number

10-15

Page 16: String Class in Java

String str1 = "Abc";

String str2 = "abc";

String str3 = "year";

String str4 = "table";

String str5 = "abc";

System.out.println(str1.compareTo(str2));

System.out.println(str2.compareTo(str1));

System.out.println(str3.compareTo(str4));

System.out.println(str5.compareTo(str2));

“ABC to “abc argument is > so negative -32

“abc to “Abc” argument is < so positive 32

“year” to “”table” argument is < so positive 5

“abc” to “abc” equal returns 0 10-16

Return Value :1.if the two strings are equal to each other returns 0 . 2.if argument is greater than String return value < 0 (negative number) 3.if the argument is less than the string return > 0 (positive number)

-32 32 5 0

Page 17: String Class in Java

String Class in Java

• java.lang Class String

• java.lang.Object java.lang.String

• We do not have to import the String class since it comes from java.lang.

• An object of the String class represents a string of characters. “abcde”;

Page 18: String Class in Java

10-18

Empty Strings• An empty string has no characters;

• Its contents are “null”.

String s1 = “"; String s2 = new String();

Empty strings

Page 19: String Class in Java

String indexes

• String is a string of characters.

Each letter in the string has its own index location and can be accessed by it.

index location

10-19

0 1 2 3 4 5 6 7

c o m p u t e r

The length of the String is how many letters it contains: 8The ending index of the String is length – 1; 7

Page 20: String Class in Java

10-20

Index locations • Strings have index locations from

• 0 to length-1

• strawberry // 10 letters in the word

• // index from 0 to 9

• length() = 10

• length()-1 = 9

• Index starts at 0 and goes to 9

0123456789

Page 21: String Class in Java

10-21

String Methods: • Page 78

• There are many ways to manipulate Strings.

• Look on page 78 those tested on AP

• You always use the period to separate the object from the method.

• s1.length();

Page 22: String Class in Java

10-22

Methods — length()

int length (); returns an int

• Returns the number of characters in the string

6

4

int lenF = f.length();

int lenW = w.length();

Returns:

String f = “Flower”;

String w = “Wind”;

Page 23: String Class in Java

10-23

Methods — substring

String s2 = s.substring (i, k);

returns the substring of chars in positions from i to k-1

String s3 = s.substring (i);

returns the substring from i char to the end

String s = “strawberry”;

rawrawberry

String s2 = s.substring (2,5); start at 2 end at 4 String s3 = s.substring (2); start at 2 thru end

Returns:

strawberry

i k

strawberry

i

Strings have index locations from 0 to length-1

Page 24: String Class in Java

String n = "Computer";

String one = n.substring(0,7);

String two = n.substring(1,6);

String three = n.substring(2,5);

String four = n.substring(4);

String five = n.substring(3);

String six = n.substring(1,n.length()-2);

String seven = six.substring(0, n.length()/2);

10-24

0 1 2 3 4 5 6 7

c o m p u t e r

s.substring(i, k);returns the substring of chars in positions from i to k-1

s.substring(i);returns the substring from i char to the end

Page 25: String Class in Java

Given a string, return a new string made of 3 copies of the last 2 chars of the original string. The string length will be at least 2.

extraEnd("Hello") → "lololo"extraEnd("ab") → "ababab"extraEnd("Hi") → "HiHiHi“

public String extraEnd(String str) {

String end = str.substring(str.length()-2);

return end+end+end;

}

10-25

Page 26: String Class in Java

• Given a string of even length, return the first half. So the string "WooHoo" yields "Woo".

firstHalf("WooHoo") → "Woo"firstHalf("HelloThere") → "Hello"firstHalf("abcdef") → "abc“

public String firstHalf(String str) {

String firstHalf = str.substring(0,str.length()/2);

return firstHalf;

}

10-26

Page 27: String Class in Java

10-27

Methods — Concatenation

String s1 = “obi”;String s2 = “wan”;

String result = s1 + s2;obiwan

String result = s1.concat (s2);the same as s1 + s2 obiwan

Page 28: String Class in Java

10-28

Methods — Find (indexOf)

String date ="July 5, 2012 1:28:19 PM";

date.indexOf ('J'); 0

date.indexOf ('2'); 8

date.indexOf ("2012"); 8

date.indexOf ('2', 9); 11

date.indexOf ("2020"); -1

date.lastIndexOf ('2'); 15

Returns:

(not found)

(starts searching at position 9)

0 8 11 15

Index of return the index location of the first occurrence of the character requested.