Beruflich Dokumente
Kultur Dokumente
String
!
" #
$
doubleValue()
floatValue()
intValue()
longValue()
parse ()
get ()
toString()
toHexString()
Supplementary Objectives
' Object
" ! Object
%Boolean Character Byte Short Integer Long Float
Double& " " ) "
(
*+ %Runtime& %SecurityManager& %ClassLoader&
%Thread& $ %Throwable& java.lang !
" %System&
%String StringBuffer& %Math&
, -.-
java.lang
int hashCode()
"
" $ ,
hashCode() 0 --5 61-
7 " # == !=
% 0 8-. 19& equals() Object true
equals()
" " " #
String , equals()
0 --5 61-
:
java.lang.Class
3 $ % " &
clone() " "
" / "
" , $ "
" public
String toString()
) " $
; @ ;
"
System.out.println(objRef) " $
toString() 0
+ 0 81 12
% 0 9- 826&
)
< " finalize() Object
) Object " =
0 >? 85.
/ "! ! /
Object
// Two strings.
String str1 = new String("WhoAmI");
String str2 = new String("WhoAmI");
str1.equals(str2): true
str1 == str2 : false
obj1.equals(obj2): false
obj1 == obj2 : false
Review Questions
hashCode() Object @
String
int
long
Object
Class
) x y $
x.equals(y) false
) x y $
(x.hashCode() == y.hashCode()) false
/ " clone
$ clone() Object @
CloneNotSupportedException
NotCloneableException
IllegalCloneException
NoClonesAllowedException
) " "
"
WrapperType( type v )
WrapperType( String str )
, -.2
4 % $ Character& valueOf(String s)
" " String
% %1 & , -.2&
NumberFormatException String "
) B valueOf()
" valueOf() !
% &
String toString()
4 toString(type v)
" " type % %1 & , -.2&
, " -. , B $
% &
% " & 3 3" " "
"NaN" "Infinity" "-Infinity" "
type typeValue()
4 % $ Boolean&
" # =
" " WrapperType #
" " WrapperType obj2
# " compareTo(WrapperType)
obj2 " WrapperType 7
ClassCastException
int hashCode()
4
$ " "
.MIN_VALUE
.MAX_VALUE
4 Value() "
" " " "
byte byteValue()
short shortValue()
int intValue()
long longValue()
float floatValue()
double doubleValue()
0 %6& , -.2
4 parse (String s)
" " String
parse $
Integer " Int NumberFormatException
String " % %?& , -.2&
type parseType(String s)
, " parse () !
2 -1 9 " $ =
'-' i
" )
i
( ) *
Character Class
Character
$ " char % 0 22 2>&
Character.MIN_VALUE
Character.MAX_VALUE
Character "
'
"
char ch = 'a';
if (Character.isLowerCase(ch)) ch = Character.toUpperCase(ch);
Boolean Class
Boolean " " true
false "
Boolean.TRUE
Boolean.FALSE
Review Questions
+ @
java.lang.Void
java.lang.Int
java.lang.Boolean
java.lang.Long
java.lang.String
, $ java.lang.Number @
java.lang.Float
java.lang.Byte
java.lang.Character
java.lang.Boolean
java.lang.Short
- @
Character
Byte
Thread
Short
Object
. " B ! @
Void
Integer
Boolean
Character
Object
/ " booleanValue() @
/ $ Void
/ compareTo()
/ $ Number
7 Boolean
0 @
String
Double compareTo()
Character intValue()
Byte $ Number
$
$ Math 7 Math
"!
Math.E
Math.PI
Math
3 # $ max(double, double)
$ " int double 0
double $ int
" round()
# " .? !
) .?
Math.floor() ) "
.? Math.ceil()
) .?
Math.ceil() ) "
.? Math.floor()
) " ! "
" "
% C82 C65& D
-.-
' 1 )
Argument: 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0
ceil: 7.0 8.0 8.0 8.0 8.0 8.0 8.0 8.0 8.0 8.0 8.0
floor: 7.0 7.0 7.0 7.0 7.0 7.0 7.0 7.0 7.0 7.0 8.0
round: 7 7 7 7 7 8 8 8 8 8 8
Argument: -7.0 -7.1 -7.2 -7.3 -7.4 -7.5 -7.6 -7.7 -7.8 -7.9 -8.0
ceil: -7.0 -7.0 -7.0 -7.0 -7.0 -7.0 -7.0 -7.0 -7.0 -7.0 -8.0
floor: -7.0 -8.0 -8.0 -8.0 -8.0 -8.0 -8.0 -8.0 -8.0 -8.0 -8.0
round: -7 -7 -7 -7 -7 -7 -8 -8 -8 -8 -8
Exponential Functions
static double pow(double d1, double d2)
d
exp() $ d% &
0 $ $
Trigonometry Functions
static double sin(double d)
sin() d
cos() d
tan() d
toRadians%& " $
toRadians%& " $
0 $
4$ "
random() # ..
-. "
/ $
.% "& -.
% $ "&
Review Questions
E" 11 $ @
class MyClass {
public static void main(String[] args) {
double v = 10.5;
System.out.println(Math.ceil(v)); // (1)
System.out.println(Math.round(v)); // (2)
System.out.println(Math.floor(v)); // (3)
System.out.println((int) Math.ceil(v)); // (4)
System.out.println((int) Math.floor(v)); // (5)
}
}
%-&
%2&
%8&
%6&
%?&
Math @
double tan2(double)
double cos(double)
int abs(int a)
double ceil(double)
round(float) Math @
int
float
double
Integer
Float
ceil(double) Math @
int
float
double
Integer
Double
+ @
0 0
0 1
-1 0
-1 1
3 "
, $ ((int)(Math.random()*4))@
) " "
) " .
" - 2
) " 8
) " 6
D
-1B % 0 2- 28&
String " =
$ StringBuffer
String
= % B $
String str3 = "You cannot" + " change me!"; // Compile-time constant expression
String B" $
String / String "
" String
final " "
String Constructors
String = String
"
String(String s)
String
String
String()
String ""
3 String
/ intern()
String F
D String
( ! 2
System.out.println("str1.equals(str5): " +
str1.equals(str5)); // (8) true
System.out.println("str1 == Auxiliary.str1: " +
(str1 == Auxiliary.str1)); // (9) true
System.out.println("str1.equals(Auxiliary.str1): " +
str1.equals(Auxiliary.str1)); // (10) true
class Auxiliary {
static String str1 = "You cannot change me!"; // Interned
}
emptyStr:
str1 == str2: true
str1.equals(str2): true
str1 == str3: true
str1.equals(str3): true
str1 == str4: false
str1.equals(str4): true
str1 == str5: false
str1.equals(str5): true
str1 == Auxiliary.str1: true
str1.equals(Auxiliary.str1): true
"You cannot change me!".length(): 21
/ $ charAt()
$. $
) $" "
StringIndexOutOfBoundsException
D $srcBegin $
srcEnd-1 " $
dstBegin $dstbegin+(srcEnd-srcBegin)-1
(srcEnd-srcBegin) / IndexOutOfBoundsException
int length()
+) ! * (
frequencyData # $
"
frequencyData[str.charAt(i)]++; // (4)
Comparing Strings
D "
boolean test = 'a' < 'b'; // true since 0x61 < 0x62
"
"abba" "aha"
'b' "abba" 'h' "aha"
#
compareTo() "
" 0 #
" 0, $
" 0, $
< $
String toLowerCase()
String toLowerCase(Locale locale)
3
String
B B "
Character
% & %
Locale.getDefault()&
H H
% & =
4$
Concatenation of Strings
D
" +
0 81 12 )
/ %8& concat()
" String "Just grooving"
"! String "
String "Just grooving in heap."
" String billboard String
String "Just
grooving" " %8&
0 $
3 String motto
%-& %2& %8& String "Program once"
%2& String "Program once, execute
everywhere." %8& motto String
$ %8&
, $
, $
$ ) $ " $
.) $
" # F " B-
, $
, $
$
String
) $
"
4$
Extracting Substrings
String trim()
%
" # '\u0020'& % &
% & "
endIndex $
$endIndex-1 ) $" "
StringIndexOutOfBoundsException
4$ $
String toString()
/ " " /
obj # " obj.toString() boolean "
true false " "true" "false" char
"
4$ "
7 $
%toCharArray()& " %getBytes()&
$ %startsWith()& $ %endsWith()& hashCode()
"
Review Questions
- String @
+=
&
. $ $ "kap" String
str = "kakapo"@
str.substring(2, 2)
str.substring(2, 3)
str.substring(2, 4)
str.substring(2, 5)
str.substring(3, 3)
/ @
class MyClass {
public static void main(String[] args) {
String str1 = "str1";
String str2 = "str2";
String str3 = "str3";
str1.concat(str2);
System.out.println(str3.concat(str1));
}
}
$ str3.concat(str1)
" println()
str3str1str2
str3
str3str1
str3str2
0 trim() String @
)
"
)
"
)
"
)
"
3 "
String
0 String
/ final
$ @
"co".concat("ol")
("co" + "ol")
false false
false true
true false
true true
" String @
()
(int capacity)
(char[] data)
(String str)
+ String @
trim()
length()
concat(String)
hashCode()
reverse()
, charAt() String @
charAt() Character
$ ("abcdef").charAt(3)
$ -
- $ " true@
("hello".concat("there")).equals("hello there")
. @
* 11
$ =
/ String StringBuffer
final $ Object < String
% & StringBuffer " " String
StringBuffer B 0 "
" "
StringBuffer(String s)
StringBuffer String
-1
StringBuffer(int length)
StringBuffer
" length .
StringBuffer()
StringBuffer
-1
4$ StringBuffer =
$
" $. $
/ StringIndexOutOfBoundsException
$ "
" append()
"
StringBuffer append(boolean b)
StringBuffer append(int i)
StringBuffer append(long l)
StringBuffer append(float f)
StringBuffer append(double d)
" String.valueOf
() offset
# .
/ StringBuffer
"
StringBuffer reverse()
4$
buffer.delete(4,12); // "bana"
buffer.append(42); // "bana42"
buffer.insert(4,"na"); // "banana42"
buffer.reverse(); // "24ananab"
buffer.deleteCharAt(buffer.length()-1); // "24anana"
buffer.append('s'); // "24ananas"
/ " "
"!
buffer "
buffer.delete(4,12).append(42).insert(4,"na").reverse().
deleteCharAt(buffer.length()-1).append('s'); // "24ananas"
" "
(((((buffer.delete(4,12)).append(42)).insert(4,"na")).reverse()).
deleteCharAt(buffer.length()-1)).append('s'); // "24ananas"
4 " "
"! $ buffer
+ $
# "
4 minCapacity ) $
$ # " newLength
# .
%'\u0000'&
"
newLength
Review Questions
/ @
String
$ (s == sb)
$ (s.equals(sb))
ClassCastException
0 @
setLength
StringBuffer
StringBuffer sb
println()
StringIndexOutOfBoundsException
have a
* ce day
" StringBuffer
@
()
(int capacity)
(char[] data)
(String str)
StringBuffer @
trim()
length()
append(String)
reverse()
setLength(int)
meeny miny mo
eenny miny mo
Chapter Summary
Object ("
" "
" "
Math "
String
StringBuffer
Programming Exercises
D Pair )
equals() hashCode() Pair
Pair + !
toString() $
Pair 7 Pair
/ $ !
"
# " !