#4 How Java Works

#4 How Java Works

Understanding Java Execution: From Code to JVM

The Problem with Running Java Code

  • The initial attempt to run the code fails, highlighting a need for understanding how Java applications operate behind the scenes.
  • The importance of the Java Virtual Machine (JVM) is introduced, emphasizing that it allows Java's platform independence across various operating systems like Windows, Linux, and Mac.

JVM and Its Environment

  • The relationship between the OS and hardware is explained; JVM operates on top of an OS which in turn runs on hardware.
  • JVM's role is clarified as executing Java code, but it does not accept raw code directly from programmers.

Platform Independence Explained

  • Platform independence means that a Java application can run on any machine with a JVM installed, regardless of hardware or OS.
  • While applications are platform-independent, the JVM itself is platform-dependent; it must be built for specific operating systems.

Bytecode and Compilation Process

  • To execute code, developers must convert their readable Java code into bytecode—a format understandable by the JVM—using a compiler known as javac.
  • This process involves writing human-readable code which then gets compiled into bytecode before being executed by the JVM.

Project Structure and Main Method Requirement

  • In larger projects with multiple files, only one file will be executed first. Developers must specify this entry point when running their application.
  • Each main file must contain a main method with a specific signature (public static void main(String[] args)), which serves as the starting point for execution.

Key Steps in Running a Java Application

  • The steps to follow include creating a Java file, compiling it into bytecode using javac, and finally running that bytecode on the JVM.

Understanding Java Code Structure and Execution

Introduction to JVM and Code Execution

  • The Java Virtual Machine (JVM) initiates code execution by looking for specific syntax in the code, which is essential for generating output.
  • It is crucial to encapsulate your code within a main method, as this is what the JVM requires to execute the program.

Main Method Syntax

  • The syntax for defining the main method in Java is public static void main(String[] args), which will be elaborated on in future sessions.
  • In object-oriented programming, everything revolves around objects; thus, a class must be defined to create these objects.

Class Definition and Indentation

  • A class definition must accompany your code; it should have the same name as your file. Proper indentation using tabs enhances readability.
  • Avoid unnecessary suggestions from development tools during teaching; focus on writing clean and understandable code.

Compiling and Running Java Code

  • After compiling your Java file, a bytecode file with a .class extension is generated automatically. This bytecode is essential for running the application.
  • To run the compiled code, use the command java Hello, where "Hello" refers to the class name—not the filename—resulting in output like "Hello World".

Concept of JRE and JVM

  • Running a Java application requires additional libraries and runtime environments similar to needing ingredients when cooking; these are stored within an environment called JRE (Java Runtime Environment).
  • The JVM (Java Virtual Machine), part of JRE, executes Java applications. Understanding both components is vital for effective coding.

Developer Tools: JDK vs. JRE

  • The JDK (Java Development Kit), used by developers, includes both JRE and JVM. However, only JRE needs to be present on machines running compiled applications.
  • When distributing applications, ensure that only JRE (and not JDK) is required on other machines for successful execution.
Video description

Check out our courses: Complete Java Developer Course Batch-4: https://go.telusko.com/Complete4 Coupon: TELUSKO10 (10% Discount) Spring Boot 3 and Spring AI - Live Course :- https://go.telusko.com/spring3springAI Coupon: TELUSKO10 (10% Discount) Master Java Spring Development : https://go.telusko.com/masterjava Coupon: TELUSKO20 (20% Discount) Udemy Courses: Spring: https://go.telusko.com/udemyteluskospring Java:- https://go.telusko.com/udemyteluskojava Java Spring:- https://go.telusko.com/Udemyjavaspring Java For Programmers:- https://go.telusko.com/javaProgrammers Python : https://go.telusko.com/udemyteluskopython Git : https://go.telusko.com/udemyteluskogit Docker : https://go.telusko.com/udemyteluskodocker For More Queries WhatsApp or Call on : +919008963671 website : https://courses.telusko.com/ n this lecture we will learn: - Steps to execute the Java code - What is Byte Code? - Java is a platform-independent language - Execution always starts from the main method - Object-oriented language - What are JRE and JDK? #1 Steps to run the Java Code:- - We required JVM to run a java code. - The job of the JVM is to execute the java code that you have created but it does not accept the code directly. - JVM does not accept the Java code as it only understands the Byte Code. - Java compiler also known as javac is used to convert java code into byte code. - The extension for the byte code is .class - Thus, the code written by the programmer is first converted into byte code by the compiler and then that byte code goes to the JVM for further execution. Programmer- Java Code- Compiler- Byte Code- JVM #2 Java is a platform-independent language:- It means you can run Java on any machine irrespective of any hardware or operating system. The one thing that the machine requires to run a java code is a JVM. :- JVM is platform dependent and it needs to be built for a particular OS. #3 In Java, execution always starts from the main method:- - JVM starts execution only with one first file. If you need to run multiple files you need to tell it which one is the first file through which JVM can start execution. - The first file must contain the main method with its proper signature. - In Java, execution always starts from the main method. Signature of main method: public static void main(String args[]) #4 Java is an object-oriented language:- - Object-oriented means everything should be an object. - We always required a class to create an object. #5 JRE:- - To run the Java application, you need extra libraries and need runtime. Runtime is something where you run things. - You use extra libraries, and you also need an environment. An additional layer is present above the OS known as JRE. - JVM is a part of JRE. JRE includes JVM and libraries. JVM - Java Virutal Machine JRE - Java Runtime Environment - For development purposes, you need to install JDK (Java Development Kit) which consists of JRE and JVM. - Java is WORA that is Write Once Run Anywhere provided that JVM and JDK installed. Github repo : https://github.com/navinreddy20/Javacode.git Java:- https://bit.ly/JavaUdemyTelusko Spring:- https://bit.ly/SpringUdemyTelusko More Learning : Java :- https://bit.ly/3x6rr0N Python :- https://bit.ly/3GRc7JX Django :- https://bit.ly/3MmoJK6 JavaScript :- https://bit.ly/3tiAlHo Node JS :- https://bit.ly/3GT4liq Rest Api :-https://bit.ly/3MjhZwt Servlet :- https://bit.ly/3Q7eA7k Spring Framework :- https://bit.ly/3xi7buh Design Patterns in Java :- https://bit.ly/3MocXiq Docker :- https://bit.ly/3xjWzLA Blockchain Tutorial :- https://bit.ly/3NSbOkc Corda Tutorial:- https://bit.ly/3thbUKa Hyperledger Fabric :- https://bit.ly/38RZCRB NoSQL Tutorial :- https://bit.ly/3aJpRuc Mysql Tutorial :- https://bit.ly/3thpr4L Data Structures using Java :- https://bit.ly/3MuJa7S Git Tutorial :- https://bit.ly/3NXyCPu Donation: PayPal Id : navinreddy20 https://www.telusko.com