Title: Builtin-in Classes in java.lang and java.util
1Builtin-in Classes in java.lang and java.util
- Wrapper Classes
- Boolean, Character, Byte, Short, Integer
- Long, Float, Double
- String StringBuffer
- Vector
- LinkedList
- Hashtable
2Wrapper Classes
Wrapper class Primitive Boolean boolean Charact
er char Byte byte Short short Integer int
Long long Float float Double double
3The Integer Class
- Convert to different types
- Parse integer strings
byte byteValue() double doubleValue() float
floatValue() int intValue() long longValue()
short shortValue() String toString()
static String toBinaryString(int value) static
String toHexString(int value) static String
toOctalString(int value) static
String toString(int value) static
String toString(int value, int radix)
static Integer decode(String s) static int
parseInt(String s) static int
parseInt(String s, int radix) static Integer
valueOf(String s) static Integer
valueOf(String s, int radix)
4Using the Integer Class
public class TestStack public static void
main(String args) Stack s new Stack()
int i s.push(new Integer(1))
s.push(new Integer(2)) i
((Integer)s.pop()).intValue()
System.out.println(s.pop())
5The Character Class
char charValue() static int digit(char c, int
radix) static int getNumericValue(char c)
static boolean isDigit(char c) static boolean
isLetter(char c) static boolean
isLetterOrDigit(char c) static boolean
isLowerCase(char c) static boolean
isSpaceChar(char c) static boolean
isUpperCase(char c) static char
toLowerCase(char c) String toString() static
char toUpperCase(char c)
6The String Class
- Defined in java.lang.
- All string literals are immutable instances of
String. - Important Identical literals have the same
object reference. - Useful methods include
- charAt, equals, length, startsWith, indexOf,
toLowerCase, etc.
7String Comparisons
- Several ways to compare two String objects
- compareTo, equals, equalsIgnoreCase.
- Never use to test for content equality between
strings - This only gives reference equality.
8The Immutability of Strings
- We often construct new strings out of existing
strings, e.g., using . - Because String objects are immutable, we always
obtain a new instance - String noun "dog"
- noun "s"
- Similarly with concat, etc.
9Parsing Strings
public int countSpaces(String s) int count
0 // Look for the first. int index
s.indexOf(' ') // indexOf returns -1 on
failure. while(index gt 0) // We
found one. count 1 // Look
for the next just after the last one.
index s.indexOf(' ',index1) return
count
10Strings from Primitive Values
- Direct assignment is illegal
- String s 32 // Forbidden.
- Implicit type conversion is common
- String s ""num
- Via the static valueOf method
- String s String.valueOf(num)
11The StringBuffer Class
- String-like objects that are mutable.
- Used for building a String out of multiple
pieces. - Size is dynamic and unlimited.
12java.lang.StringBuffer
StringBuffer append(xxx arg) where xxx can be
any primitive type or objet type StringBuffer
insert(int offset, xxx obj) int capacity() char
charAt(int index) int length() void
setCharAt(int index,char ch) StringBuffer
reverse()
13Formatting an Integer
public String formatInt(int number,int width)
// Create space for the full width.
StringBuffer formattedInt
new StringBuffer(width) // Append a
string version of the number.
formattedInt.append(number) // How many
extra spaces are required? int spaces
width-formattedInt.length() for(int i 0
i lt spaces i) formattedInt.insert(0,"
") return formattedInt.toString()