Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Intro to Java

By
Gurudarshan K
What is Java
• Java is a widely used, high-level, object-oriented programming
language developed by Sun Microsystems (now owned by Oracle
Corporation). It was first released in 1995 and has since become one
of the most popular programming languages in the world

• Utilized for Android development, web development, cloud


applications, and much more.
History of Java
1.Java originated in the early 1990s at Sun Microsystems as the "Green Project."
2.It was officially introduced to the public on May 23, 1995.
3.Java's key feature was platform independence, enabled by the Java Virtual
Machine (JVM).
4.In 2006, Java was open-sourced, leading to the creation of OpenJDK.
5.Oracle acquired Sun Microsystems, gaining control of Java, in 2010.
6.Java continued to evolve with new language features and performance
enhancements.
7.Java remains a popular and influential programming language in various
domains today.
Example Program
public class HelloWorld {
public static void main(String[] args)
{
// This is the main method where the program starts executing.
// The following line prints "Hello, World!" to the console.
System.out.println("Hello, World!"); }
}
Steps to run the program
• Save the code above in a file named HelloWorld.java.

• Open a command prompt or terminal.

• Navigate to the directory where you saved HelloWorld.java.

• Compile the program using the javac command


• javac HelloWorld.java
• If there are no errors in your code, this will generate a file named HelloWorld.class.

• Run the compiled program using the java command:


• java HelloWorld
What is JDK
• The JDK, or Java Development Kit, is like a toolbox for programmers
who want to create Java applications. It provides everything you need
to write, compile, and run Java programs. Think of it as a collection of
tools that help you build software with the Java programming
language.
What’s inside in JDK
1.Compiler: The JDK includes a special program called a compiler that takes the
code you write in Java and turns it into a form that computers can understand and
execute.
2.Libraries: It comes with a set of pre-written code libraries, known as the Java
Standard Library, which contains many ready-made functions and classes that make
it easier to perform common tasks like working with text, numbers, and graphics.
3.Runtime Environment: The JDK also includes the Java Runtime Environment
(JRE), which is essential for running Java programs. It contains the Java Virtual
Machine (JVM), which executes your compiled Java code.
4.Development Tools: The JDK provides various development tools for debugging,
testing, and profiling your Java applications.
5.Documentation: It includes documentation to help you understand how to use Java
effectively, including official guides, tutorials, and reference materials.
JVM
• JVM(Java Virtual Machine) acts as a run-time engine to run Java
applications. JVM is the one that actually calls the main method
present in a java code
How JVM Works
• Compilation to Bytecode:
When you write Java code, you use a Java compiler (e.g., javac) to translate your human-readable
code into a special format called bytecode. Bytecode is a set of instructions that are not specific to
any particular computer or operating system.

• Class Loading:
The bytecode is stored in .class files. When you run a Java program, the JVM is responsible for
loading these .class files into memory. This process is known as class loading.
Class loading includes verifying the bytecode to ensure it adheres to Java's safety and security rules.

• Bytecode Verification:
The JVM performs bytecode verification to ensure that the loaded code won't violate security or
memory constraints. This step helps prevent malicious code from causing harm.
• Execution of Bytecode:
Once the bytecode is loaded and verified, the JVM starts executing it line by line. The bytecode
instructions are executed by the JVM's execution engine.
The JVM keeps track of the program's state, manages memory allocation and deallocation, and
handles exceptions.

• Just-In-Time (JIT) Compilation:


To improve performance, many modern JVM implementations use a technique called Just-In-
Time (JIT) compilation.
The JVM's JIT compiler translates some of the bytecode into native machine code specific to the
underlying hardware and caches it for reuse.
This native machine code can be executed much faster than interpreting bytecode, resulting in
better performance for frequently executed code.

Garbage Collection:
• The JVM includes a garbage collector that automatically manages memory by reclaiming
memory occupied by objects that are no longer in use.
• This helps prevent memory leaks and reduces the risk of memory-related errors.
Variable
• In Java, a variable is a named storage location that holds data that can
be manipulated and used throughout a program. Variables are a
fundamental concept in programming and are used to store values like
numbers, text, and objects. When you declare a variable, you give it a
name and specify its data type.
• Example:
int age; // Declares an integer variable named "age.“
int age = 30; // Declares and initializes an integer variable "age" with

the value 30.


Data Types
• Data types that allow you to store and manipulate different kinds of data, There are two types
Primitive and Non-Primitive Datatypes.
• Primitive data types:

1. Int: Represents a 32-bit signed integer. It's commonly used for storing whole numbers.

2. long: Represents a 64-bit signed integer. Used for larger integer values that can't be
accommodated by int.

3. float: Represents a 32-bit floating-point number. Used for numbers with decimal places.

4. double: Represents a 64-bit floating-point number. Provides higher precision than float.

5. char: Represents a single 16-bit Unicode character, such as a letter, digit, or symbol.

6. boolean: Represents a true or false value. Used for logical operations.


Non-Primitive Data Types
• class: Java allows you to create custom classes, which are reference
data types. Instances of these classes store data and behavior related
to specific objects.
• arrays: Arrays are collections of elements of the same data type. They
can be of any primitive or reference data type.
• Interfaces:
Thankyou

You might also like