Professional Documents
Culture Documents
FEATURES OF JAVA - PPTM
FEATURES OF JAVA - PPTM
JAVA
DAY 2
JAVA BUZZWORDS/MAIN FEATURES:
Platform Independence: Java's bytecode can run on any system with a compatible JVM, enabling "Write
Once, Run Anywhere."
Object-Oriented: Java's code is organized around objects, promoting reusability and modular design.
Robust and Secure: Java's strict type checking and exception handling enhance reliability and security.
Simple and Familiar: Java's syntax is easy to understand, influenced by C and C++.
High Performance: While not as fast as some languages, Java balances performance with portability.
Dynamic: Java allows loading classes and resources at runtime for adaptability.
These buzzwords capture the essence of Java's strengths, making it a versatile and widely used
programming language.
JAVA PROGRAM EXECUTION
Writing the Java Code: First, you need to write the Java code using a text editor or an
Integrated Development Environment (IDE) such as Eclipse, IntelliJ IDEA, or Visual Studio Code.
Compiling: Java is a compiled language, which means that your human-readable Java code
needs to be converted into machine-readable bytecode. This is done using the Java Compiler
(javac). When you run the compiler on your .java source files, it produces corresponding .class
files containing bytecode. These files are sometimes referred to as "compiled classes".
Java Virtual Machine (JVM): The JVM is responsible for executing Java programs. It's a
platform-specific software that interprets or compiles and runs the bytecode produced earlier. The
JVM handles memory management, garbage collection, and other runtime tasks.
Class Loading: When a Java program starts, the JVM loads the necessary classes into memory.
Java classes can be loaded from the local file system or from network locations.
Bytecode Verification: Before executing the bytecode, the JVM performs bytecode verification to ensure
that it's valid and won't cause security or stability issues.
Just-In-Time Compilation (JIT): To improve performance, many JVM implementations use JIT compilation.
This means that certain parts of the bytecode are compiled into native machine code just before they are
executed. This can lead to faster execution times compared to interpreting the bytecode directly.
Execution: The JVM executes the bytecode instructions one by one, following the program logic you've
written. It manages memory, object creation, method calls, and other runtime aspects of the program.
Garbage Collection: The JVM automatically manages memory by reclaiming memory that is no longer
being used (i.e., unreachable objects). This process is called garbage collection.
Termination: The program runs until it completes its tasks or encounters an error. At this point, the JVM
releases any resources used by the program, including memory, file handles, and network connections.
It's important to note that the JVM abstracts away many of the low-level details of the underlying hardware and
operating system, allowing Java programs to be highly portable across different platforms.
STURCTURE OF A JAVA PROGRAM:
Class Declaration:
public class HelloWorld declares a class named HelloWorld.
Class names must start with an uppercase letter and follow the CamelCase naming convention.
Main Method:
public static void main(String[] args) is the entry point of the program.
The public keyword indicates that the method is accessible from outside the class.
static means the method belongs to the class itself, not to instances of the class.
void indicates that the method doesn't return a value.
main is the method name, and String[] args is an array of command-line arguments.
Method Body:
The code within curly braces { ... } constitutes the method body.
System.out.println("Hello, World!"); is a statement that prints "Hello, World!" followed by a newline.
System.out.println:
System is a class that provides access to the system resources.
out is a public static field (an instance of the PrintStream class) that represents the standard output stream.
println("Hello, World!"); is a method call to print the given text followed by a newline.
Execution:
• The Java compiler (javac) compiles the code into bytecode, generating a
HelloWorld.class file.
• The Java Virtual Machine (JVM) interprets or compiles the bytecode, executing the
program.
• The main method is the entry point, and it's executed when you run the program.
• The System.out.println statement prints "Hello, World!" to the console.
This simple example illustrates the core structure of a Java program, with a class
declaration, a main method, and a statement that prints output to the console
Naming Conventions
Case Sensitivity:
Java is case-sensitive, meaning that uppercase and lowercase letters are treated as distinct
characters.
For example, myVariable and myvariable are considered different identifiers.
Semicolons:
Statements in Java are terminated by semicolons (;).
Semicolons indicate the end of a statement and are used to separate multiple statements
on the same line.
Comments:
Comments provide non-executable text used for documentation and explanations.
Single-line comments start with // and extend to the end of the line.
Multi-line comments are enclosed between /* and */.
Operators:
Arithmetic operators: +, -, *, /, % (remainder).
Comparison operators: ==, !=, <, >, <=, >=.
Logical operators: && (AND), || (OR), ! (NOT).
Control Structures:
if, else if, else statements for conditional branching.
switch statement for multi-way branching.
while, do-while, and for loops for iteration.
Method Calls:
Methods are invoked using their names followed by parentheses.
Arguments (if required) are enclosed within the parentheses.
Access Modifiers:
Control the visibility and accessibility of classes, fields, and methods.
Common access modifiers include public, private, protected, and package-private (default)
.
String Concatenation:
Strings can be concatenated using the + operator.
For example, "Hello, " + "world" results in "Hello, world".
JAVA BYTECODE:
Java bytecode is an intermediate representation of Java source code that serves as an
essential component in the execution of Java programs. It's a low-level instruction set that
is platform-independent and can be executed by the Java Virtual Machine (JVM). Here's an
overview of Java bytecode:
Interpretation: The JVM reads each bytecode instruction and executes it directly. While
this approach is slower compared to native machine code execution, it allows for greater
portability since the same bytecode can be executed on different platforms.
Just-In-Time (JIT) Compilation: Some JVM implementations use JIT compilation. In this
approach, portions of bytecode are compiled into native machine code right before they are
executed. This results in faster execution times compared to pure interpretation.
In summary, Java bytecode is a vital component in the execution of Java programs. It allows for
platform independence, security, and portability while serving as an intermediary between
source code and machine code execution.