Prev Next

Java / JDK, JRE, JVM, JIT

1. Architectural-neutral. 2. What is High level language? 3. Is Java a WORA? 4. Features of Java. (or) Java buzzwords. 5. Java is Simple. 6. Difference between JDK, JRE and JVM. 7. Java is Platform independent. 8. Interpreted. 9. Three flavors of Java. 10. What is Java? 11. Java is object oriented. 12. How many types of memory areas are allocated by JVM? 13. What is the latest version of Java? 14. What is Write Once, Run Anywhere (WORA)? 15. What is a Platform? 16. Is Java a pure/fully object oriented language? 17. Secured. 18. What is bytecode? 19. Portable. 20. Robust. 21. Dynamic. 22. High-performance. 23. Multithreaded. 24. Distributed. 25. What is classloader? 26. Types of classloader. 27. What is Heap space in Java? 28. Default heap size in Java. 29. What is Java Memory Model? 30. What are the new features introduced in Java 8? 31. Difference between EAR, JAR and WAR file in J2EE. 32. Explain memory leak in Java. 33. How Garbage collection works in Java? 34. What are the GC roots in Java? 35. Types of memory leaks in Java. 36. How do you diagonise memory leaks in Java? 37. Difference between Stack and Heap memory in Java. 38. Does Java garbage collector cleans both heap and stack memory? 39. Why garbage collection is required in Java? 40. How GC does not affect Java program's performance? 41. Different categories of GC algorithm. 42. List the available GC algorithms in Java. 43. What is the default GC collector in Java 8? 44. What are types of reference in Java? 45. Explain strong reference type in Java. 46. Explain weak reference type in Java. 47. Explain Serial Garbage Collector in Java. 48. Explain Parallel Garbage Collector in Java. 49. Explain CMS Garbage Collector in Java. 50. Explain G1 Garbage Collector in Java. 51. What is soft reference in Java? 52. What is phantom reference in Java? 53. Explain the javap command in Java. 54. What is PermGen in Java? 55. What is metaspace in JDK 1.8? 56. Explain the usage of MetaspaceSize parameter in Java 8. 57. Difference between permgen and metaspace in Java. 58. New features introduced in Java 1.7. 59. How do I load Jars dynamically at runtime? 60. What is synthetic construct in Java? 61. New changes introduced in Java 9. 62. What is jshell tool in Java 9? 63. Difference between Java.exe and Javaw.exe. 64. What is Nashorn in Java8? 65. How do I debug to see when class is loaded in jvm? 66. Explain Class loading subsystem of JVM.
Could not find what you were looking for? send us the question and we would be happy to answer your question.

Architectural-neutral.

Java programs are compiled to generate byte-code which is .class file. This byte-code files are interpreted by the architecture specific JVM to execute that makes java programs run in any type of hardware.

What is High level language?

High level languages(HLL) are languages that has less complexity and easy to understand, as the syntax are close to human language and it abstracts the machine language.

Is Java a WORA?

Yes.

Features of Java. (or) Java buzzwords.

There are 12 buzzwords.

  1. Simple.
  2. Object-Oriented.
  3. Platform independent.
  4. Secured.
  5. Robust.
  6. Architecture neutral.
  7. Portable.
  8. Dynamic.
  9. Interpreted.
  10. High Performance.
  11. Multithreaded.
  12. Distributed.
Java is Simple.

Java is an easy to understand and simple to learn.

Difference between JDK, JRE and JVM.

JVM

     JVM is Java Virtual Machine, it provides the runtime environment in which java bytecode can be executed. JVMs are available for many hardware and software platforms (so JVM is platform dependent).

JRE

     JRE is Java Runtime Environment. It is the implementation of JVM.

JRE doesn't have Javac compiler.

JDK

   JDK is Java Development Kit. It exists physically and contains JRE and development tools.

Java is Platform independent.

JAVA supports WORA which make it run across different platform/operating system.

Interpreted.

The Java compiler generates byte-codes, rather than native machine code. To run a Java program, you use the Java interpreter to execute the compiled byte-codes. Java byte-codes provide an architecture-neutral object file format. The code is designed to transport programs efficiently to multiple platforms.

Three flavors of Java.

ME(Micro Edition) for mobile,
SE(Standard Edition) for desktops,
and EE(Enterprise edition) for enterprise.

What is Java?

Java is a high-level programming language and object oriented. It was developed by Sun Micro systems, initially released in 1995.

Java is object oriented.

Java is object based as we cannot create any program without creating a class.

How many types of memory areas are allocated by JVM?

Many types:

Class(Method) Area,
Heap,
Stack,
Program Counter Register,
Native Method Stack.

What is the latest version of Java?

Java 10 released on March 20, 2018.

What is Write Once, Run Anywhere (WORA)?

WORA refers to the ability of a computer program to run across any operation system or platform.

What is a Platform?

Platform refers to any hardware/software environment under which a program runs.

Is Java a pure/fully object oriented language?

Java has primitive data types which are not objects, so Java is considered as not fully object oriented language. However Java could be considered as fully object oriented language as it has wrapper classes for its primitive types.

Secured.

Java is secured for developing application due to many of security features that includes, Byte-code verification before execution, Run time security checks, Immutable classes (String for e.g.)

What is bytecode?

Bytecode is a highly optimized set of instructions, low level language and non-executable code. The platform specific JVM interprets bytecode to run the program.

Portable.

Being platform independent and architecture neutral, Java application developed in one enviroment can be deployed to different platform and executed using compliant JVM. Java application developed for Desktops(SE) are fully portal where Java EE has EJB which makes it less portal.

Robust.
  • highly supported language and portable,
  • Strongly typed language,
  • Memory Management,
  • Garbage Collection,
  • Exception Handling,
  • No pointers.
Dynamic.

Java was designed to adapt to an evolving/changing environment. Even after binaries have been released, they can adapt to a changing environment. Java loads in classes as they are needed, even from across the network It defers many decisions (like object layout) to runtime, which solves many of the version problems that C++ has.

High-performance.

Although Java is an interpreted language, it is faster than other interpreted languages. JITC ( Just in Time compilation) is one such feature that enables Java as high-performance language (HPC).

Multithreaded.

Java is inherently multi-threaded. A single Java program can have many different threads executing independently and continuously.

Distributed.

Java application can be distributed and the implementation of variable protocols enables effective communication.

What is classloader?

The classloader, a subsystem of JVM, is used to load classes and interfaces.

Types of classloader.

There are many types of classloaders e.g.

  • Bootstrap,
  • Extension,
  • System,
  • Plugin.
What is Heap space in Java?

Java Virtual Machine(JVM) gets some memory from the underlying operating system to run any java program. This memory is referred as Java heap.

Heap in Java located at bottom of address space and move upwards. When an object is created using new operator or by any other means object is allocated memory from Heap and when the object is garbage collected, memory goes back to Heap space in Java and get reused.

Default heap size in Java.

Default maximum heap space is 1/2 of physical memory of size upto 192 bytes and 1/4th of physical memory for size upto 1Gig. So for 1Gig machine maximum heap size is 256MB 2.maximum heap size will not be used until program creates enough object to fill initial heap space which will be much lesser but at-least 8 MB or 1/64th part of Physical memory upto 1GB.

For Servers, Java virtual machine default maximum heap space is 1G for 4GB of physical memory on a 32 bit JVM. for 64 bit JVM its 32G for a physical memory of 128GB.

What is Java Memory Model?

The Java memory model specifies how the Java virtual machine works with the computer's memory (RAM).

What are the new features introduced in Java 8?
  • Lambda Expressions,
  • Interface Default and Static Methods,
  • Method Reference,
  • Parameters Name,
  • Optional feature,
  • Streams,
  • and Concurrency.
Difference between EAR, JAR and WAR file in J2EE.

J2EE defines three types of archives.

  • Java Archive (JAR) A JAR file encapsulates one or more Java classes, a manifest, and a descriptor. JAR files are the lowest level of archive. JAR files are used in J2EE for packaging EJBs and client-side Java Applications.
  • Web Archive (WAR) A WAR files are similar to JAR files, except that they are specifically for web applications made from Servlets, JSPs, and supporting classes. Struts and Spring based Web applications may be archived to a WAR.
  • Enterprise Archive (EAR) An EAR file contains all of the components that make up a particular J2EE application. A EAR may contain one or more WAR files. EAR files can also contain connector modules packaged as RAR files and Client modules packaged as JAR files.
Explain memory leak in Java.

Memory leak in Java refers to a situation where some objects are not used by the application any more, but GC fails to recognize them as unused. As a result, these objects remain in memory indefinitely, reducing the amount of memory available to the application.

How Garbage collection works in Java?

Every Java object tree must have one or more root objects. As long as the application can reach those roots, the whole tree is reachable.

There are some objects which are considered ?important? by GC. These are called GC roots and are (almost) never discarded. They are, for example, currently executing method's local variables and input parameters, application threads, references from native code and similar ?global? objects. Any object referenced from those GC roots are assumed to be in use and not discarded.

What are the GC roots in Java?

There are 4 different kinds of GC roots in Java.

  • Local variables,
  • Active Java threads,
  • Static variables,
  • and JNI References.
Types of memory leaks in Java.

OutOfMemoryError is one of the symptom to be diagnosed as it could lead to memory leaks.

