the life and death of objects sta2cs
play

The life and death of objects, sta2cs CSCI 136: - PowerPoint PPT Presentation

The life and death of objects, sta2cs CSCI 136: Fundamentals of Computer Science II Keith Vertanen Overview Where Java stores stuff The


  1. The ¡life ¡and ¡death ¡of ¡objects, ¡sta2cs ¡ CSCI ¡136: ¡Fundamentals ¡of ¡Computer ¡Science ¡II ¡ ¡• ¡ ¡Keith ¡Vertanen ¡

  2. Overview ¡ • Where ¡Java ¡stores ¡stuff ¡ – The ¡Heap ¡ – The ¡Stack ¡ – Breaking ¡both ¡ – Java ¡Garbage ¡Collector ¡(GC) ¡ • CreaAng ¡objects ¡ – Copy ¡constructors ¡ – Methods ¡creaAng ¡objects ¡ • StaAc ¡keyword ¡ – StaAc ¡methods ¡and ¡instance ¡variables ¡ 2 ¡

  3. Stack ¡ ¡vs. ¡Heap ¡ • Stack ¡ – Where ¡method ¡invocaAons ¡live ¡ – Where ¡local ¡variables ¡live ¡ – A ¡chunk ¡of ¡memory ¡ • Heap ¡ – Where ¡all ¡objects ¡live ¡ – Another ¡chunk ¡of ¡memory ¡ 3 ¡

  4. The ¡Stack ¡ • Methods ¡are ¡“stacked” ¡ – You ¡call ¡a ¡method ¡→ ¡put ¡on ¡top ¡of ¡a ¡call ¡stack ¡ – A ¡method ¡stays ¡on ¡stack ¡unAl ¡it ¡finishes ¡execuAng ¡ – Each ¡stack ¡frame ¡contains: ¡ • What ¡line ¡it ¡is ¡execuAng ¡ • Values ¡of ¡all ¡the ¡method’s ¡local ¡variables ¡ ¡ – This ¡includes ¡parameters ¡to ¡the ¡method ¡ 4 ¡

  5. public ¡void ¡ doStuff() ¡ { ¡ ¡ ¡ ¡ boolean ¡b ¡= ¡ true ; ¡ ¡ ¡ ¡go(4); ¡ } ¡ ¡ public ¡void ¡ go( int ¡x) ¡ { ¡ ¡ ¡ ¡ int ¡z ¡= ¡x ¡+ ¡24; ¡ ¡ ¡ ¡crazy(); ¡ } ¡ ¡ public ¡void ¡ crazy() ¡ { ¡ ¡ ¡ ¡ char ¡c ¡= ¡'a'; ¡ } ¡ 5 ¡

  6. What ¡if ¡we ¡have ¡variables ¡in ¡different ¡ public ¡void ¡ doStuff() ¡ { ¡ methods ¡that ¡have ¡the ¡same ¡name? ¡ ¡ ¡ ¡ boolean ¡b ¡= ¡ true ; ¡ ¡ ¡ ¡go(4); ¡ } ¡ ¡ 2 1 public ¡void ¡ go( int ¡b) ¡ { ¡ go() ¡ doStuff() ¡ ¡ ¡ ¡ int ¡c ¡= ¡b ¡+ ¡24; ¡ int ¡b ¡→ ¡4 ¡ boolean ¡b ¡→ ¡true ¡ ¡ ¡ ¡crazy(); ¡ int ¡c ¡→ ¡28 ¡ } ¡ ¡ doStuff() ¡ public ¡void ¡ crazy() ¡ 3 boolean ¡b ¡→ ¡true ¡ { ¡ ¡ ¡ ¡ char ¡c ¡= ¡'a'; ¡ crazy() ¡ } ¡ 4 char ¡c ¡→ ¡'a' ¡ go() ¡ go() ¡ int ¡b ¡→ ¡4 ¡ int ¡b ¡→ ¡4 ¡ int ¡c ¡→ ¡28 ¡ int ¡c ¡→ ¡28 ¡ doStuff() ¡ doStuff() ¡ boolean ¡b ¡→ ¡true ¡ boolean ¡b ¡→ ¡true ¡ 6 ¡

  7. CompuAng ¡a ¡factorial ¡recursively ¡ public ¡class ¡ Factorial ¡ { ¡ ¡ ¡ ¡public ¡static ¡long ¡ fact( long ¡ n) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ if ¡(n ¡<= ¡1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ return ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ return ¡n ¡* ¡ fact (n ¡-­‑ ¡1); ¡ fact(1) ¡ ¡ ¡ ¡} ¡ n ¡→ ¡1 ¡ ¡ ¡ ¡ ¡ ¡public ¡static ¡void ¡ main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ fact(2) ¡ ¡ ¡ ¡ ¡ ¡ ¡ long ¡result ¡= ¡ fact (4); ¡ n ¡→ ¡2 ¡ ¡ ¡ ¡ ¡ ¡ ¡System. out .println("4! ¡= ¡" ¡+ ¡result); ¡ ¡ ¡ ¡ ¡} ¡ fact(3) ¡ } ¡ n ¡→ ¡3 ¡ ¡ ¡ fact(4) ¡ 0! ¡= ¡1 ¡ n ¡→ ¡4 ¡ 1! ¡= ¡1 ¡ ¡ 2! ¡= ¡1 ¡x ¡2 ¡= ¡2 ¡ main() ¡ 3! ¡= ¡1 ¡x ¡2 ¡x ¡3 ¡= ¡6 ¡ ¡ 4! ¡= ¡4 ¡x ¡3 ¡x ¡2 ¡x ¡1 ¡= ¡24 ¡ Some ¡example ¡factorials. ¡ 7 ¡

  8. CompuAng ¡a ¡factorial ¡recursively ¡ public ¡class ¡ Factorial ¡ { ¡ ¡ ¡ ¡public ¡static ¡long ¡ fact( long ¡ n) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ if ¡(n ¡<= ¡1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ return ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ return ¡n ¡* ¡ fact (n ¡-­‑ ¡1); ¡ fact(1) ¡ ¡ ¡ ¡} ¡ n ¡→ ¡1 ¡ ¡ return ¡1 ¡ ¡ ¡ ¡public ¡static ¡void ¡ main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ fact(2) ¡ ¡ ¡ ¡ ¡ ¡ ¡ long ¡result ¡= ¡ fact (4); ¡ n ¡→ ¡2 ¡ ¡ ¡ ¡ ¡ ¡ ¡System. out .println("4! ¡= ¡" ¡+ ¡result); ¡ return ¡2 ¡* ¡fact(1) ¡ ¡ ¡ ¡} ¡ fact(3) ¡ } ¡ n ¡→ ¡3 ¡ ¡ return ¡3 ¡* ¡fact(2) ¡ fact(4) ¡ 0! ¡= ¡1 ¡ n ¡→ ¡4 ¡ 1! ¡= ¡1 ¡ return ¡4 ¡* ¡fact(3) ¡ 2! ¡= ¡1 ¡x ¡2 ¡= ¡2 ¡ main() ¡ 3! ¡= ¡1 ¡x ¡2 ¡x ¡3 ¡= ¡6 ¡ fact(4) ¡= ¡24 ¡ 4! ¡= ¡4 ¡x ¡3 ¡x ¡2 ¡x ¡1 ¡= ¡24 ¡ Some ¡example ¡factorials. ¡ 8 ¡

  9. Breaking ¡the ¡stack ¡ fact(-­‑2) ¡ n ¡→ ¡-­‑2 ¡ fact(-­‑1) ¡ public ¡class ¡ Factorial ¡ n ¡→ ¡-­‑1 ¡ { ¡ Exception ¡in ¡thread ¡"main" ¡ ¡ ¡ ¡public ¡static ¡long ¡ fact( long ¡ n) ¡ java.lang.StackOverflowError ¡ ¡ ¡ ¡{ ¡ fact(0) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ ¡ n ¡→ ¡0 ¡ at ¡Factorial.fact(Factorial.java:6) ¡ ¡ at ¡Factorial.fact(Factorial.java:6) ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡n ¡* ¡ fact (n ¡-­‑ ¡1); ¡ at ¡Factorial.fact(Factorial.java:6) ¡ fact(1) ¡ ¡ ¡ ¡} ¡ at ¡Factorial.fact(Factorial.java:6) ¡ ¡ n ¡→ ¡1 ¡ at ¡Factorial.fact(Factorial.java:6) ¡ ¡ ¡ ¡public ¡static ¡void ¡ main(String ¡[] ¡args) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ ¡ ¡ ¡{ ¡ at ¡Factorial.fact(Factorial.java:6) ¡ fact(2) ¡ ¡ ¡ ¡ ¡ ¡ ¡ long ¡result ¡= ¡ fact (4); ¡ at ¡Factorial.fact(Factorial.java:6) ¡ n ¡→ ¡2 ¡ ¡ ¡ ¡ ¡ ¡ ¡System. out .println("4! ¡= ¡" ¡+ ¡result); ¡ at ¡Factorial.fact(Factorial.java:6) ¡ ¡ ¡ ¡} ¡ at ¡Factorial.fact(Factorial.java:6) ¡ } ¡ fact(3) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ ¡ at ¡Factorial.fact(Factorial.java:6) ¡ n ¡→ ¡3 ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ fact(4) ¡ 0! ¡= ¡1 ¡ at ¡Factorial.fact(Factorial.java:6) ¡ n ¡→ ¡4 ¡ 1! ¡= ¡1 ¡ at ¡Factorial.fact(Factorial.java:6) ¡ at ¡Factorial.fact(Factorial.java:6) ¡ 2! ¡= ¡1 ¡x ¡2 ¡= ¡2 ¡ at ¡Factorial.fact(Factorial.java:6) ¡ 3! ¡= ¡1 ¡x ¡2 ¡x ¡3 ¡= ¡6 ¡ main() ¡ ... ¡ 4! ¡= ¡4 ¡x ¡3 ¡x ¡2 ¡x ¡1 ¡= ¡24 ¡ Some ¡example ¡factorials. ¡ 9 ¡

  10. Where ¡things ¡live ¡ • Local ¡variables ¡live ¡on ¡the ¡ stack ¡ – Local ¡variables ¡in ¡a ¡method ¡ – Parameters ¡passed ¡to ¡a ¡method ¡ – Reference ¡variables ¡ • e.g. ¡passing ¡in ¡a ¡ int ¡[] ¡array, ¡a ¡ Hero ¡object, ¡… ¡ • Only ¡the ¡remote ¡control ¡lives ¡on ¡the ¡stack ¡ • Objects ¡live ¡on ¡the ¡ heap ¡ – Objects ¡instanAated ¡with ¡ new , ¡live ¡on ¡the ¡heap ¡ – Instance ¡variables ¡of ¡an ¡object ¡ • Stored ¡as ¡part ¡of ¡the ¡object ¡living ¡on ¡the ¡heap ¡ 10 ¡

  11. The ¡miracle ¡of ¡object ¡creaAon ¡ • CreaAng ¡an ¡object ¡in ¡3 ¡easy ¡steps: ¡ 1) Declare ¡a ¡reference ¡variable ¡ Heap ¡ myDuck ¡ Duck ¡myDuck ¡ 11 ¡

  12. The ¡miracle ¡of ¡object ¡creaAon ¡ • CreaAng ¡an ¡object ¡in ¡3 ¡easy ¡steps: ¡ 1) Declare ¡a ¡reference ¡variable ¡ 2) Create ¡an ¡object ¡ Heap ¡ myDuck ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡new ¡Duck(); ¡ 12 ¡

  13. The ¡miracle ¡of ¡object ¡creaAon ¡ • CreaAng ¡an ¡object ¡in ¡3 ¡easy ¡steps: ¡ 1) Declare ¡a ¡reference ¡variable ¡ 2) Create ¡an ¡object ¡ 3) Link ¡the ¡object ¡and ¡the ¡reference ¡ You ¡have ¡now ¡consumed ¡memory ¡on ¡ Heap ¡ the ¡heap ¡based ¡on ¡how ¡much ¡state ¡a ¡ Duck ¡requires ¡(i.e. ¡the ¡number ¡and ¡ type ¡of ¡its ¡instance ¡variables). ¡ myDuck ¡ Duck ¡myDuck ¡= ¡new ¡Duck(); ¡ 13 ¡

  14. Java ¡Garbage ¡Collector ¡ • Java ¡tracks ¡# ¡of ¡variables ¡referring ¡to ¡an ¡object ¡ – As ¡soon ¡as ¡nobody ¡refers ¡to ¡an ¡object, ¡its ¡memory ¡ can ¡be ¡freed ¡ – Java's ¡automaAc ¡Garbage ¡Collector ¡(GC) ¡ periodically ¡handles ¡this ¡for ¡you ¡ • Exactly ¡when ¡is ¡up ¡to ¡the ¡JVM ¡ You're ¡so ¡going ¡to ¡ • Enjoy ¡it ¡(while ¡it ¡lasts) ¡ have ¡to ¡this ¡yourself ¡ in ¡data ¡structures. ¡ 14 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend