You are on page 1of 41

How good are your Java skills?

Today

Java traps, pitfalls, puzzles and problems. A tour of things that go wrong ome e!amples are taken from Java Puzzlers by "lo#h and $after %efinitely worth a read, no matter what level of programmer you are Paperback: &'( pages Publisher: Addison )esley *(' Jul (++,ISBN-10: +&('&&./01 ISBN-13: 2/03+&('&&./0'

4oly4ain
public class PolyPain 5 publi# tring name 6 74arent78 publi# void 4rint*- 5 ystem.out.println*74arent7-8 9 publi# stati# void 4rint(*- 5 ystem.out.println*74arent7-8 9 9 public class PolyPainChild extends PolyPain 5 publi# tring name 6 7:hild78 publi# void 4rint*- 5 ystem.out.println*7:hild7-8 9 publi# stati# void 4rint(*- 5 ystem.out.println*7:hild7-8 9 9 public static void ain!Strin"#$ ar"s% & 4oly4ain:hild # 6 new 4oly4ain:hild*-8 4oly4ain p 6 *4oly4ain-#8 p.4rint*-8 p.4rint(*-8 ystem.out.println*p.name); 9

A. ".

;4arent< ;:hild<

4oly4ain

=verridden methods e!hibit dynami# polymorphism =verridden stati# methods do not =verridden *;shadowed<- fields do not

>ven or odd?
publi# stati# boolean is=dd *int !- 5 return *! ? ( 66 '-8 9

A. ". :. %. >.

)orks @ust fine )orks for negative ! only )orks for positive ! only Aails all the time B donCt #are

>ven or =dd?

