0% found this document useful (0 votes)
44 views

Java Lang

The document provides an overview of several core Java classes including Math, String, StringBuffer, wrapper classes, Process, Runtime, and System. It discusses the key methods and functionality of each class through examples. The Math class contains predefined constants and methods for mathematical operations. The String and StringBuffer classes represent strings but String is immutable while StringBuffer is mutable. Wrapper classes provide object representations for primitive types. Process and Runtime represent and control running programs. System provides useful methods for I/O, time, and other system operations.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Java Lang

The document provides an overview of several core Java classes including Math, String, StringBuffer, wrapper classes, Process, Runtime, and System. It discusses the key methods and functionality of each class through examples. The Math class contains predefined constants and methods for mathematical operations. The String and StringBuffer classes represent strings but String is immutable while StringBuffer is mutable. Wrapper classes provide object representations for primitive types. Process and Runtime represent and control running programs. System provides useful methods for I/O, time, and other system operations.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Tour of java.lang.* & java.util.

* Classes

Topics

Math Class String and the StringBuffer Class Wrapper Classes Process and the Runtime Class System Class

Math Class
3

The Math Class


Provides predefined constants and methods for performing different mathematical operations Methods:

The Math Class: Methods

The Math Class: Example


1 2 3

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

The Math Class: Example


12 13 1( 1$ 1+ 11. 10 2* 21

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

The Math Class: Example


22 23 2( 2$ 2+ 22. 20 3* 31

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

The String Class

Definition:

Represents combinations of character literals Using Java, strings can be represented using:

Array of characters he String class

!ote: A String ob"ect is different from an array of characters# %% constructors

$tring constructors

10

The String Class: Constructors


1 2 3 ( $ + . 0 1* 11

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

11empty string 11s26!hello!' 11s36!2orld!

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)'

The String Class: Constructors


12 13 1( 1$ 1+ 11. 10

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

The String Class: Methods

13

The String Class: Methods

14

The String Class: Example


1 2 3 ( $ + . 0 1* 11 12 13

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

The String Class: Example


14 1$ 1+ 11. 10 2* 21 22 23 2( 2$

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

The String Class: Example


26 22. 20 3* 31 32 33 3( 3$ 3+ 33. 30

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

The String Class: Example


40 (1 (2 (3 (( ($ (+ ((. (0 $* $1 $2

System out println(!=rim D! !

a b c d e "

D!% D!! &

a b c d e "

! trim() & !D!!)'

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

The StringBuffer Class

Problem &ith String ob"ects:

'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

The StringBuffer Class: Methods

21

The StringBuffer Class: Example


1 2 3 ( $ + . 0 1* 11 12

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

The StringBuffer Class: Example


13 1( 1$ 1+ 11. 10 2* 21 22 23

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

The StringBuffer Class: Example


24 2$ 2+ 22. 20 3* 31 32

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

The Wrapper Classes

$ome .acts:

Primitive data types are not ob"ects

Cannot access methods of the Object class

'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:

'b"ect representations of simple non1ob"ect variables


26

The Wrapper Classes

/rapper classes are very similar to their primitive e2uivalents3


Capitali4ed $pelled out versions of the primitive data types

27

The Wrapper Classes: Boolean Example


1 2 3 ( $ + . 0 1* 11 12

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

The Wrapper Classes: Boolean Example


13 1( 1$ 1+ 11. 10

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

The Process Class

Definition:

Provides methods for manipulating processes


5illing the process Running the process Chec,ing the status of the process

Represents running programs

Methods:

31

The Runtime Class

Represents the runtime environment 6as t&o important methods:

32

The Process and Runtime Class: Example


1 2 3 ( $ + . 0 1* 11 12 13

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

The System Class

Provides many useful fields and methods


$tandard input $tandard output Utility method for fast copying of a part of an array

35

The System Class: Methods

36

The System Class: Example


1 2 3 ( $ + . 0 1* 11 12

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

The System Class: Example


13 1( 1$ 1+ 11. 10 2* 21

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

The System Class: Example


22 23 2( 2$ 2+ 22. 20 3* 31 32

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

"ava3vm3specification3version "ava3vm3specification3vendor "ava3vm3specification3name "ava3vm3version "ava3vm3vendor "ava3vm3name

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

"ava3specification3version Java Runtime 7nvironment specification version "ava3specification3vendor "ava3specification3name

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

"ava3library3path -ist of paths to search &hen loading libraries

"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

User>s account name User>s home directory


41

user3dir User>s current &or,ing directory

Example: Display System Properties


public static void main($tring?@ args* A Properties p% B $ystem3getProperties(*C p%3list($ystem3out*C D

42

Example: Display System Properties


"ava3runtime3nameBJava( M* E Runtime 7nvironment, $tand333 sun3boot3library3pathBC:=Program .iles=Java="d,%3F3GHGI="re333 "ava3vm3versionB%3F3GHGI1bGF "ava3vm3vendorB$un Microsystems )nc3 "ava3vendor3urlBhttp:;;"ava3sun3com; path3separatorBC "ava3vm3nameBJava 6ot$pot( M* Client 8M file3encoding3p,gBsun3io user3countryBU$ sun3os3patch3levelB$ervice Pac, E "ava3vm3specification3nameBJava 8irtual Machine $pecification user3dirBC:=handsonE=development="avalang=samp333 "ava3runtime3versionB%3F3GHGI1bGF "ava3a&t3graphicsenvBsun3a&t3/inJEKraphics7nvironment "ava3endorsed3dirsBC:=Program .iles=Java="d,%3F3GHGI="re333 333

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

The Properties Class: Example


22 23 2( 2$ 2+ 22. 20 3* 31 32 33 3(

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

The Date Class: Example


22 23 2( 2$ 2+ 22. 20 3*

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

You might also like