Summary Day - 1

Things we covered

  1. Intention behind creating java was to create a language that can run anywhere and everywhere. That's why you see the oracle's message that java runs on 1B Devices So they were quite successful in doing that.

  2. Java is actually a language specification, and there are multiple implementations of it

    1. OpenJDK JVM => Hotspot
    2. Oracle JVM => RockIt
  3. Java(1995) is a much younger language than python(1991) then the natural question is why is python more feature rich and has more libraries than java. Now that's a quiz question for tomorrow

  4. Java is

    1. Simple -- Similar syntax as C, so its easier to learn for people coming from C
    2. Object-Oriented -- Since realworld has a lot of objects, thinking in terms of objects is easier so Java uses Object oriented convention [read-more](OOPS Concepts)
    3. Portable -- Since JDK compiles .java files to .class file and .class file is executable prettymuch anywhere a JVM can run it's portable
    4. Platform independent -- Unlike C/C++ where code has to be compiled seperately for each environment (OS+CPU Architecture) in Java we only compile once and the .class files will be executable anywhere
    5. Secured -- Java doesn't have explict pointers, each program runs inside JVM sandbox, classloader in java keeps local .java files and external libraries seperate adding an additional layer of security, Bytecode verifier checks if there's any invalid code or malicious code even before executing the code and secuirty manager monitors each class for things like disk read or write.
    6. Robust -- Has Strong memory management with a garbage collector, avoids security issues due to lack of pointers, has exception handling
    7. Architecture neutral -- In java datatypes are platform independent and occupy exactly the same space as defined in spec. In C programming, int data type occupies 2 bytes of memory for 32-bit architecture and 4 bytes of memory for 64-bit architecture. However, it occupies 4 bytes of memory for both 32 and 64-bit architectures in Java.
    8. Interpreted -- Javac creates .class file which is executed by JVM making the language interpreted
    9. High Performance -- Even though java is intrepreted which adds a additional layer of translation before the code is executed, java uses JIT compiler to optimize pieces of code that matters making it more performant
    10. Multithreaded -- Java is multithreaded
    11. Distributed -- Java allow to create distributed applications which can connect with eachother over a network to execute a task
    12. Dynamic -- Java is dynamic because classes are loaded on demand as and when they are needed.
  5. How Java works

    [.java file] -- Compiler (javac) -> [.class file] -- JVM -> Output
        
  6. How JVM works

    [.class files] -- [Class Loader] -> [Byte code verifier] -> [Intrepreter] -> [Runtime] -> Hardware -> Output
        
  7. JVM Architecture

    1. ClassLoader
      1. BootStrap class loader -- Loads rt.jar which contains all standard library classes
      2. Extension class loader -- is a child class of bootstrap class loader and loads classes in $JAVA_HOME/jre/lib/ext
      3. System class loader -- loads all files in classpath, which is set by passing -cp or -classpath argument to java command when executing a class
    2. *Class Area -- Class definations are stored here, this is used to create new classes.
    3. *Heap -- When an Object instance is created from a class by using new keyword it is stored in heap. Note that this heap has nothing to do with datastructure min/max heap even though it has the same name.
    4. *Stack -- Functions as they are executed their local variables are stored here, each thread gets its own stack. Note that this is actually a stack datastructure as i can keep adding new entries to stack as i execute a new function and pop it out as i am done with it. Recurse a lot and you get stackoverflow. Now you know why its called so :P
    5. PC Register -- Stores the memory Address / location of current instruction being executed in JVM, it is undefined in case of native method
    6. Native method stack -- Java supports running methods/functions written in other languages to be executed in java by using something known as JNI (java native interface) their local
  8. What's a Path Path is a variable that helps OS find where an executable is. This helps us run the executable in terminal, without having to remember the whole file path for that executable.

  9. Type of variables

    1. Local -- Scoped local to the function being executed. aka only the function being executed can access it.
    2. Instance -- Scoped at an instance level, aka only the specific instance can access it.
    3. Static -- Scoped at a class level, aka doesn't need an object instance to be executed and all instances of the class can use it.
  10. Data types

    1. Primitive
      1. boolean -- 1 byte
      2. byte -- 1 bytes
      3. char -- 2 bytes -- java uses 2 bytes in char because it uses unicode system not ASCII.
      4. short -- 2 bytes
      5. int -- 4 bytes - integer literals are int by default
      6. long -- 8 bytes
      7. float -- 4 bytes
      8. double -- 8 bytes -- decimal literals are double by default
    2. Non Primitive -- Any Object instance / array is a non primitive type
  11. Operators

    1. Operator Type Category Precedence
      Unary postfix expr++ expr--
      prefix ++expr --expr +expr -expr ~ !
      Arithmetic multiplicative * / %
      additive + -
      Shift shift << >> >>>
      Relational comparison < > <= >= instanceof
      equality == !=
      Bitwise bitwise AND &
      bitwise exclusive OR ^
      bitwise inclusive OR |
      Logical logical AND &&
      logical OR ||
      Ternary ternary ? :
      Assignment assignment = += -= *= /= %= &= ^=
  12. Important Keywords when coming from C

    1. class -- Java class keyword is used to declare a class.
    2. implements -- Java implements keyword is used to implement an interface.
    3. instanceof -- Java instanceof keyword is used to test whether the object is an instance of the specified class or implements an interface.
    4. interface -- Java interface keyword is used to declare an interface. It can have only abstract methods.
    5. native -- Java native keyword is used to specify that a method is implemented in native code using JNI (Java Native Interface).
    6. new -- Java new keyword is used to create new objects.
    7. null -- Java null keyword is used to indicate that a reference does not refer to anything. It removes the garbage value.
    8. package -- Java package keyword is used to declare a Java package that includes the classes.
    9. strictfp -- Java strictfp is used to restrict the floating-point calculations to ensure portability.
    10. synchronized -- Java synchronized keyword is used to specify the critical sections or methods in multithreaded code.
    11. this -- Java this keyword can be used to refer the current object in a method or constructor.
    12. throw -- The Java throw keyword is used to explicitly throw an exception. The throw keyword is mainly used to throw custom exceptions. It is followed by an instance.
    13. throws -- The Java throws keyword is used to declare an exception. Checked exceptions can be propagated with throws.
    14. try -- Java try keyword is used to start a block of code that will be tested for exceptions. The try block must be followed by either catch or finally block.
    15. catch -- Java catch keyword is used to catch the exceptions generated by try statements. It must be used after the try block only.
    16. finally -- Java finally keyword indicates a block of code in a try-catch structure. This block is always executed whether an exception is handled or not.
    17. super -- Java super keyword is a reference variable that is used to refer to parent class objects. It can be used to invoke the immediate parent class method.

Things i got wrong / Mis understood

4.11 Distributed -- Java allow to create distributed applications which can connect with eachother over a network to execute a task

9.1.3 char -- 2 bytes -- java uses 2 bytes in char because it uses unicode system not ASCII. | If you need a primer on ascii vs unicode ask.

7.1 ClassLoader

  1. BootStrap class loader -- Loads rt.jar which contains all standard library classes
  2. Extension class loader -- is a child class of bootstrap class loader and loads classes in $JAVA_HOME/jre/lib/ext
  3. System class loader -- loads all files in classpath, which is set by passing -cp or -classpath argument to java command when executing a class

Things We missed

  1. OOPS Concepts
    1. Object -- Object is an entity that has a state and a behavior. state means it holds some data, and behavior means its capable of performing an action
    2. Class -- Class is the blueprint for creating an object, objects of the same class will hold similar states and will be capable of performing similar actions.
    3. Inheritance -- A class can inherit state and behaviour from a parent class and extend/modify it. read-SOLID
    4. Polymorphism -- A child class can decide to do things different way than a parent class, so we can have similar objects that decide to do an action in different ways. eg: Let's say Cat and Dog class inherit Animal class. Each of them can override Animal Class's String talk() method to return meow or Woof as long as they take no input and return string.
    5. Abstraction -- We can decide to expose only methods & instance/class variables we choose to expose using access modifiers using public/private/protected keyword. Everything else can stay hidden.
    6. Encapsulation -- In Java we can encapsulate data and code into a single unit and we can expose getter/setter methods to provide access to class attributes we choose to expose.
  2. Access Modifiers
    Modifier Class Package Subclass World
    public Y Y Y Y
    protected Y Y Y N
    no modifier Y Y N N
    private Y N N N