Skip to content

Instantly share code, notes, and snippets.

@akanshmurthy
Last active November 27, 2017 21:15
Show Gist options
  • Select an option

  • Save akanshmurthy/baaaeb2d1bb343a2a080bd8bbac816a4 to your computer and use it in GitHub Desktop.

Select an option

Save akanshmurthy/baaaeb2d1bb343a2a080bd8bbac816a4 to your computer and use it in GitHub Desktop.
Java
Java highlights from Head First Java
- public static main; at least one per app
- source code -> compile -> byte code -> run on platform independent JVMs
- bool and int are not compatible -> no int x = 1; if (x)
- class = blueprint for object; object oriented = inheritance with superclass -> DRY
- main only really used to test or start the Java app
- every time an object is created in Java, it is stored in memory called the heap (a garbage-collectible heap)
-> JVM does a lot of the magic to manage the heap
- jar = when you have lots of classes
- primitive var actually contains the value/bits representing the value
- var with object just contains reference to the object in the heap
- make instance vars private and getters/setters public to encapsulate data
- instance vars get initialized but local vars don’t!
- == for object ref to same object; equals for if two different objects are equal
- power of Java API; Array vs ArrayList
- IS A = extends; HAS A = instance variable
- subclass inherits all public instances of parent class
- polymorphic objects (such as arrays), arguments and return types allow more flexibility
- final, non-public, all private constructions prevent subclassing
- subclassing methods have to use the same args/return types as superclass and access can’t be less accessible
- overloading: return types/access can be different but the change has to be more than just the return type ->
allows convenience for callers/consumers
- abstract classes can’t be instantiated; abstract methods, which are empty, need to be implemented/overridden
by the first concrete class
- Deadly Diamond of Death is prevented in Java since you can’t have multiple inheritance ->
interface can be used instead… “implements” keyword
- interface methods are abstract and end in semicolon since they have no body; interface like
100% abstract class with only abstract methods
- stacks = local variables
- heaps = instance vars/objects
- if you don’t write constructor, compiler writes one for you
- overloaded constructors MUST have different args
- all the constructors in an object’s inheritance tree must run when you make a new object;
compiler always call super in constructor
- static vars can change and are accessible by all instances; static final can’t
- try, catch, finally (finally will always run even if return statements are present in try or catch
- multiple exceptions of the same superclass need to be caught from smaller to larger
- you can duck an exception by adding a throws to the method definition
- swing = Java GUI toolkit
- write serialized objects to disk to save state (implement Serializable); all or nothing saving
- transient keyword doesn’t serialize/save instance variable
- buffers help while writing files
- to make a socket connection, you need an IP address and TCP port number
- each thread gets its own call stack
- threads make it appear like multiple things are happening at the same time but the
JVM is really just switching between call stacks really quickly
- thread scheduler = black box; use Thread.sleep to make the scheduler more predictable
- job/worker = Runnable/Thread -> run() of Runnable is the first method on the new thread’s call stack
- use Thread.sleep to make the scheduler more predictable
- synchronized = only one thread can access the lock with the key (but it has a performance hit and could lead to deadlock)
- tree sets/comparables for sorting; need to override equals and hashCode
- manifest file + JAR is one way to deploy an application; Java Web Start for a hybrid local/remote option;
RMI allows two different JVMs to talk to each other; servlets = pure web Java programs with HTTP server (
JSP = Java server pages with Java in the HTML); J2EE/EJB for heavy duty enterprise apps;
Jini - adaptive discovery, self-healing, etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment