Download presentation
Presentation is loading. Please wait.
1
1 Builtin-in Classes in java.lang and java.util 4 Wrapper Classes –Boolean, Character, Byte, Short, Integer –Long, Float, Double 4 String & StringBuffer 4 Vector 4 LinkedList 4 Hashtable
2
2 Wrapper Classes Wrapper classPrimitive Booleanboolean Characterchar Bytebyte Shortshort Integerint Longlong Floatfloat Doubledouble
3
3 byte byteValue()byteValue double doubleValue()doubleValue float floatValue()floatValue int intValue()intValue longlongValue()longValue short shortValue()shortValue String toString()toString The Integer Class 4 Convert to different types 4 Parse integer strings static Integer decode(String s)decode static int parseInt(String s)parseInt static int parseInt(String s, int radix)parseInt static Integer valueOf(String s)valueOf static Integer valueOf(String s, int radix)valueOf static StringtoBinaryString(int value)toBinaryString static StringtoHexString(int value)toHexString static String toOctalString(int value)toOctalString static StringtoString(int value)toString static StringtoString(int value, int radix)toString
4
4 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()); }
5
5 The Character Class char charValue()charValue static int digit(char c, int radix)digit static intgetNumericValue(char c)getNumericValue static boolean isDigit(char c)isDigit static boolean isLetter(char c)isLetter static boolean isLetterOrDigit(char c)isLetterOrDigit static boolean isLowerCase(char c)isLowerCase static boolean isSpaceChar(char c)isSpaceChar static boolean isUpperCase(char c)isUpperCase static char toLowerCase(char c)toLowerCase String toString()toString static char toUpperCase(char c)toUpperCase
6
6 The String Class Defined in java.lang. All string literals are immutable instances of String. –Important: Identical literals have the same object reference. 4 Useful methods include: –charAt, equals, length, startsWith, indexOf, toLowerCase, etc.
7
7 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.
8
8 The Immutability of Strings We often construct new strings out of existing strings, e.g., using +. 4 Because String objects are immutable, we always obtain a new instance: –String noun = "dog"; –noun += "s"; Similarly with concat, etc.
9
9 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; }
10
10 Strings from Primitive Values 4 Direct assignment is illegal: –String s = 32; // Forbidden. 4 Implicit type conversion is common: –String s = ""+num; Via the static valueOf method: –String s = String.valueOf(num);
11
11 The StringBuffer Class 4 String-like objects that are mutable. Used for building a String out of multiple pieces. 4 Size is dynamic and unlimited.
12
12 java.lang.StringBuffer StringBuffer append(xxx arg)StringBuffer where xxx can be any primitive type or objet type StringBuffer insert(int offset, xxx obj)StringBuffer int capacity() char charAt(int index) int length() void setCharAt(int index,char ch) StringBufferStringBuffer reverse()
13
13 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(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.