{"id":570,"date":"2009-02-18T14:53:28","date_gmt":"2009-02-18T19:53:28","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=570"},"modified":"2009-02-21T23:48:10","modified_gmt":"2009-02-22T04:48:10","slug":"java-basics","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=570","title":{"rendered":"Java Basics"},"content":{"rendered":"<p>\u2022Objects &#038; Variables<br \/>\n-Local var resides in stack -> space is allocated at method call and deallocated at method return<\/p>\n<p>-Objects are created on heap by the &#8220;new&#8221; operator but object ref is a value and resides in stack<br \/>\n-Objects in heap exist if reachable from some variable on stack. Otherwise they are GCed<\/p>\n<p>-Primitive types define values<br \/>\n-Nonprimitive types define objects<\/p>\n<p>-Primitive var contain values in stack<br \/>\n-Nonprimitive var contain reference values in stack pointing to objects in heap<\/p>\n<p>-object var assignment causes vars to share objects<br \/>\n-the &#8220;==&#8221; determines whether two vars contain same primitive value or same object ref value(points to same object in heap)<br \/>\n-prefer getClass in equals()<\/p>\n<p>-All objects are either mutable (e.g. Arrays) or immutable(e.g. Strings)<\/p>\n<p>\u2022Type Safety<br \/>\n-Java is a strong typed language<br \/>\n-Java provides automatic storage management for all objects<br \/>\n-Java checks all array accesses to ensure they are within bounds<\/p>\n<p>\u2022Type Hierarchy<br \/>\n-Apparent type vs Actual type<\/p>\n<p>\u2022Conversion and Overloading<br \/>\n-most specific rule for overloading otherwise error<\/p>\n<p>\u2022Exceptions<br \/>\nThrowable<br \/>\n\t<- Error\n\t\t<-VirtualMachineError\n\t\t         <-OutOfMemoryError\n\t<- Exception\n\t\t<- RuntimeException\n\t\t\t<- unchecked excptns\n\t\t<- checked exceptions\n-Exceptions are not just errors. They also fulfills defensive programming\n\n-Use unchecked exception only if you expect that users will usually write code that ensures the exception will not happen because a)there is a convenient and inexpensive way to avoid exception b)the context of use is local\n\n\u2022Use chained exceptions to \n-abstract out low level exceptions;\n-decouple client from impl\n\n\u2022Load properties\nProperties p = new Properties();\np.load(\"app.properties\");\nString val = p.getProperty(\"name\");\n\n\u2022Use bundle\nPropertyResourceBundle bundle = (PropertyResourceBundle) ResourceBundle.getBundle(\"r1\");\n\n-In war\nInputStream is = \n this.getClass()\n .getClassLoader()\n .getResourceAsStream(\"pkg\/file\");\n\nor this.getClass().getResourceAsStream(\"pkg\/file\");\n\nor ClassLoader.get...\n\n\u2022Enable https support with URLclass\nSystem.setProperty(\"java.protocol.handler.pkqs\",\"com.sun.net.ssl.internal.www.protocol\");\njava.security.Security.addProvider( new com.sun.net.ssl.internal.ssl.Provider());\n\n\u2022Class types\nInteger.TYPE.getName()\n-Arrays\n[Ljava.lang.String\t\tString array\n[C\t\t\t\t\tchar array\n[Z\t\t\t\t\tboolen array\n[J\t\t\t\t\tlong array\n\n\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u2022Objects &#038; Variables -Local var resides in stack -> space is allocated at method call and deallocated at method return -Objects are created on heap by the &#8220;new&#8221; operator but object ref is a value and resides in stack -Objects &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=570\">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-570","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-9c","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/570","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=570"}],"version-history":[{"count":3,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/570\/revisions"}],"predecessor-version":[{"id":609,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/570\/revisions\/609"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}