HOW JAVA WORKS

 

HOW JAVA WORKS

How Java Code Works:

Java is a high-level, object-oriented programming language that was first developed by Sun Microsystems in 1995 (now owned by Oracle Corporation). It’s designed to be simple, flexible, and platform-independent, meaning that Java programs can run on any device or operating system that has a Java Virtual Machine (JVM). Let us know how does it actually work?

It involves a series of steps that transform human-readable code into machine-executable instructions.

1. Writing Java Code

The first step is writing the Java code. You write Java code using a text editor or an Integrated Development Environment (IDE) like Eclipse or IntelliJ IDEA. The code is saved in a file with a .java extension.

You save this as Demo.java. This is your source code.

2. Compiling Java Code

Once the code is written, you need to compile it. This means converting your human-readable code into bytecode, a language that the computer can understand.

To compile your code:

  1. Open the Command Prompt (Windows) or Terminal (Mac/Linux).
  2. Go to the folder where you saved your code.
  3. Run the following command:

This creates a new file called Demo.class. This .class file contains bytecode.


3. Running the Program with JVM

  • Now, the bytecode is ready, but we need to run it. This is where the Java Virtual Machine (JVM) comes in. The JVM turns the bytecode into machine code, which is what your computer understands and executes.
  • JVM allows Java’s bytecode to execute on any device,that why java known as “write once ,run anywhere”

To run the program, use this command:

The JVM will read the Demo.class file, convert it into machine code, and then print “Hello World” on the screen.

Summary:

  1. Write Java Code: You write code in a .java file.
  2. Compile: The code is turned into bytecode using javac.
  3. Run with JVM: The JVM converts bytecode into machine code and runs it.

INTERVIEW QUESTIONS:

  1. What is Java, and why is it called a “write once, run anywhere” language?
  2. What are the steps involved in writing and running a Java program?
  3. What is the difference between a .java file and a .class file in Java?
  4. Explain the role of the Java Virtual Machine (JVM) in executing Java programs.
  5. What is bytecode in Java, and why is it important?
  6. What command is used to compile a Java program?
  7. What command is used to run a compiled Java program?
  8. What happens when you execute the java Demo command in the terminal?
  9. What is the significance of the public static void main(String[] args) method in a Java program?
  10. What does the javac command do during the compilation process?
  11. Why is Java considered platform-independent?
  12. What is the role of bytecode in the Java compilation process?
  13. Can Java code run on any machine without modification? Explain why.
  14. How does the JVM convert bytecode into machine-readable instructions?
  15. What is the difference between bytecode and machine code?
  16. Explain the term “JVM-based execution.” How does this process enable Java to be platform-independent?
  17. What are some of the reasons Java uses bytecode rather than direct machine code for execution?
  18. How does the JVM manage platform-specific details while running Java code?
  19. What is the advantage of using Java’s “write once, run anywhere” capability in a large software development project?
  20. What role does the javac compiler play in making Java platform-independent?
  21. What does it mean for a Java program to be “compiled” and “interpreted”?
  22. How do you handle errors if your Java program does not compile or run correctly?
  23. What kind of tools and IDEs do you typically use when writing Java code? Why?

The answers---> (answers)

Comments

Popular posts from this blog

"Don't Believe Everything You Think"-Joseph Nguyen

How Java Works (Interview Question Answers)