Java Notes
Java Notes
• Just-in-time compilation
• dynamic translation
• run-time compilation
• kompilacja do kodu maszynowego bezpośrednio przed wykonaniem danego fragmentu kodu
◦ may consist of source code translation, but is more commonly bytecode translation to
machine code, which is then executed directly
• cały proces:
◦ kod źródłowy kompilowany do kodu pośredniego
◦ maszyna wirtualna kompiluje do z kodu pośredniego do kodu maszynowego
What is JDK?
• Java Development Kit
• combined package of
◦ JRE (Java Runtime Environment)
◦ Developer tools
JDBC
• Java Database Connector
What is a ClassLoader?
• Responsible for loading Java classes dynamically to the JVM (Java Virtual Machine) during
runtime
• part of JRE (Java Runtime Environment)
• JVM doesn’t need to know about the underlying file systems in order to run Java
• Java classes are not loaded into the memory all at once, rather when they are needed
◦ ClassLoaders are responsible for loading classes into memory if needed
Order of specifiers
• there is no rule for specifiers in Java
• static public void main is ok
• public static void main is ok too
Constructors
• copy constructor
◦ constructor that initializes an object through another object of the same class
◦ deep copy
• can a constructor return a value?
◦ Yes, it implicitly returns an instance of the class
◦ a constructor can’t return a value explicitly
• impossible for a subclass to inherit the constructor
Marker interface
• empty interface
• tagging interface
• run-time type information about objects
• JVM has additional information about the objects
• newer development favours annotations
Object Cloning
• clone method
Wrapper classes
• responsible for converting primitive data types into objects (reference types)
Instance variable
• declared inside a class
• scope limited to specific object
Java Exceptions
• checked exceptions
◦ exceptions that Java compiler requires us to handle
▪ throw it up the stack
▪ handle the exception
• unchecked exceptions
◦ f. ex. RuntimeException
Keywords
• final
◦ variables
▪ creates a constant variable
▪ always capitalize final variables
◦ methods
▪ can’t be overridden by children classes
◦ classes
▪ can’t have children (subclasses)
• super
◦ mostly used in constructors
• this
◦ two variables of the same name (local and instance variables)
▪ getter
▪ constructor
Static methods
• can be called from object (not necessarily from class)
• overriding static methods: CALLED METHOD HIDING
• static methods resolves at compile time
Polymorphism
• method overloading
◦ compile-time polymorphism
◦ process of creating multiple method signatures using one method name
▪ varying in the number of arguments
▪ varying in the return-type method
• Java still doesn’t know which method to call!
▪ varying in the type of arguments
◦ overloading static methods
▪ nope
▪ error “static method cannot be referenced”
• run-time polymorphism
◦ has to do with inheritance
◦ method overriding
◦ if the child provides the specific implementation of the method that has been provided by
one of its parent classes
◦ the proper method call determined at runtime, not compile time
◦ also called “late binding”
▪ binding
• process of unifying the method call with the method’s code segment
▪ late binding
• method’s code segment unknown till the method is called during the run-time
Access specifiers
• public
• protected
• private
• default
◦ package-private
Thread
• life cycle
◦ Newborn
◦ Runnable (Ready)
◦ Running
◦ Blocked
◦ Dead
• daemon thread
◦ thread with least priority
◦ run in the background
Enumeration
equals vs ==
• ==
◦ operator
◦ reference comparison
◦ checks if two entities are at the same address (point to the same memory location)
• equals
◦ method
◦ comparing content inside of that memory address
Generics in Java
• catch unnecessary invalid types at compile time
• enable types to be parameters (classes and interfaces)
• pass in types as if they were parameters
• Java compiler issues errors if the code violates type safety
Externalizable interface
• serves the purpose of custom Serialization
• readExternal
• writeExternal
• difference from Serializable interface
◦ externalizable
▪ explicit mention what fields and variables to serialize
◦ serializable
▪ no methods (marker interface)
Finalize method
• called only once per each object
• usually used for garbage collection
• not always called (in case of crashes, System.exit())
Garbage collector
• main objective is to free up memory space that’s not being used any more
• memory resources used efficiently
• daemon
• how does it work
◦ keeps track of every object that is created
◦ keeps track of how many variables are pointing to this object
Passing arguments
• Java is always passed by value
• primitive types: no worries
• reference types: technically new objects are created, but they are still pointing the same
underlying address in memory! (shallow copy: that’s why it seems like passing by reference)
Static block
• gets executed at the time of class loading
• gets executed before the main method runs
◦ possible to execute code before main!
Comparator vs Comparable
• Comparable
◦ interface
▪ requires the compareTo method
• Comparator
◦ implement a new Comparator with compare method
Object class
• parent of all objects in Java
• methods
◦ clone
◦ equals
▪ compares pointers
◦ finalize
▪ called when garbage collection determines that there are no more references to the
object
◦ getClass
▪ runtime class of an object
◦ hashCode
◦ toString
◦ notify
◦ notifyAll
◦ wait
▪ wait()
▪ wait(long timeout)
▪ wait(long timeout, int nanos)
Tricky questions
• 100 + 100 + “dupa” = “200dupa”
◦ znaki przetwarzane od lewej do prawej
◦ pierwszy łączy dwa inty, drugi int oraz stringa
• “dupa” + 100 + 100 = “dupa100100”
◦ wszędzie plus oznacza łączenie dwóch stringów
• what if main isn’t declared as static?
◦ RuntimeError “NoSuchMethodError”
• difference between “>>” and “>>>”
◦ “>>” preserves the sign
◦ “>>>”
▪ avoids overflow
▪ needed as Java doesn’t support unsigned int