Builtin-in Classes in java.lang and java.util - PowerPoint PPT Presentation

About This Presentation
Title:

Builtin-in Classes in java.lang and java.util

Description:

static int parseInt(String s, int radix) static Integer valueOf(String s) ... static int digit(char c, int radix) static int getNumericValue(char c) ... – PowerPoint PPT presentation

Number of Views:286
Avg rating:3.0/5.0
Slides: 14
Provided by: davidj77
Category:
Tags: builtin | classes | java | lang | radix | util

less

Transcript and Presenter's Notes

Title: Builtin-in Classes in java.lang and java.util


1
Builtin-in Classes in java.lang and java.util
  • Wrapper Classes
  • Boolean, Character, Byte, Short, Integer
  • Long, Float, Double
  • String StringBuffer
  • Vector
  • LinkedList
  • Hashtable

2
Wrapper Classes
Wrapper class Primitive Boolean boolean Charact
er char Byte byte Short short Integer int
Long long Float float Double double
3
The 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)
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
The 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)
6
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.

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
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.

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 gt 0) // We
found one. count 1 // Look
for the next just after the last one.
index s.indexOf(' ',index1) return
count
10
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)

11
The StringBuffer Class
  • String-like objects that are mutable.
  • Used for building a String out of multiple
    pieces.
  • Size is dynamic and unlimited.

12
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()
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 lt spaces i) formattedInt.insert(0,"
") return formattedInt.toString()
Write a Comment
User Comments (0)
About PowerShow.com