{"id":511,"date":"2009-02-17T11:46:08","date_gmt":"2009-02-17T16:46:08","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=511"},"modified":"2009-02-17T11:46:08","modified_gmt":"2009-02-17T16:46:08","slug":"java-note","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=511","title":{"rendered":"Java Note"},"content":{"rendered":"<p>\u00a7OO<\/p>\n<p>\u2022overriding methods can not have more restrictive access modifier<br \/>\n\u2022overriding methods can throw narrower or fewer exceptions<\/p>\n<p>\u2022overloaded methods can change return typs<br \/>\n\u2022overridding methods can not<\/p>\n<p>\u2022constructors<br \/>\n-this(): invoke constructor<br \/>\n-first line in constructor must be super() or this(), otherwise compiler will add them for you.<\/p>\n<p>\u00a7Garbagg collection<br \/>\n\u2022An object is eligible for garbage collection when no live thread can access it.<\/p>\n<p>\u2022Make object eligible for gc<br \/>\n-assign null to an object ref var<br \/>\n-assign object ref var to another obj<\/p>\n<p>\u2022Islands of isolation<\/p>\n<p>\u2022Request GC<br \/>\n-System.gc();<br \/>\n-Runtime rt = Runtime.getRuntim();<br \/>\nrt.gc();<\/p>\n<p>\u2022Java 2 Reference<br \/>\n-Strong ref: can not be gced<br \/>\n-Soft ref: gced when mem is low<br \/>\n-Weak ref: gced in next gc cycle<br \/>\n-Phantom ref: finalized but not gced<\/p>\n<p>\u00a7Inner Classes<\/p>\n<p>\u2022Instantiating inner class<br \/>\n-From within outer class<br \/>\nMyInner mi = new MyInner();<br \/>\n-From outside ths outer class<br \/>\nMyOuter.MyInner inner = new MyOuter().new MyInner();<\/p>\n<p>\u2022reference inner class instance within<br \/>\ninner class code<br \/>\n-this<br \/>\n\u2022reference outer this from within inner class code<br \/>\n-MyOuter.this<\/p>\n<p>\u2022Method-Local inner class<br \/>\n-can only be used below class def<br \/>\n-can not use local method var unless it&#8217;s final<\/p>\n<p>\u2022Anonymous Inner Class<br \/>\n-to override one or more methods of superclass<br \/>\n-ends with };<br \/>\n-new methods within anonymous inner class can&#8217;t be seen outside<\/p>\n<p>\u2022Argument-Defined Anonymous inner class<\/p>\n<p>\u2022static nested class<br \/>\nMyOuter.Nested n = new MyOuter.Nested();<\/p>\n<p>\u00a7Thread<\/p>\n<p>\u2022Two ways to create new threads<br \/>\n-extend Thread class<br \/>\n-implement Runnable<\/p>\n<p>\u2022Starting a thread<br \/>\nRunnable r = new MyRunnable();<br \/>\nThread t = new Thread(r);<br \/>\nt.start();<\/p>\n<p>\u2022Thread name<br \/>\nThread.currentThread().getName();<\/p>\n<p>\u2022dead thread can never be started again<\/p>\n<p>\u2022Thread states<br \/>\n-new<br \/>\n-runnable<br \/>\n-waiting\/blocked\/sleeping<br \/>\n-dead<\/p>\n<p>-sleep()<br \/>\n-yield()<br \/>\n-join()<br \/>\nThread t = new Thread();<br \/>\nt.start();<br \/>\nt.join();<br \/>\ncauses current thread to stop until thread t is dead<\/p>\n<p>\u2022synchronization<br \/>\n-only methods can be synchronized<br \/>\n-variables can not be synchronized<br \/>\n-each obj has one and only one lock<br \/>\n-if a thread goes to sleep, it takes locks with it<br \/>\n-a thread can acquire > 1 locks<br \/>\n-ok to have synchronized blocks and synchronize on diff objects<\/p>\n<p>\u2022Thread interaction<br \/>\n-wait(), notify(), notifyAll() must be called from within a synchronized context! A thread can&#8217;t invoke a wait or notify method on an object unless it owns that object&#8217;s lock<br \/>\n-use notifyAll()<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00a7OO \u2022overriding methods can not have more restrictive access modifier \u2022overriding methods can throw narrower or fewer exceptions \u2022overloaded methods can change return typs \u2022overridding methods can not \u2022constructors -this(): invoke constructor -first line in constructor must be super() or &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=511\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[14],"tags":[],"class_list":["post-511","post","type-post","status-publish","format-standard","hentry","category-java"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-8f","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=511"}],"version-history":[{"count":1,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/511\/revisions"}],"predecessor-version":[{"id":512,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/511\/revisions\/512"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}