The Java
Environment
The Java environment consists of several components
that work together to develop, compile, and run Java programs. The key elements
are:
- Java Development Kit (JDK):
- A complete software development environment for building Java
applications.
- It includes the Java Compiler (javac), Java Runtime Environment (JRE), Java Debugger, and various tools
for building, testing, and documenting Java programs.
- Java Runtime Environment (JRE):
- Provides the libraries, Java Virtual Machine (JVM), and other
components to run Java applications.
- It does not include development tools like the compiler or debugger.
- Java Virtual Machine (JVM):
- An abstract machine that provides the runtime environment to execute
Java bytecode.
- Makes Java platform-independent by converting bytecode to machine
code for any operating system.
Java Source File
A Java source file is a plain text file
containing Java code with a .java extension. It
consists of:
- Package Declaration (Optional):
- Defines a namespace for organizing related classes.
- Example:
o
package mypackage;
- Import Statements (Optional):
- Used to import built-in or user-defined classes or packages.
- Example:
o
import java.util.Scanner;
- Class Declaration (Required):
- Defines the blueprint of a class which includes attributes and
methods.
- Example:
o
public class MyClass {
o
// Fields
(Variables)
o
int x;
o
o
// Methods
(Functions)
o
public
void display() {
o
System.out.println("Hello,
World!");
o
}
o
}
- Main Method (Required for Execution):
- Entry point for program execution.
- Example:
o
public static void main(String[] args) {
o
MyClass
obj = new MyClass();
o
obj.display();
o
}
Structure of a
Java Source File
// Package
Declaration (Optional)
package
mypackage;
// Import
Statements (Optional)
import
java.util.Scanner;
// Class
Declaration
public class
MyClass {
// Fields (Variables)
private String name;
// Constructor
public MyClass(String name) {
this.name = name;
}
// Methods (Functions)
public void display() {
System.out.println("Hello, "
+ name + "!");
}
// Main Method
public static void main(String[] args) {
MyClass obj = new
MyClass("Krishna");
obj.display(); // Output: Hello, Krishna!
}
}
Compilation and
Execution Process
Java follows a two-step compilation process:
1. Compilation
- The source code file (.java) is compiled by the Java Compiler (javac).
- This process converts the source code into bytecode, stored in
a .class file.
- Example of compilation:
·
javac MyClass.java
- This generates MyClass.class which contains platform-independent bytecode.
2. Execution
- The .class file is
executed by the Java Virtual Machine (JVM).
- The JVM converts the bytecode into machine-specific code and
executes it.
- Example of execution:
·
java MyClass
- Output: Hello, Krishna!
Compilation
Process Workflow
- Write Code:
- Write the Java code and save it with a .java extension.
- Compile Code:
- Use the javac compiler to convert the .java file to .class bytecode.
- Load Bytecode:
- The JVM loads the .class file into memory via the Class Loader.
- Verify Bytecode:
- The Bytecode Verifier checks the code for any security
violations or incorrect instructions.
- Execute Bytecode:
- The JVM’s Interpreter/Just-In-Time (JIT) Compiler translates
bytecode to machine code for execution.
Example
Compilation and Execution
Given a file HelloWorld.java:
public class
HelloWorld {
public static void main(String[] args) {
System.out.println("Hello,
World!");
}
}
Compiling:
javac
HelloWorld.java
Generates: HelloWorld.class
Executing:
java HelloWorld
Output:
Hello, World!
Important Points
to Remember
- The Java file name must match the public class name.
- Case Sensitivity: Java is
case-sensitive (HelloWorld is
different from helloworld).
- Platform Independence: Bytecode
can run on any machine with JVM installed.
- Security: Bytecode verification prevents unsafe code
execution.
- Classpath: The -classpath option specifies the location of user-defined classes and packages.
No comments:
Post a Comment