- What's new in Java 8?
- What are the memory types in Java?
- What is java agent?
- Possible Performance Tools for Java?
- What is java profiler?
- What is stop the world?
- What is the difference between int, Integer and AtomicInteger?
- How i++ will work for Integer?
- What can you say about interface constants?
- What is the contract between equals and hashcode?
- What are the rules for overriding equals/hashcode methods?
- Are the same fields needed for equals/hashcode implementation?
- What are the purposes of inner classes?
- What is better interfaces or abstract classes?
- Do inner classes have access to private fields from outer class?
- What are the differences between static nested classes and non-static nested classes?
- What are the methods of Object class?
- What is Jmeter?
- Possible Ways to Capture Java Heap Dumps?
- What is the result of this code:
- Are checked exceptions bad?
- How var in Java 10 can be used?
- What is functional interface?
- Functional interfaces vs abstract classes?
- Why getters/setters?
- Serialization?
- Deserialization after changes in class? Possible issues?
- What is the difference between cohesion and coupling?
- @Override annotation?
- Java modifiers?
- Exception Hierarchy?
- One exception is thrown by catch block and another one is thrown from finally block, which exception will be thrown by method?
- Is it possible to serialize lambda expression?
- Why do Java Collections can not directly store Primitives types?
- How is recursion implemented in Java?
- Is it needed to document unchecked exceptions?
- Java Date classes?
- Why Do Local Variables Used in Lambdas Have to Be Final or Effectively Final?
- orElse() vs orElseGet() in Optional?
- How to filter list of objects using Stream API without .filter()?
- What is String Pool? How do Strings get there?
- What is the difference between StringBuilder and StringBuffer?
- Difference between <? super T> and <? extends T> ?
- How does JIT compiler work?
- Mutable vs Immutable object?
- Provide some examples when a finally block won't be executed in Java?
- What is type erasure?
- Lambda expressions, Method Reference , Optional, Streams added.
- Advanced Date , Time, Zone and Calendar Classes introduced.
- PermGen Removed.
- https://codete.com/blog/java-8-java-11-quick-guide/
- https://www.quora.com/What-are-the-differences-between-Java-8-9-10-and-11
- Stack. Stack memory is responsible for holding references to heap objects and for storing value types (also known in Java as primitive types), which hold the value itself rather than a reference to an object from the heap.
- Heap. This part of memory stores the actual object in memory. Those are referenced by the variables from the stack.
Java agents are able to “intrude” into the execution of Java applications running on the JVM at runtime by performing the direct modifications of the bytecode. Java agents are extremely as powerful as dangerous: they can do mostly everything however if something goes wrong, they can easily crash the JVM.
- https://www.javacodegeeks.com/2015/09/java-agents.html
- https://javabeat.net/introduction-to-java-agents/
- https://docs.oracle.com/javase/7/docs/api/java/lang/instrument/package-summary.html
- Java Profilers
- Tracing Java Web Requests and Transactions
- Java Application Performance Management (APM)
- Real User Monitoring (RUM)
- JVM Performance Metrics
- Web Server (Apache/Nginx) Access Logs
- Tracking All Java Exceptions
- Memory Analysis
A Java Profiler is a tool that monitors Java bytecode constructs and operations at the JVM level. These code constructs and operations include object creation, iterative executions (including recursive calls), method executions, thread executions, and garbage collections.
Stop-the-world will occur no matter which GC algorithm you choose. Stop-the-world means that the JVM is stopping the application from running to execute a GC. When stop-the-world occurs, every thread except for the threads needed for the GC will stop their tasks. The interrupted tasks will resume only after the GC task has completed. GC tuning often means reducing this stop-the-world time.
- https://www.cubrid.org/blog/understanding-java-garbage-collection
- https://stackoverflow.com/questions/16695874/why-does-the-jvm-full-gc-need-to-stop-the-world
- int is a primitive type. Variables of type int store the actual binary value for the integer you want to represent. int.parseInt("1") doesn't make sense because int is not a class and therefore doesn't have any methods.
- Integer is a class, no different from any other in the Java language. Variables of type Integer store references to Integer objects, just as with any other reference (object) type. Integer.parseInt("1") is a call to the static method parseInt from class Integer (note that this method actually returns an int and not an Integer). To be more specific, Integer is a class with a single field of type int. This class is used where you need an int to be treated like any other object, such as in generic types or situations where you need nullability.
- AtomicInteger is used in multithreaded environments when you need to make sure that only one thread can update an int variable. The advantage is that no external synchronization is requried since the operations which modify it's value are executed in a thread-safe way.
- https://stackoverflow.com/questions/8660691/what-is-the-difference-between-integer-and-int-in-java
- https://stackoverflow.com/questions/38846976/what-is-the-difference-between-atomic-integer-and-normal-immutable-integer-class
variable will be changed
The constant interface pattern is a poor use of interfaces. That a class uses some constants internally is an implementation detail. Implementing a constant interface causes this implementation detail to leak into the class's exported API. It is of no consequence to the users of a class that the class implements a constant interface. In fact, it may even confuse them. Worse, it represents a commitment: if in a future release the class is modified so that it no longer needs to use the constants, it still must implement the interface to ensure binary compatibility. If a nonfinal class implements a constant interface, all of its subclasses will have their namespaces polluted by the constants in the interface. There are several constant interfaces in the java platform libraries, such as java.io.ObjectStreamConstants. These interfaces should be regarded as anomalies and should not be emulated.
objects which are .equals() MUST have the same .hashCode().
- https://stackoverflow.com/questions/17027777/relationship-between-hashcode-and-equals-method-in-java
- Always use same attributes of an object to generate hashCode() and equals() both.
- equals() must be consistent (if the objects are not modified, then it must keep returning the same value).
- Whenever a.equals(b), then a.hashCode() must be same as b.hashCode().
- If you override one, then you should override the other.
The fields don't have to be the same. The requirement is for two objects that are equal, they must have the same hash code. If they have the same hash code, they don't have to be equal. For example, you could return 1 as your hash code always, and you would obey the hash code contract, no matter what fields you used in your equals method. Returning 1 all the time would improve the computation time of hashCode, but HashMap's performance would drop since it would have to resort to equals() more often.
- It is a way of logically grouping classes that are only used in one place.
- It increases encapsulation.
- It can lead to more readable and maintainable code.
- https://stackoverflow.com/questions/11398122/what-are-the-purposes-of-inner-classes
- https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Use abstract classes when you have a class that A is kind of B and interface when A can do B.
- A class can implement multiple interfaces, but it can only extend one abstract class.
- Interfaces allow the creation of proxies that encapsulate a concrete class. This is used extensively by frameworks in order to intercept method calls to the concrete class (e.g., for starting a transaction before the method is executed or to write to the log).
- https://stackoverflow.com/questions/2971265/situation-where-interface-is-better-than-abstract-class
- https://stackoverflow.com/questions/11889588/choosing-interface-or-abstract-class
Inner classes has access to private fields from outer class.
- Static nested classes do not directly have access to other members(non-static variables and methods) of the enclosing class because as it is static, it must access the non-static members of its enclosing class through an object. That is, it cannot refer to non-static members of its enclosing class directly. Because of this restriction, static nested classes are seldom used.
- Non-static nested classes (inner classes) has access to all members(static and non-static variables and methods, including private) of its outer class and may refer to them directly in the same way that other non-static members of the outer class do.
- https://www.geeksforgeeks.org/nested-classes-java/
- https://stackoverflow.com/questions/253492/static-nested-class-in-java-why
toString(), hashCode(), equals(Object obj), getClass(), finalize(), clone(), wait(), notify() notifyAll()
Apache JMeter is an open source, Java-based, load testing tool that can be used to analyze the functional behavior of a system and measure the performance of a system under a load test. A load test will simulate end-user behavior that approach the limits of an application’s specifications. Apache JMeter can be used to simulate varying or heavy loads on singular or multiple servers, networks or objects to test a system’s strength.
Possible options from the link.
public static void main(String[] args) {
Point pnt1 = new Point(0, 0);
Point pnt2 = new Point(0, 0);
System.out.println("X: " + pnt1.x + " Y: " + pnt1.y);
System.out.println("X: " + pnt2.x + " Y: " + pnt2.y);
System.out.println(" ");
tricky(pnt1, pnt2);
System.out.println("X: " + pnt1.x + " Y:" + pnt1.y);
System.out.println("X: " + pnt2.x + " Y: " + pnt2.y);
}
public void tricky(Point arg1, Point arg2) {
arg1.x = 100;
arg1.y = 100;
Point temp = arg1;
arg1 = arg2;
arg2 = temp;
}
If we execute this main() method, we see the following output:
X: 0 Y: 0
X: 0 Y: 0
X: 100 Y: 100
X: 0 Y: 0
The method successfully alters the value of pnt1, even though it is passed by value; however, a swap of pnt1 and pnt2 fails! This is the major source of confusion. In the main() method, pnt1 and pnt2 are nothing more than object references. When you pass pnt1 and pnt2 to the tricky() method, Java passes the references by value just like any other parameter. This means the references passed to the method are actually copies of the original references.
- https://www.javaworld.com/article/3142626/are-checked-exceptions-good-or-bad.html
- https://www.yegor256.com/2015/07/28/checked-vs-unchecked-exceptions.html
- https://medium.com/@eob/checked-exceptions-considered-evil-f7d07e051fa6
One exception is thrown by catch block and another one is thrown from finally block, which exception will be thrown by method?
It's a combination of two facts:
- Java primitive types are not reference types (e.g. an int is not an Object)
- Java does generics using type-erasure of reference types (e.g. a
List<?>
is really aList<Object>
at run-time)
public static void main(String[] args) {
final List<Person> list = Arrays.asList(Person.builder().setAge(25).build(), Person.builder().setAge(10).build(),
Person.builder().setAge(15).build(), Person.builder().setAge(21).build());
list.stream()
.flatMap(person -> person.getAge() < 18 ? Stream.of(person) : Stream.empty())
.forEach(person -> System.out.println(person.getAge()));
}
String pool is the special memory region where Strings are stored by the JVM.
All public methods of StringBuffer are synchronized, it provides Thread safety but on a performance cost.
- https://www.interviewcake.com/concept/java/mutable#:~:text=A%20mutable%20object%20can%20be,an%20immutable%20object%20can't.&text=That%20said%2C%20if%20you're,all%20fields%20final%20and%20private.&text=Strings%20are%20immutable%20in%20Java.
- https://www.edureka.co/blog/java-mutable-and-immutable-objects/
If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
Type erasure removes all type parameters and replaces them with their bounds or with Object if the type parameter is unbounded. This way, the bytecode after compilation contains only normal classes, interfaces and methods, ensuring that no new types are produced. Proper casting is applied as well to the Object type at compile time.
Java enum