Application may crash without issuing OutOfMemoryError.

How do you diagonise memory leaks in Java?
  • Identify symptoms,
  • Enable verbose garbage collection using -verbosegc argument,
  • Enable profiling,
  • Analyze the trace.
Difference between Stack and Heap memory in Java.

The main difference between heap and stack is that stack memory is used to store local variables and function call while heap memory stores objects in Java.

Each Thread in Java has its own stack whose size can be specified using -Xss JVM parameter, similarly, you can also specify heap size of Java program using JVM option -Xms and -Xmx where -Xms is the starting size of the heap and -Xmx is the maximum size of java heap.

If there is no memory left in the stack for storing function call or local variable, JVM will throw java.lang.StackOverFlowError, while if there is no more heap space for creating an object, JVM will throw java.lang.OutOfMemoryError: Java Heap Space.

Size of stack memory is a lot lesser than the size of heap memory in Java.

Variables stored in stack is only visible to the owner thread while objects created in the heap are visible to all threads. Stack memory is a private memory of a Java Thread while heap memory is shared among all threads.

Does Java garbage collector cleans both heap and stack memory?

GC sweeps heap memory only. Usually, stack memory is collected automatically when the execution path reaches the end of the scope.

Why garbage collection is required in Java?

Garbage collection relieves programmers from the burden of freeing allocated memory.

GC helps ensure program integrity. Garbage collection is an important part of Java's security strategy.

How GC does not affect Java program's performance?

Because Java's garbage collector runs in its own thread, in most cases, it will run transparently alongside the execution of a program.

Different categories of GC algorithm.

GC algorithms can be divided into 3 categories: sweeping, compacting and copying.

List the available GC algorithms in Java.

The Serial GC recommended for client-style applications that do not have low pause time requirements.

The Parallel GC - use when the throughput matters.

The Mostly-Concurrent GC (also known as Concurrent Mark-Sweep GC(CMS)) - use when the latency matters.

The Garbage First GC (G1) - new GC algorithm, for CMS replacement.

What is the default GC collector in Java 8?

Parallel GC. It is faster compared to the other algorithms.

What are types of reference in Java?

Java provides 4 different types of Reference Objects: strong, weak, soft and phantom.

Explain strong reference type in Java.

Strong reference : This reference is something that we use daily while writing the code. Any object in the memory which has active strong reference is not eligible for garbage collection.

For example String s = "abc" , reference variable s has strong reference to String object "abc". Any object which has Strong reference attached to it is not eligible for garbage collection.

Explain weak reference type in Java.

Weak Reference are represented using java.lang.ref.WeakReference class and you can create Weak Reference by using following code.

WeakReference<StringBuilder> weakBuilder = new WeakReference<StringBuilder>(builder);

A weak reference is a reference that isn't strong enough to force an object to remain in memory. Weak references allow you to leverage the garbage collector's ability to determine reachability for you, so you don't have to do it yourself.

Explain Serial Garbage Collector in Java.

Serial garbage collector works by holding all the application threads. It is designed for the single-threaded environments. It uses just a single thread for garbage collection. The way it works by freezing all the application threads while doing garbage collection may not be suitable for a server environment. It is best suited for simple command-line programs.

Run with -XX:+UseSerialGC JVM argument to use the serial garbage collector.

Serial GC is recommended for client-style applications that do not have low pause time requirements.

Explain Parallel Garbage Collector in Java.

Parallel garbage collector also called as throughput collector, is the default garbage collector of the JVM. Unlike serial garbage collector, this uses multiple threads for garbage collection. Similar to serial garbage collector this also freezes all the application threads while performing garbage collection.

Turn on the -XX:+UseParallelGC JVM argument to use the CMS garbage collector.

The Parallel GC is recommended for situations where high throughput required.

Explain CMS Garbage Collector in Java.

Concurrent Mark Sweep (CMS) garbage collector uses multiple threads to scan the heap memory to mark instances for eviction and then sweep the marked instances. CMS garbage collector holds all the application threads in the following two scenarios only,

  • While marking the referenced objects in the tenured generation space.
  • If there is a change in heap memory in parallel while doing the garbage collection.

CMS collector uses more CPU to ensure better application throughput. If we can allocate more CPU for better performance then CMS garbage collector is the preferred choice over the parallel collector.

Turn on the XX:+USeParNewGC JVM argument to use the CMS garbage collector.

Explain G1 Garbage Collector in Java.

G1 garbage collector is used for large heap memory areas. It separates the heap memory into regions and does collection within them in parallel. G1 also does compacts the free heap space on the go just after reclaiming the memory. But CMS garbage collector compacts the memory on stop the world (STW) situations. G1 collector prioritizes the region based on most garbage first.

