Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

FEATURES OF

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++.

 Portable: Java apps can be moved across systems without modification.

 High Performance: While not as fast as some languages, Java balances performance with portability.

 Multithreaded: Java supports concurrent execution, enabling responsive applications.

 Distributed: Java's networking libraries facilitate building distributed applications.

 Dynamic: Java allows loading classes and resources at runtime for adaptability.

 Architecture Neutral: Java abstracts hardware details for consistent behavior.

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".

 Bytecode: Bytecode is a low-level representation of your Java code that is platform-independent.


It's not directly executable by the CPU, but it can be executed by the Java Virtual Machine (JVM).

 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:

Let us go through the structure of a very basic program:


public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Explaination of Structure:
 Package Declaration (Optional):
In this example, we don't have a package declaration, but you can include it if your class belongs to a specific
package.
It is used to group the related classes.

 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

•For classes, we use Pascal Convention. The first and Subsequent


characters from a word are capital letters (uppercase).
Example: Main, MyScanner, MyEmployee.

•For functions and variables, we use camelCaseConvention.


Here the first character is lowercase, and the subsequent
characters are uppercase like myScanner, myMarks.
JAVA SYNTAX:
Java syntax refers to the rules and conventions that define how Java code is written and
structured. Adhering to proper syntax is essential for writing code that is both
understandable by humans and correctly interpreted by the Java compiler. Here's an
overview of Java syntax:

 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 */.

 Variables and Data Types:


Variables store data and are declared with a specific data type.
Data types include primitives (e.g., int, double, char) and reference types (e.g., String, custom classes).
Variable names must start with a letter, underscore, or dollar sign, followed by letters, digits, underscores, or dollar signs.

 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.

 Classes and Objects:


Classes define blueprints for objects, while objects are instances of classes.
The new keyword is used to create objects.

 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:

Key Characteristics of Java Bytecode:


 Platform Independence: Java bytecode is designed to be platform-independent. This means that you can compile your
Java code once and run it on any platform that has a compatible JVM. This concept is often referred to as "Write Once,
Run Anywhere" (WORA).
 Intermediate Representation: Bytecode is an intermediate representation between the human-readable Java source
code and the machine code executed by the CPU. It's not specific to any particular architecture or operating system.
 Compact and Efficient: Java bytecode is designed to be compact and efficient. It's a relatively low-level representation
of the source code, consisting of instructions that the JVM can understand and execute quickly.
 Security and Portability: The use of bytecode enhances security, as the JVM performs bytecode verification to ensure
that the code adheres to certain safety constraints. Additionally, bytecode's portability across different platforms
contributes to the security of Java programs.
Execution of Bytecode:
When you run a Java program, the JVM comes into play. The JVM interprets or compiles the
bytecode and executes the instructions. There are different approaches to executing 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.

You might also like