1 / 13

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

Builtin-in Classes in java.lang and java.util. Wrapper Classes Boolean, Character, Byte, Short, Integer Long, Float, Double String & StringBuffer Vector LinkedList Hashtable. Wrapper Classes. Wrapper class Primitive Boolean boolean Character char Byte byte Short short

quasar
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 Character 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 >= 0){ // We found one. count += 1; // Look for the next just after the last one. index = s.indexOf(' ',index+1); } 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 < spaces; i++){ formattedInt.insert(0," "); } return formattedInt.toString(); }

More Related