Turn on the -XX:+UseG1GC JVM argument to use the G1 garbage collector.

What is soft reference in Java?

A soft reference is exactly like a weak reference, except that it is less eager to throw away the object to which it refers. An object which is only weakly reachable (the strongest references to it are WeakReferences) will be discarded at the next garbage collection cycle, but an object which is softly reachable will generally stick around for a while.

What is phantom reference in Java?

A phantom reference is quite different than either SoftReference or WeakReference. Its grip on its object is so tenuous that you can't even retrieve the object -- its get() method always returns null. The only use for such a reference is keeping track of when it gets enqueued into a ReferenceQueue, as at that point you know the object to which it pointed is dead.

Explain the javap command in Java.

The javap command disassembles one or more class files. javap prints out the package, protected, and public fields and methods of the classes passed to it. javap prints its output to stdout.

You may use it to find out what methods are available for a class if you don?t have the source code that was used to create the class.

javap [options] JavaClassName

What is PermGen in Java?

PermGen stands for permanent generation space.

PermGen is a memory pool containing all the reflective data of the java virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas. The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application.

The permanent generation is populated by the JVM at runtime based on classes in use by the application. In addition, Java SE library classes and methods may be stored here.

What is metaspace in JDK 1.8?

PermGen has been completely replaced by MetaSpace in Java8, a native memory to store class meta-data information and that grows automatically.

We don't encounter OutOfMemoryError in Java 1.8, PermSize and MaxPermSize arguments are ignored at the start-up of application. On the contrary these two jvm arguments are now replaced by MetaspaceSize and MaxMetaspaceSize. These two new flags gives us the flexibility to change the value for default size of metaspace and to change the maximum value that this metaspace can take.

Explain the usage of MetaspaceSize parameter in Java 8.

Set MetaspaceSize to a value larger than the default, if you know that your applications needs more space for class data. Setting it to a larger size will avoid some number of GCs at startup.

Difference between permgen and metaspace in Java.

Metaspace auto increases its size by default (up to what the underlying OS provides), while PermGen always has a fixed maximum size.

New features introduced in Java 1.7.

1. Underscores Between Digits in Numeric Literals.

int ten_million = 10_000_000;

2. Improved Type Inference for Generic Instance Creation using Diamond operator.

Map<String, List<String>> anagrams = new HashMap<String, List<String>>();

// The above statement can be rewritten using Java Diamond operator.

Map<String, List<String>> anagrams = new HashMap<>();

3. Handling multiple exceptions in a single catch block.

4. String in Switch Expression.

5. Try-with-resources Statement.

6. Integral Types as Binary Literals.

How do I load Jars dynamically at runtime?

Using URLClassLoader we can load the jar.

URLClassLoader child = new URLClassLoader (myJarFile.toURL(), this.getClass().getClassLoader());
Class classToLoad = Class.forName ("net.javapedia.MyClass", true, child);
Method method = classToLoad.getDeclaredMethod ("methodToCall");
Object instance = classToLoad.newInstance ();
Object result = method.invoke (instance);
What is synthetic construct in Java?

Synthetic constructs are class, fields, and methods that are not defined in Java source code and created by the Java compiler while compiling.

For switch statement, Java compiler creates a synthetic field that start with $, and also for the local classes which is required by the VM.

Synthetic constructs are also known as dynamic proxy.

New changes introduced in Java 9.

Allow @SafeVargs on private instance methods.

Allow effectively final variables to be used as resources in the try-with-resources statement.

Allow the diamond with anonymous classes if the argument type of the inferred type is denotable.

Complete the removal of the underscore from the set of legal identifier names.

Add support for private interface methods.

What is jshell tool in Java 9?

JDK 9 adds Read-Eval-Print Loop (REPL) functionality to the Java platform.

The jshell tool provides an interactive command-line interface for evaluating declarations, statements, and expressions of the Java programming language. It facilitates prototyping and exploration of coding options with immediate results and feedback.

Difference between Java.exe and Javaw.exe.

Java is the plain old java interpreter, you have a console window open all the time. Javaw is a wrapper around java that doesn't open a console window.

java.exe pops up a console window. javaw.exe does not. If you expect text output, you'll need to use java.exe. If the application pops up its own window (For example, Eclipse), you can use javaw.exe.

What is Nashorn in Java8?

Nashorn is the new Javascript processing engine for the Java platform that shipped with Java 8. Until JDK 7, the Java platform used Mozilla Rhino for the same purpose as a Javascript processing engine.

How do I debug to see when class is loaded in jvm?

You may add the command line option -verbose:class to your Java process, this will display information about each class loaded.

java -verbose:class
Explain Class loading subsystem of JVM.

«
»
Object, Class and Package

Comments & Discussions