3.1

Cards (47)

  • Trace Code String s1 = "Welcome to Java"; String s2 = new String("Welcome to Java"); String s3 = "Welcome to Java";: String Interned string object for "Welcome to Java" s1 s2 s3
  • A string object for "Welcome to Java" s1 s2 s3
  • You can add, insert, or append new contents into a string buffer, whereas the value of a String object is fixed once the string is created.
  • To convert a double value to a string, use String.valueOf(5.44).
  • A StringBuilder/StringBuffer can be used wherever a string is used.
  • These methods have the same name valueOf with different argument types char, char[], double, long, int, and float.
  • StringBuilder/StringBuffer is more flexible than String.
  • The StringBuilder class has several constructors: java.lang.StringBuilder() constructs an empty string builder with capacity 16, java.lang.StringBuilder(capacity: int) constructs a string builder with the specified capacity, and java.lang.StringBuilder(s: String) constructs a string builder with the specified string.
  • The StringBuilder/StringBuffer class is an alternative to the String class.
  • The StringBuilder class provides several methods for modifying strings in the builder: +append(data: char[]), +append(data: char[], offset: int, len: int), +append(v: aPrimitiveType), +append(s: String), +delete(startIndex: int, endIndex: int), +deleteCharAt(index: int), +insert(index: int, data: char[], offset: int, len: int), +insert(offset: int, data: char[]), +insert(offset: int, b: aPrimitiveType), +insert(offset: int, s: String), +replace(startIndex: int, endIndex: int, s: String), +reverse(), +setCharAt(index: int, ch: char).
  • The String class provides several static valueOf methods for converting a character, an array of characters, and numeric values to strings.
  • The return value is a string consisting of characters ‘5’, ‘.’, ‘4’, and ‘4’.
  • BigInteger c = a.multiply(b); // 9223372036854775807 * 2
  • BigDecimal c = a.divide(b, 20, BigDecimal.ROUND_UP);
  • The parseInt method in the Integer class and the parseDouble method in the Double class are used to parse a numeric string into an int value and a double value respectively.
  • The following statement in (a) can be simplified as in (b): Integer[] intArray = {new Integer(2), new Integer(4), new Integer(3)};
  • The methods “convert” objects into primitive type values.
  • BigDecimal b = new BigDecimal(3);
  • BigInteger b = new BigInteger("2");
  • Both the BigInteger and BigDecimal classes are immutable and extend the Number class and implement the Comparable interface.
  • JDK 1.5 allows primitive type and wrapper classes to be converted automatically.
  • BigInteger a = new BigInteger("9223372036854775807");
  • The numeric wrapper classes have a useful class method, valueOf(String s), which creates a new object initialized to the value represented by the specified string.
  • Each numeric wrapper class has two overloaded parsing methods to parse a numeric string into an appropriate numeric value.
  • If you need to compute with very large integers or high precision floating-point values, you can use the BigInteger and BigDecimal classes in the java.math package.
  • Each numeric wrapper class implements the abstract methods doubleValue, floatValue, intValue, longValue, and shortValue, which are defined in the Number class.
  • BigDecimal a = new BigDecimal(1.0);
  • Examples: s1 == s is false, s1 == s3 is true
  • Conversions between Strings and Arrays
  • Substrings (substring(index), substring(start, end))
  • Constructing a String: String message = "Welcome to Java"; String message = new String("Welcome to Java"); String s = new String();
  • Comparisons (equals, compareTo)
  • Strings are immutable; their contents cannot be changed
  • String Conversions
  • Obtaining String length and Retrieving Individual Characters in a string
  • Converting Characters and Numeric Values to Strings
  • String Concatenation (concat)
  • Interned Strings: JVM uses a unique instance for string literals with the same character sequence
  • Finding a Character or a Substring in a String
  • The Integer class has methods such as byteValue(), shortValue(), intValue(), longValue(), floatValue(), doubleValue(), compareTo(), toString(), valueOf(), and parseInt().