Photo by Vlad Tchompalov on Unsplash

IMPORTANT CONCEPTS IN JAVA

Java Concepts — Features, JDK & JVM

Explanations, Examples, Codes and Diagrams

Published in
6 min readApr 1, 2022

--

This series on Must Know Java Concepts will introduce all the important Java Concepts.

  1. Java Features & JDK ← we are here
  2. Fundamentals & OOPS
  3. Generics
  4. Collections
  5. Exception Handling
  6. Files & Input-Output
  7. Serialization
  8. Multithreading
  9. Synchronization
  10. Networking
  11. All 50 Java Keywords
Image created by Suryakant Bharti

Java Features and Program Execution

Java is a programming language and a platform.

Platform: A platform is a software environment where a program runs. Java has its own runtime environment (JRE) and Application Programming Interface (API), so it is called a platform.

Features of Java :

Image created by Suryakant Bharti

Simple & Familiar:

  • Its syntax and features are like C/C++, so it is familiar.
  • It does not use complex/confusing concepts of C/C++ like explicit pointers, goto statements, operator overloading, multiple inheritance, storage classes, preprocessors, header files, etc.
  • It provides Automatic Garbage Collection, so no need to remove the unreferenced objects manually.
  • It has a great collection of APIs which programming and development easier.

Object-Oriented:

  • Software development technique in which we codebase is centered around objects that contain both data (attributes)and code to modify the data (behavior).
  • Fundamental concepts of Object Orient Programming are Objects, Class, Inheritance, Polymorphism, Abstraction, Encapsulation.

Portable:

  • Java code is a portable as can run in any operation system via JVM (Java Virtual Machine), i.e., Write Once and Run Anywhere.

Platform Independent:

  • For running Java code, it is compiled by the compiler and converted to bytecode. This bytecode is a platform-independent, i.e., it can run on all platforms (operating systems) — Windows, Linux, Mac, etc.
Image created by Suryakant Bharti

Secured:

  • Java does not allow explicit pointers. Pointers point to memory locations which is used for memory management, by allowing to use it directly can be security nightmare.
  • Java programs runs in Java Virtual Machine which separates it from the operating system and makes the security better.
  • Class Loaders, which are responsible for loading Java classes dynamically to the JVM during runtime, increases security by separating the packages for classes of the local disk from those of network.
  • Bytecode Verifier acts like gatekeeper and ensures that the code that is passed to an interpreter is not likely to break the Java interpreter.
  • One of Java built-in security feature is Security Manager which regulate what resources a class can access like connecting to network or reading/writing to the local disk.
  • Java has security features which provide authentication based on encryption and helps us in creating tamper-free software systems. We can use SSL, JAAS, cryptography, encryption keys, etc.

Robust:

  • Java reduces situations which can produce issues by putting emphasis on compile-time and runtime errors checks.
  • Java provides automatic garbage collection which highly improves memory management.
  • With no explicit pointers Java increases security.
  • With exception handling in Java increases robustness against errors.
  • Strongly typed — It means all variables must be declared with a data type.
  • Statically typed — It means the type checking of variables is done during compile time.

Architecture-Neutral:

  • Java does not have implementation dependent features. E.g., primitive data type size is fixed.

Interpreted:

  • Java code is compiled to bytecodes, which are then interpreted by JRE.
  • The interpreter reads bytecode stream then execute the instructions.

High-Performance:

  • Uses Bytecode — Java is faster than traditional interpreted languages since byte code is “close” to native code.
  • Just-In-Time (JIT) — It is designed to support JIT compilers, which dynamically compile bytecodes to machine code.
  • Garbage Collector — it collects unused memory space and improves the performance of the program.

NOTE: Java is still slower than a compiled language like C/C++.

Distributed:

  • Devices can access each-other by calling methods from other devices connected to internet (depending on accessibility provided by developer). Thus, helping us create distributed applications.

Multi-threaded:

  • Thread is similar to an independent program which can execute concurrently. By using multiple threads, we can write Java applications that handle multiple tasks simultaneously.
  • Primary advantage of multi-threading is that it shares a common memory area.
  • Use of threads are critical for mobile-apps, web apps, games, and any application that uses network, database, i/o calls extensively.

Dynamic:

  • Dynamic Compilation (JIT) — Implementations to gain performance during program execution. The machine code released by a dynamic compiler is built and optimized at the runtime of the program. Using dynamic compilation allows efficiency optimization.
  • Load on Demand — Loads in classes as they are needed, even from across the network.
  • Dynamic Memory Allocation — All Java objects are dynamically allocated.
  • Dynamic Polymorphism — Compiler does not know which method to be called in advance. JVM decides which method to called at run time.

Java Program Execution Process :

Image created by Suryakant Bharti

JDK — JRE — JVM — JIT :

Image created by Suryakant Bharti

Java Development Kit (JDK): It is a collection of development tools including JRE.

Java Runtime Environment (JRE): It contains set of libraries and the JVM.

Java Virtual Machine (JVM): It is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed. Primary work done by JVM are loading code, verifying code, executing code, and providing runtime environment.

NOTE — JVMs are available for many hardware and software platforms. JVM, JRE as well as JDK are platform dependent because configuration of each OS varies. But Java itself is platform independent.

Internal Architecture of JVM :

Image created by Suryakant Bharti

JVM (Java Virtual Machine) has various subcomponents internally. You can see the most important ones in the above diagram.

Class loader sub system:

JVM’s class loader sub system performs 3 tasks

  • It loads .class file into memory.
  • It verifies byte code instructions.
  • It allots memory required for the program.

Run time data area:

This is the memory resource used by JVM and it is divided into 5 parts

  • Class (Method) Area: It stores code for methods, data for field and method, constant pool.
  • Heap: It stores all class instances.
  • Stacks: It executes Java methods; stores local variables, partial results, and plays a key role in calling and returning of a method. It contains frames, and on each frame, a separate method is executed. Each thread has a private stack that is created simultaneously with the thread. A new frame is created every time a method is called and destroyed when method is finished.
  • Program Counter Registers: PC (program counter) register. It contains the address of the JVM instruction currently being executed.
  • Native Method Stacks: Are places where native methods (e.g., C language programs, etc.) are executed.

Native method interface:

Native method interface is a program that connects native methods libraries (C header files) with JVM for executing native methods.

Native method library:

Holds the native libraries information.

Execution engine:

  • Just-In-Time (JIT) compiler: It is used to improve the performance. It converts byte code into machine code. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Compiler translates the instruction set of JVM into the instruction set for a given CPU.
  • Interpreter: Read bytecode stream then execute the instructions.
  • Virtual processor
  • NOTE — JVM uses optimization technique to decide which part to be interpreted and which part to be used with JIT compiler.

Congrats! 🎉 We are one step closer to “becoming a Java Expert” now!!

Image created by Suryakant Bharti

P.S: This article is supposed to serve as a beginner’s guide for JDK & Program Execution.

Give a clap if you liked the article or a leave a comment for any suggestion/discussion/dispute. Thank you!

--

--

Android Developer (Kotlin, Java, OOPS, Data Structures, Algorithms, Design Patterns) github.com/Suryakant-Bharti