What is JIT?
• 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
What are memory allocation available in Java?
• Class Memory
◦ memory block that stores
▪ class code
▪ variable code
▪ method code
▪ constructor code
◦ stores class-level data of every class
• Heap
◦ grows/shrinks dynamically as the program runs
◦ objects are created
◦ objects are stored
◦ allocates memory for class interfaces and arrays
◦ allocate memory to objects at runtime
• Stack
◦ memory allocated for each individual program
▪ local variables
▪ parameters
◦ fixed memory space
◦ each thread has a private JVM stack
◦ stores frames
◦ new frame created each time at every invocation of the method
◦ frame is destroyed when its method invocation completes
• Program counter register
◦ each JVM thread has a program counter register associated with it
◦ PC that stores the address of the available JVM instruction
• Native method stack memory
◦ C stacks
◦ not written in Java
◦ allocated for each thread when it’s created
◦ can be of fixed or dynamic nature
Order of specifiers
• there is no rule for specifiers in Java
• static public void main is ok
• public static void main is ok too
Default values in local variables
• 0
• null
◦ also for Strings because Strings are objects
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
Why is Java not completely object oriented?
• Still makes use of primitive data types
◦ boolean
◦ byte
◦ char
◦ int
◦ float
◦ double
◦ long
◦ short
◦ they need to be wrapped into objects of that class
Wrapper classes
• responsible for converting primitive data types into objects (reference types)
Define package in Java
• collective bundle of
◦ classes
◦ interfaces
◦ necessary libraries
◦ necessary JAR files
• using packages is good practice
• used to group related classes
• avoid name conflicts
• write better maintainable code
• two categories
◦ built-in packages
◦ user-defined packages
Implement pointers in Java
• not a possibility
◦ Java’s motto: keep programming simple
• JVM takes care of memory management – direct access discouraged
• they are unsafe
• increases complexity
Instance variable
• declared inside a class
• scope limited to specific object
Java String Pool
• collection of strings in Java String Pool
• Java’s heap memory
• if you try to create a new string object, JVM checks if such a string isn’t already there
◦ if yes, the same object reference is created with the variable
◦ else new object is created
• Strings immutable because Strings contain information like file system, networking connection,
database connection
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
Shallow vs deep copy
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
Why is Java dynamic?
• Designed to adapt to an evolving environment
• include large amount of run-time information that is used to resolve access to objects in real-
time
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)
Make class immutable
• mark as final
• mark fields as private
• don’t provide setters
• make mutable fields final
• initialize fields via deep copy
• getters: deep copy
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