Java Lang
Java Lang
* Classes
Topics
Math Class String and the StringBuffer Class Wrapper Classes Process and the Runtime Class System Class
Math Class
3
Provides predefined constants and methods for performing different mathematical operations Methods:
class MathDemo { public static void main(St ring args[]) { System out println(!absolute value o" #$% ! & Math abs(#$))' System out println(!absolute value o" $% ! & Math abs(#$))' System out println(!random number(ma) is 1*)% ! & Math random(),1*)' System out println(!ma) o" 3 $ and 1 2% ! & Math ma)(3 $/1 2))' System out println(!min o" 3 $ and 1 2% ! & Math min(3 $/1 2))' 11continued
6
$ + . 0 1* 11
System out println(!ceiling o" 3 $% ! & Math ceil(3 $))' System out println(!"loor o" 3 $% ! & Math "loor(3 $))' System out println(!e raised to 1% ! & Math e)p(1))' System out println(!log 1*% ! & Math log(1*))' System out println(!1* raised to 3% ! & Math po2(1*/3))' 11continued
System out println(!rounded o"" value o" pi% ! & Math round(Math 34))' System out println(!s5uare root o" $ 6 ! & Math s5rt($))' System out println(!1* radian 6 ! & Math toDegrees(1*) & ! degrees!)' System out println(!sin(0*)% ! & Math sin(Math to7adians(0*)))' 8 8
String Class
9
Definition:
Represents combinations of character literals Using Java, strings can be represented using:
$tring constructors
10
class String9onstructorsDemo { public static void main(String args[]) { String s1 6 ne2 String()' String s2 6 ne2 String(chars)' String s3 6 ne2 String(bytes)' String s$ 6 ne2 String(s2)' String s+ 6 s2' 11continued
11
char chars[] 6 { :h:/ :e:/ :l:/ :l:/ :o:8' byte bytes[] 6 { :2:/ :o:/ :r:/ :l:/ :d: 8' String s( 6 ne2 String(chars/ 1/ 3)'
System out println(s1)' System out println(s2)' System out println(s3)' System out println(s()' System out println(s$)' System out println(s+)' 8 8
12
13
14
class StringDemo { public static void main(String args[]) { String name 6 !;onathan!' System out println(!name% ! & name)' System out println(!3rd character o" name% ! & name char<t(2))' 1, character that "irst appears alphabetically has lo2er unicode value ,1 System out println(!;onathan compared to Solomon% ! & name compare=o(!Solomon!))' System out println(!Solomon compared to ;onathan% ! & !Solomon! compare=o(!;onathan!))' 11continued
15
1, :;: has lo2er unicode value compared to :>: ,1 System out println(!;onathan compared to >onathan% ! & name compare=o(!>onathan!))' System out println(!;onathan compared to >onathan (ignore case)% ! & name compare=o4gnore9ase(!>onathan!))' System out println(!4s ;onathan e5ual to ;onathan? ! & name e5uals(!;onathan!))' System out println(!4s ;onathan e5ual to >onathan? ! & name e5uals(!>onathan!))' System out println(!4s ;onathan e5ual to >onathan (ignore case)? ! & name e5uals4gnore9ase(!>onathan!))' 11continued
16
char char<rr[] 6 !@i AA! to9har<rray()' 1, Beed to add 1 to the endSrc inde) o" get9hars ,1 !;onathan! get9hars(*/ 2/ char<rr/ 3)' System out print(!get9hars method% !)' System out println(char<rr)' System out println(!Cength o" name% ! & name length())' System out println(!7eplace a:s 2ith e:s in name% ! & name replace(:a:/ :e:))' 1, Beed to add 1 to the end4nde) parameter o" substring,1 System out println(!< substring o" name% ! & name substring(*/ 2))' 11continued
17
a b c d e "
a b c d e "
System out println(!String representation o" boolean e)pression 1*E1*% ! & String valueF"(1*E1*))' 1, toString method is implicitly called in the println method,1 System out println(!String representation o" boolean e)pression 1*G1*% ! & (1*G1*))' 1, Bote there:s no change in the String ob>ect name even a"ter applying all these methods System out println(!name% ! & name)' 8 8
18
,1
StringBuffer Class
19
'nce created, can no longer be modified ()t is a final class* $imilar to a $tring ob"ect +ut, mutable or can be modified
A $tring+uffer ob"ect
Unli,e String in this aspect -ength and content may changed through some method calls
20
21
class StringHu""erDemo { public static void main(String args[]) { StringHu""er sb 6 ne2 StringHu""er(!;onathan!)' System out println(!sb 6 ! & sb)' 1, initial capacity is 1+ ,1 System out println(!capacity o" sb% !&sb capacity())' System out println(!append D:FD: to sb% ! & sb append(:F:))' System out println(!sb 6 ! & sb)' System out println(!3rd character o" sb% ! & sb char<t(2))' 11continued
22
char char<rr[] 6 !@i AA! to9har<rray()' 1, Beed to add 1 to the endSrc inde) o" get9hars ,1 sb get9hars(*/ 2/ char<rr/ 3)' System out print(!get9hars method% !)' System out println(char<rr)' System out println(!4nsert D:>oD: at the 3rd cell% ! & sb insert(2/ !>o!))' System out println(!Delete D:>oD: at the 3rd cell% ! & sb delete(2/())' System out println(!length o" sb% ! & sb length())' 11continued
23
System out println(!replace% ! & sb replace(3/ 0/ ! Fng!))' 1, Beed to add 1 to the end4nde) parameter o" substring,1 System out println(!substring (1st t2o characters)% ! & sb substring(*/ 3))' System out println(!implicit toString()% ! & sb)' 8 8
24
Wrapper Class
25
$ome .acts:
'nly actual ob"ects can access methods of the Object class /hy &rapper classes0
!eed an ob"ect representation for the primitive type variables to use Java built1in methods
Definition:
27
class HooleanIrapper { public static void main(String args[]) { boolean booleanJar 6 1E2' Hoolean booleanFb> 6 ne2 Hoolean(!=rue!)' 1, primitive to ob>ect' can also use valueF" method ,1 Hoolean booleanFb>2 6 ne2 Hoolean(booleanJar)' System out println(!booleanJar 6 ! & booleanJar)' System out println(!booleanFb> 6 ! & booleanFb>)' System out println(!booleanFb>2 6 ! & booleanFb>2)' 11continued
28
System out println(!compare 2 2rapper ob>ects% ! & booleanFb> e5uals(booleanFb>2))' 1, ob>ect to primitive ,1 booleanJar 6 booleanFb> booleanJalue()' System out println(!booleanJar 6 ! & booleanJar)' 8 8
29
Process Class
30
Definition:
5illing the process Running the process Chec,ing the status of the process
Methods:
31
32
class 7untimeDemo { public static void main(String args[]) { 7untime rt 6 7untime get7untime()' 3rocess proc' try { proc 6 rt e)ec(!regedit!)' proc 2aitKor()' 11try removing this line 8 catch (L)ception e) { System out println(!regedit is an unMno2n command !)' 8 8 8
33
System Class
34
$tandard input $tandard output Utility method for fast copying of a part of an array
35
36
import >ava io ,' class SystemDemo { public static void main(String args[]) thro2s 4FL)ception { int arr1[] 6 ne2 int[1*$****]' int arr2[] 6 ne2 int[1*$****]' long start=ime/ end=ime' 1, initialiNe arr1 ,1 "or (int i 6 *' i G arr1 length' i&&) { arr1[i] 6 i & 1' 8 11continued
37
1, copying manually ,1 start=ime 6 System current=imeMillis()' "or (int i 6 *' i G arr1 length' i&&) { arr2[i] 6 arr1[i]' 8 end=ime 6 System current=imeMillis()' System out println(!=ime "or manual copy% ! & (end=ime#start=ime) & ! ms !)' 11continued
38
1, using the copy utility provided by >ava ,1 start=ime 6 System current=imeMillis()' System arraycopy(arr1/ */ arr2/ */ arr1 length)' end=ime 6 System current=imeMillis()' System out println(!=ime "or manual copy% ! & (end=ime#start=ime) & ! ms !)' System gc()' 11"orce garbage collector to 2orM System set4n(ne2 Kile4nputStream(!temp t)t!))' System e)it(*)' 8 8
39
System Properties
"ava3version Java Runtime 7nvironment version "ava3vendor Java Runtime 7nvironment vendor "ava3vendor3url "ava3home Java vendor URJava 8irtual Machine specification version Java 8irtual Machine specification vendor Java 8irtual Machine specification name Java installation directory
Java 8irtual Machine implementation version Java 8irtual Machine implementation vendor Java 8irtual Machine implementation name Java Runtime 7nvironment specification vendor Java Runtime 7nvironment specification name
40
System Properties
"ava3class3version "ava3class3path "ava3io3tmpdir "ava3compiler os3name os3version file3separator path3separator line3separator user3name user3home
Java class format version number Java class path Default temp file path !ame of J) compiler to use
"ava3e9t3dirs Path of e9tension directory or directories 'perating system name 'perating system version .ile separator (:;: on U!)<* Path separator (::: on U!)<* -ine separator (:=n: on U!)<* os3arch 'perating system architecture
42
43
Properties Class
44
Properties Class
he Properties class represents a persistent set of properties he Properties can be saved to a stream or loaded from a stream
ypically a file
7ach ,ey and its corresponding value in the property list is a string A property list can contain another property list as its :defaults:C this second property list is searched if the property ,ey is not found in the original property list
45
11 set up ne2 properties ob>ect 11 "rom "ile !my3roperties t)t! Kile4nputStream propKile 6 ne2 Kile4nputStream(!my3roperties t)t!)' 3roperties p 6 ne2 3roperties(System get3roperties())' p load(propKile)' 11 set the system properties System set3roperties(p)' 11 display ne2 properties System get3roperties() list(System out)'
46
Date Class
47
Date Class
Represents a precise moment in time, do&n to the millisecond Dates are represented as a long type that counts the number of milliseconds since midnight, January %, %LMG, Kreen&ich Mean ime
48
11 7eturn the number o" milliseconds in the Date 11 as a long/ using the get=ime() method Date d1 6 ne2 Date()' 11 timed code goes here "or (int i6*' iG1******' i&&) { int > 6 i'8 Date d2 6 ne2 Date()' long elapsedOtime 6 d2 get=ime() # d1 get=ime()' System out println(!=hat tooM ! & elapsedOtime & ! milliseconds!)'
49
Than !ou"
50