Download Built-in classes

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Builtin-in Classes in java.lang
and java.util
 Wrapper Classes
– Boolean, Character, Byte, Short, Integer
– Long, Float, Double
 String & StringBuffer
 Vector
 LinkedList
 Hashtable
1
Wrapper Classes
Wrapper class
Primitive
Boolean
boolean
Character
char
Byte
Short
Integer
Long
byte
short
int
long
Float
Double
float
double
2
The Integer Class
 Convert to different types
byte
double
float
int
long
short
String
byteValue()
doubleValue()
floatValue()
intValue()
longValue()
shortValue()
toString()
static String
static String
static String
static String
static String
toBinaryString(int value)
toHexString(int value)
toOctalString(int value)
toString(int value)
toString(int value, int radix)
 Parse integer strings
static Integer
static int
static int
static Integer
static Integer
decode(String s)
parseInt(String s)
parseInt(String s, int radix)
valueOf(String s)
valueOf(String s, int radix)
3
Using 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());
}
}
4
The Character Class
char
static int
static int
static boolean
static boolean
static boolean
static boolean
static boolean
static boolean
static char
String
static char
charValue()
digit(char c, int radix)
getNumericValue(char c)
isDigit(char c)
isLetter(char c)
isLetterOrDigit(char c)
isLowerCase(char c)
isSpaceChar(char c)
isUpperCase(char c)
toLowerCase(char c)
toString()
toUpperCase(char c)
5
The 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.
6
String 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.
7
The 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.
8
Parsing Strings
public int countSpaces(String s){
int count = 0;
// Look for the first.
int index = s.indexOf(' ');
// indexOf returns -1 on failure.
while(index >= 0){
// We found one.
count += 1;
// Look for the next just after the last one.
index = s.indexOf(' ',index+1);
}
return count;
}
9
Strings 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);
10
The StringBuffer Class
 String-like objects that are mutable.
 Used for building a String out of multiple
pieces.
 Size is dynamic and unlimited.
11
java.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()
12
Formatting 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 < spaces; i++){
formattedInt.insert(0," ");
}
return formattedInt.toString();
}
13