Java Basics

•Objects & Variables
-Local var resides in stack -> space is allocated at method call and deallocated at method return

-Objects are created on heap by the “new” operator but object ref is a value and resides in stack
-Objects in heap exist if reachable from some variable on stack. Otherwise they are GCed

-Primitive types define values
-Nonprimitive types define objects

-Primitive var contain values in stack
-Nonprimitive var contain reference values in stack pointing to objects in heap

-object var assignment causes vars to share objects
-the “==” determines whether two vars contain same primitive value or same object ref value(points to same object in heap)
-prefer getClass in equals()

-All objects are either mutable (e.g. Arrays) or immutable(e.g. Strings)

•Type Safety
-Java is a strong typed language
-Java provides automatic storage management for all objects
-Java checks all array accesses to ensure they are within bounds

•Type Hierarchy
-Apparent type vs Actual type

•Conversion and Overloading
-most specific rule for overloading otherwise error

•Exceptions
Throwable
<- Error <-VirtualMachineError <-OutOfMemoryError <- Exception <- RuntimeException <- unchecked excptns <- checked exceptions -Exceptions are not just errors. They also fulfills defensive programming -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 •Use chained exceptions to -abstract out low level exceptions; -decouple client from impl •Load properties Properties p = new Properties(); p.load("app.properties"); String val = p.getProperty("name"); •Use bundle PropertyResourceBundle bundle = (PropertyResourceBundle) ResourceBundle.getBundle("r1"); -In war InputStream is = this.getClass() .getClassLoader() .getResourceAsStream("pkg/file"); or this.getClass().getResourceAsStream("pkg/file"); or ClassLoader.get... •Enable https support with URLclass System.setProperty("java.protocol.handler.pkqs","com.sun.net.ssl.internal.www.protocol"); java.security.Security.addProvider( new com.sun.net.ssl.internal.ssl.Provider()); •Class types Integer.TYPE.getName() -Arrays [Ljava.lang.String String array [C char array [Z boolen array [J long array

This entry was posted in java. Bookmark the permalink.