





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






























It has a list of all Java packages, classes, and interfaces; along withall of their methods, fields and constructors. ^
java.lang
intrinsic classes (
String
, etc)
^
java.io
reading and writing
^
java.util
Java Collection Framework and utility classes
^
javax.swing
GUI
It tells you how to use Java classes but little how they areimplemented
String Properties^
It has fields, constructors, and methods.
Construct a string^ String
s
"Hello
world";
Get the length of the string^ int
length
s.length();//length
Access a part of the string^ String
sub
s.substring(6);//"world"
char
c
s.charAt(1);
//'e'
Concatenate strings^ String
t
of
java";
String
s
s
t;
//"Hello
world
of
java"
String Properties^
char[]
charArray
{'a',
'b',
'c'};
char[]
charArray
charArray1;
charArray1[1]
'i';
//charArray
{'a',
‘i',
'c'}
//charArray
{'a',
‘i',
'c'}
String Properties^ Q: What are the values of s1 and s2 at the end of the code
String
s
"abc";
String
s
s
s
s
"def";
s
"abcdef"
//s
"abc"
String Main Usage^
s
"hello
world";
System.out.println(s.startsWith("hell"));//trueSystem.out.println(s.endsWith("world"));//true
sUpper
s.toUpperCase();//"HELLO
String Main Usage^
String
s
"Hello
world";
s.indexOf("o");//4, return -1 if the argument string not found
s.lastIndexOf("o");//
String Advanced Usage – breaking words apart
String
s^
"Hello
world
of
java";
StringTokenizer
st
new
StringTokenizer(s);
while
(st.hasMoreTokens())
System.out.println(st.nextToken()); }
//Hello//world//of//java
String Advanced Usage – breaking words apart
String
s^
"Hello,
world
of
java";
StringTokenizer
st
new
StringTokenizer(s);
while
(st.hasMoreTokens())
System.out.println(st.nextToken()); }
//Hello,//world//of//java
String Advanced Usage – Concatenate strings repetitively^
stringList
new
ArrayList();
for
(int
i
i^
i++)
stringList.add("string"
i);
} String
finalString
for
(Iterator
iter
stringList.iterator();
iter.hasNext();
String
s
(String)
iter.next();
finalString
s;
//work
but
inefficient
for
large
strings
and
large
amount
of
concatenation
because
String
is
immutable.
Intuitively,
Java
needs
to
maintain
an
a^
old
string,
a
new
string,
and
a
concatenate
string
in
each
iteration.
String Advanced Usage – Concatenate strings repetitively^
stringList
new
ArrayList();
for
(int
i
i^
i++)
stringList.add("string"
i);
} StringBuffer
sb
new
StringBuffer();
//StringBuffer
is
mutable.
Recommended
for
a
large
amount
of
string
concatenation.
for
(Iterator
iter
stringList.iterator();
iter.hasNext();
String
s
(String)
iter.next();
sb.append(s); }
String Advanced Usage – String and OOP
public
class
Point
double
x;
double
y;
Point(double
x,
double
y)
this.x
x;
this.y
y;
public
String
toString()
return
"x
x^
y
y;
} Point
p^
new
Point(2,3);
System.out.println(p);
x = 2.0, y = 3.
String Advanced Usage – Convert between strings toprimitives
int
i^
String
s^
i
String
t^
String.valueOf(i);
String
u^
Integer.toString(i);
//s
t^
u
int
j^
Integer.parseInt(s);
j
//Extension
Double.parseDouble();