#
Review No. 10
This page contains Java concepts briefly explained.
#
Multiple Inheritance
In Java, a class can't extend multiple classes, can implement multiple interfaces.
Info
- If two interfaces have methods with the same signature (method name and parameters), the implementing class only needs to provide one implementation -> it is OK.
- If two interfaces provide a default method with the same signature, the implementing class must override the method to resolve the conflict.
#
Hashing alorithms for Java
SHA-512 (has hardware acceleration), BCrypt, SCrypt, Argon2
#
Protocols used with Java
- SMTP : for sending emails
- HTTP : for sending requests and receiving responses over the web
- WebSocket: for WebSocket communication
- TCP (Transport Layer in the OSI model) : Client-server applications (including database connection), file transfers (FTP), SSH
- UDP (User Datagram Protocol, Transport Layer): connectionless protocol (DNS, VoIP, video streaming)
- RMI (Remote Method Invocation) : Java-specific protocol that allows an object in one JVM (Java Virtual Machine) to invoke methods on an object in another JVM
- SFTP (Secure File Transfer Protocol) : Secure version of FTP that uses SSH for encryption
- SOAP (Simple Object Access Protocol) : for exchanging structured information in web services, often used in enterprise environments
- gRPC (Google Remote Procedure Call) : modern, high-performance RPC (Remote Procedure Call) protocol that uses HTTP/2 for transport and Protocol Buffers for serialization
- MQTT (Message Queuing Telemetry Transport) : lightweight messaging protocol for devices with limited bandwidth and resources, commonly used in IoT
- AMQP (Advanced Message Queuing Protocol) : open-standard application layer protocol for message-oriented middleware
- JMX (Java Management Extensions) is a Java technology that provides tools for managing and monitoring applications, system objects, devices
#
Calling a Non-Static Method from a Static Method
public class MyClass {
public void instanceMethod() {
System.out.println("Inside non-static (instance) method.");
}
public static void staticMethod() {
System.out.println("Inside static method.");
// Create an instance to call the non-static method
MyClass obj = new MyClass();
obj.instanceMethod();
}
public static void main(String[] args) {
staticMethod();
}
}
#
join() method in the Thread class
public class JoinExample {
public static void main(String[] args) {
// Creating a thread with a lambda expression
Thread thread1 = new Thread(() -> {
for (int i = 1; i <= 5; i++) {
System.out.println("Thread 1: " + i);
try {
Thread.sleep(500); // Simulate work
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread1.start();
try {
// Wait for thread1 to complete before continuing in main
thread1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread 1 has finished. Now main can continue.");
}
}
#
default method modifiers in Interface
public & abstract
Info
A method cannot be private and abstract in the same time.
#
final for Maps & Collections
final
for Maps & Collections makes the variable reference immutable,
but we can add/update the values inside the object.
#
Map.of & Map.ofEntries
Map.of
& Map.ofEntries
create immutable Maps (the content inside is immutable).
Map<String, Integer> myMap = Map.of(
"a", 1,
"b", 2,
"c", 3
);
Map<String, Integer> myLargeMap = Map.ofEntries(
Map.entry("a", 1),
Map.entry("b", 2),
Map.entry("c", 3),
Map.entry("d", 4),
Map.entry("e", 5)
);
Info
We can use for the same purpose Collections.unmodifiableMap() as well.
Info
We can make the Lists read-only using Collections.unmodifiableCollection() or List.of().
#
sticky sessions (affinity routing)
If you want all requests from the same client to go to the same JVM instance when you have multiple instances of the same service behind a gateway we need to:
- configure the gateway for this
- configure the client to send back to the gateway the cookie or the header information received from the gateway
Info
In general, a service mesh (like Istio) supports session affinity through destination rules, which can use cookies or headers as well.