Java defines ? asD *a E b- F b G *a ? b- 6 a o if aH+, bI+ then *a ? b- H +. i.e *3/ ? (- 6 3' and not 'J Ai!esD publi# stati# boolean is=dd *int !- 5 return *! ? ( J6 +-8 9 publi# stati# boolean is=dd *int !- 5 return *! K ' J6 +-8 9

:an Java do simple maths?

public static void main (String[] args) { System.out.println(12+2l); }

A. ". :. %. >.

&( &' 'L / till donCt #are

:an Java do simple maths?

public static main (String[] args) { System.out.println(12+2l); }

Meant to be *int-'( G *long-(. The problem is that Java doesnCt enfor#e the use of #apital ;N <, whi#h #auses #onfusion in #ertain fontsJ MoralD Always use ;N <

:an Java do simple maths (?


publi# #lass :anJava%oMaths( 5 publi# stati# void main* tringOP args5 long prod 6 '++++++F,+++8 ystem.out.println*prod-8 9
A. ". :. %. >. A.

,+++++++++ ,+++ (&,.++L& /+,+&(/+L omething else eriously, B donCt #are

:an Java do simple maths (?


publi# #lass :anJava%oMaths( 5 5 publi# stati# void main* tringOP argslong prod 6 '++++++F,+++8 ystem.out.println*prod-8 9 9 ame asD int ! 6 '++++++8 int y 6 ,+++8 int !y 6 !Fy8 EE This overflowsJ long prod 6 !y8

:an Java do simple maths (?


publi# #lass :anJava%oMaths( 5 5 publi# stati# void main* tringOP argslong prod 6 '++++++NF,+++8 ystem.out.println*prod-8 9 9

:an Java do simple maths &?


publi# #lass :anJava%oMaths& 5 publi# stati# void main* tringOP args- 5 double ! 6 (.+8 double y 6 '.'8 ystem.out.println* ! 3 y -8 9 9
A. ". :. %.

+ +.2 omething else Are you not hearing me? BCm not interested

:an Java do simple maths &?

The problem here is that powers of ten *whi#h we use so often for #urren#y et#- #anCt be represented e!a#tly using binary point

'.' 6 ''!'+Q*3'- Ode#imalP ''d 6 '+''b )e want e s.t. '+Q*3'- 6 (Q*elog*'+Q*3'-- 6 e log*(e 6 3'E*log (-, but *log (- is irrationalJJ

Bn this #ase, '.' gets represented as the nearest double

Then we subtra#t The answer is rounded to the nearest double, whi#h happens to be the ugly thing youCve @ust seen.

MoralD Rse integers where you #anJJJ

:an Java do simple maths L?


publi# #lass :anJava%oMathsL 5 publi# stati# void main* tringOP args- 5 int ! 6 '+ G +'+8 ystem.out.println*!-8 9 9

A. ". :. %. >.

(+ '0 '' omething else. Hmmm.. B wonder how rude B #an be on the feedba#k form?

:an Java do simple maths L?


publi# #lass :anJava%oMathsL 5 publi# stati# void main* tringOP args- 5 int ! 6 '+ G +'+8 ystem.out.println*!-8 9 9
Bf you prefi! an integer with a zero, Java interprets it as being in o#tal *base30- rather than in de#imalJ +'+o 6 0d

:an Java do simple maths ,?


publi# #lass :anJava%oMaths, 5 publi# stati# void main* tringOP args- 5 double ! 6 '.+ E (N8 ystem.out.println*!-8 9 9
+., +.+ '.+ omething else. )here did B put that #opy of Sarsity?

A. ". :. %. >.

:an Java do simple maths ,?


publi# #lass :anJava%oMaths, 5 publi# stati# void main* tringOP args- 5 double ! 6 '.+ E (N8 ystem.out.println*!-8 9 9 Just testing T thereCs nothing une!pe#ted going on hereJ

JavaCs $one Noopy


for *long i 6 Nong.MA1USANR>3,8 iH6Nong.MA1USANR>8 iGG- 5 ystem.out.println*;Hello<-8 9
A. ". :. %. >. A.

L! ,! .! '++! Vever stops At least this is the last of these silly le#tures

Noopy

for *long i 6 Nong.MA1USANR>3,8 iH6Nong.MA1USANR>8 iGG- 5 ystem.out.println*;Hello<-8 Always true. hould have used 9 H and not H6

Mad Modulo
publi# #lass MadModulo 5 publi# stati# void main* tringOP args- 5 int ! 6 '' ? (F,8 ystem.out.println*!-8 9 9
A. ". :. %. >. A.

' + '+ , omething else Bs it lun#h time yet?

Mad Modulo
publi# #lass MadModulo 5 publi# stati# void main* tringOP args- 5 int ! 6 '' ? (F,8 ystem.out.println*!-8 9 9
=perator pre#eden#e is the same for ? and F o Java does *'' ? (-F, 6, )e should have been e!pli#itD ''?*(F,-8

:ast3igation

**int-*#har-*byte- 3'-

A. ". :. %. >. A.

+ 3' (,, .,,&, omething else WWWzzz...

:ast3igation
igned *3'''''''''''''''''''''''''''''''''

**int-*#har-*byte- 3'igned *3''''''''' Rnsigned *.,,&,'''''''''''''''' igned *.,,&,++++++++++++++++''''''''''''''''

Rule: Sign extension is performed if the original value is signed. Otherwise zero extension.

:ast3igation 4art BB
tudent p 6 new tudent*-8 =b@e#t o 6 *=b@e#t-p8 ausage b 6 * ausage-o8

A. ". :. %. >.

)onCt #ompile $ives :lass:ast>!#eption Xuns fine omething else Aaaarrrggghhh...

:ast3igation 4art BBB


tudent p 6 null8 =b@e#t o 6 *=b@e#t-8 ausage b 6 * ausage-o8

A. ". :. %. >.

)onCt #ompile $ives :lass:ast>!#eption Xuns fine omething else Aaarrgggghhh...

:ast3igation 4art BBB


tudent p 6 null8 =b@e#t o 6 *=b@e#t-8 ausage b 6 * ausage-o8

Bt turns out that Java lets us #ast null to anything we like without throwing an errorJJJ

TryHarder
publi# #lass TryHarder 5 publi# stati# boolean test*- 5 try 5 return true8 9 finally 5 return false8 9 9

A. ". :. %.

True Aalse L( )ould he noti#e if B snu#k out?

publi# stati# void main* tringOP args- 5 ystem.out.println*test*--8 9 9

TryHarder
publi# #lass TryHarder 5 publi# stati# boolean test*- 5 try 5 return true8 9 finally 5 return false8 9 9

finally will always run when the fun#tion surrenders #ontrol so the ;return true< is overridden

publi# stati# void main* tringOP args- 5 ystem.out.println*test*--8 9 9

4lus4lus4ain
int @6+8 for *int i6+8 iH'++8 iGG- @6@GG8 ystem.out.println*@-8
A. ". :. %. >. A.

'++ 22 + ' omething else Arrggghhh.. Net me out of hereJ

4lus4lus4ain
int @6+8 for *int i6+8 iH'++8 iGG- @6@GG8 ystem.out.println*@-8
This is a postfix operator. That means this is the same asD int @( 6 @8 @ 6 @ G '8 @ 6 @(8 NessonD %onCt assign the same variable more than on#e per line

4lus4lus4ain
int @6+8 int i 6 *@GG- G *@GG-8 ystem.out.println*iG7 7G@-8
+' '( +( ++ omething else This is the last le#ture, right?

A. ". :. %. >. A.

trung out
publi# #lass trung=ut 5 publi# stati# void main* tringOP args- 5 ystem.out.print*;X<G;(<-8 ystem.out.print*Y%CGY(C-8 9 9
X(%( X( omething else whi#h B #ould figure out if B wanted to Vo idea, but youCre probably trying to tri#k us ! head"#utting ta#le repeatedly $

A. ". :. %. .

trung out
YAC is a #har *an unsigned '.3bit number;A< is a string #har is treated as a number for the G operator. o when two #hars are added, we get a numeri#al result.

hift hame
publi# #lass hift hame 5 publi# stati# void main * tringOP args- 5 long ! 6 ' HH &(8 ystem.out.println*!-8 9
+ (,, ' omething else B #ould be home in bed right now

A. ". :. %. >.

hift hame
publi# #lass hift hame 5 publi# stati# void main * tringOP args- 5 long ! 6 ' HH &(8 ystem.out.println*!-8 9
NH treated as an int "ut isnCt *'HH&(- all zeroes, so the answer should have been +? The @ava shift operator performs shifts by modulo3&( *int- or modulo3.L *long- amounts. *'HH&(- is therefore *'HH+-

:lassy:onundrum
publi# #lass :lassy:onundrum 5 publi# tring m tring 6 7: is fun78 publi# void :lassy:onundrum*- 5 m tring 6 7: is dull78 9 publi# stati# void main* tringOP args- 5 :lassy:onundrum ## 6 new :lassy:onundrum*-8 ystem.out.println*##.m tring-8 9
A. ". :. %.

: is fun : is dull omething else urely there #anCt be any more... #an there?

:lassy:onundrum
publi# #lass :lassy:onundrum 5 publi# tring m tring 6 7: is fun78 publi# void :lassy:onundrum*- 5 m tring 6 7: is dull78 9 publi# stati# void main* tringOP args- 5 :lassy:onundrum ## 6 new :lassy:onundrum*-8 ystem.out.println*#.m tring-8 9 9 MoralD donCt name your methods after your #lass *unless they are #onstru#torsJ-

>nough AlreadyJ

:ourse Xeview
Co puter Basics Sirtual Ma#hines :ompilation 4ointers Xeferen#es ''P (unda entals Modularity >n#apsulation Bnheritan#e 4olymorphism RMN :lass %iagrams Abstra#t :lasses Bnterfa#es OJavaP :onstru#tors %estru#tors tati# %ata and Methods

*esi"n Patterns %e#orator tate trategy :omposite ingleton 4ro!y =bserver Abstra#t fa#tory

)ava :loning =b@e#ts :omparing =b@e#ts 4a#kages :olle#tions $eneri#s

:ourse Aeedba#k

Zou will get an email re[uest to fill in a feedba#k [uestionnaire on this #ourse. 4N>A > fill it in

Thank you for your attention (or for not snoring at least)

You might also like