0% found this document useful (0 votes)
11 views33 pages

10 Java Pre Defined Classes

The document provides an overview of Java's pre-defined classes, focusing on the java.lang package, which includes essential classes like String, StringBuffer, and Math. It details the characteristics and methods of these classes, as well as wrapper classes for primitive types. Additionally, it covers the Vector and Thread classes, explaining their functionalities and providing examples of their usage.

Uploaded by

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

10 Java Pre Defined Classes

The document provides an overview of Java's pre-defined classes, focusing on the java.lang package, which includes essential classes like String, StringBuffer, and Math. It details the characteristics and methods of these classes, as well as wrapper classes for primitive types. Additionally, it covers the Vector and Thread classes, explaining their functionalities and providing examples of their usage.

Uploaded by

mrroopak25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA PRE‐DEFINED CLASSES

Nurochman
Java.lang package
• Java compiler automatically imports all the
classes in the java.lang package into every
source file.
• some off th
the mostt iimportant
t t classes
l off th
the
java.lang package:
– Object
– Math
– The
Th wrapper classes
l
– String
– StringBuffer
Java Pre‐Defined Classes
• String
• StringBuffer
• Math
• Vector
• Hashtable
• Thread
• Socket, ServerSocket
Wrapper Classes
• Boolean
• Character
• Byte, Short, Integer, Long
• Float, Double
The String Class
• Class String berisi string yang tetap (immutable string).
• Sekali intance String dibuat maka isinya tidak bisa diubah.
• Memiliki beberapa konstruktor.
• Common string constructors:
String s1 = new String(“immutable”);
String s1 = “immutable”;
• Java mempunyai mediad penyimpanan literal
l l string yang yang
disebut “pool”.
• Jika
Jik suatu
t literal
lit l string
t i sudah
d h ada
d di pool,l JJava “tidak
“tid k akan
k
membuat copy lagi”.
Membandingkan String
• Method equals() membandingkan contentnya
• == membandingkan alamatnya
alamatnya.
Class String Methods
• char charAt(int index)
• String concat(String str)
• static String copyValueOf(char[] data)
• boolean endsWith(String suffix)
• boolean equals(Object anObject)
• boolean equalsIgnoreCase(String
anotherString)
Class String Methods
• byte[] getBytes()
• int indexOf(int ch)
ch), int indexOf(int ch,
ch int
fromIndex)
• int lastIndexOf(int ch), int lastIndexOf(int ch,
int fromIndex))
• int length()
• String replace(char
l ( h oldChar,
ld h char
h newChar)h )
• String[]
g[] split(String
p ( g regex)
g )
Class String Methods
• boolean startsWith(String prefix)
• String substring(int beginIndex)
• String substring(int beginIndex, int endIndex)
• String toLowerCase()
• String toUpperCase()
• String trim()
• static String valueOf(boolean b), dll
The StringBuffer Class
• represents a string that can be dynamically modified.
• Constructors:
– StringBuffer(): Constructs an empty string buffer
– StringBuffer(int capacity): Constructs an empty string
buffer with the specified initial capacity
– StringBuffer(String
ff ( initialString):
l )
Constructs a string buffer that initially contains the
specified string
StringBuffer methods
• StringBuffer append(String str): Appends str to the
current string buffer. Alternative forms support
appending primitives and character arrays; these are
converted to strings before appending.
• StringBuffer append(Object obj): Calls toString() on obj
and appends
pp the result to the current stringg buffer.
• StringBuffer insert(int offset, String str): Inserts str into
the current string buffer at position offset.
offset There are
numerous alternative forms.
StringBuffer methods
• StringBuffer reverse(): Reverses the characters of the
current string buffer.
• StringBuffer setCharAt(int offset, char newChar):
Replaces
p the character at p
position offset with newChar.
• StringBuffer setLength(int newLength): Sets the length
of the string buffer to newLength
newLength. If newLength is less
than the current length, the string is truncated. If
newLength is greater than the current length,
length the
string is padded with null characters.
String Concatenation the Easy Way
• concat() method of the String class
• pp
append()() method of the StringBuffer
g class
• + operator.
• Example:
String Concatenation:
a+b+c
p
Java compiler treats as:
new StringBuffer().append(a).append(b).append(c).toString();
The Math Class
• a collection of methods and two constants that
support mathematical computation.
• Two constans: E dan PI
• is final, so you cannot extend it.
• constructor is private,
private so you cannot create an
instance.
• the
h methodsh d and d constants are static
i
Methods of the Math Class
The Wrapper Classes
• is simply a class that encapsulates a
• single,
single immutable value
value.
– Integer class wraps up an int value,
– Float class wraps up a float value.
Primitives and Wrappers
Vector
• General purpose resizable array class
• Use instead of arrayy if
– you want to store a variable number of objects
– all elements are objects (not basic types)
– you need to look for an object within an array (search for an
element))
– you want to store objects of differing types
Vector VS Array
• Arrays are more efficient than vectors
• Use arrays if:
– you have a known storage requirement
or
– you are only dealing with values which have
the same basic type
Creating the Vector
• Vector is a class provided with the JDK
• Behaves in the same way as any other class
• Vector myVector = new Vector();
• Other constructors available
Adding Elements
void addElement(Object obj)
• Adds obj to the end of the vector
void insertElementAt(Object obj, int index)
• Adds obj at position index in the vector
Setting and Getting Elements
Object elementAt(int index)
• Retrieve the object at position index
void setElementAt(Object obj, int index)
• Set obj at position index
int size()
• Return size of the vector
Vector Example
import java.util.*;
class VectorTest {
public static void main(String args[]){
Vector hector = new Vector();
hector.addElement("apple");
hector.addElement("banana");
hector.addElement("date");
for(int i=0;i<hector.size();i++)
System.out.println(hector.elementAt(i));
}
}
Thread Class
void run()
– must be overridden.
overridden Not called directly
void start()
– called to begin excution of a thread
static void sleep(long m)
– makes the currently running thread pause for
the given number (m) of milliseconds
Thread Example
class ThreadExample {
private int count=0;
class AddThread extends Thread {
public void run() {
for(int i=0;i<10;i++) {
count++;
System.out.print("Add ");
System.out.println(count);
}
}
}
class SubtractThread extends Thread {
public void run() {
for(int i=0;i<10;i++) {
count‐‐;
System.out.print("Subtract ");
System.out.println(count);
}
}
}
public static void main(String args[]) {
ThreadExample ex = new ThreadExample();
AddThread a = ex.new AddThread();
S b
SubtractThread
Th d b = ex.new SubtractThread();
S b Th d()
a.start();
b.start();
}
}
Socket
• TCP connections are made using sockets
• Connecting to port 13 of any Unix machine will
give you the time
• Web servers use port 80
• Java provides Socket class for network
connections
Socket Class
• Socket (String host, int port)
• synchronized void close()
• InputStream getInputStream()
• OutputStream getOutputStream()
• void setSoTimeout(int timeout)
Server Socket
• ServerSocket(int port) throws IOException
• Socket accept() throws IOException
• void close() throws IOException
Pertanyaan???

You might also like