Lab-1 Introduction To Java - 1148858255

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

Outline

1. Introduction to Java
2. Bytecode
3. JVM Architecture
4. Applications of Java
5. Types of Java Application
6. History of Java
7. Features of Java
8. Simple Java Program

2
Introduction to Java

• Java is a general purpose programming language.


• It is High Level language.
• Java was originally developed by James Gosling at Sun
Microsystems in 1995.
• Java is a language that is platform independent.
• A platform is the hardware and software environment in which a
programs run.
• Java has its own Java Runtime Environment (JRE) and API.

3
Introduction to Java (Cont…)
• Java code is once compiled, it can run on any platform without recompiling or
any kind of modification.
• “Write Once Run Anywhere”
• The aforementioned feature of Java is supported by Java Virtual Machine
(JVM).
• First, Java code is complied into bytecode. This bytecode gets interpreted on
different machines. Java bytecode is the result of the compilation of a Java program,
an intermediate representation of that program which is machine independent.

• Java versions:
• J2SE (JAVA TO STANDARD EDITION)
• J2EE (JAVA TO ENTERPRISE EDITION)
• J2ME (JAVA TO MICRO EDITION)

4
Introduction to Java (Cont…)
• JDK (JAVA DEVEOPMENT KIT): develop and run java application (developer,
environment to run)
• JRE (JAVA RUNTIME ENVIRONMENT): Just to run java application (client)
• JVM (Java Virtual Machine): Convert byte code into machine code

• JVM + library classes = JRE


• JRE + development tools = JDK

5
Java Code Execution Process
JAVA BYTE CODE
(.CLASS FILE)

JVM

OS

Note: JVM, is different for windows, Linux and mac OS (operating system)

COMPILER INTERPRETER
In a compiler, the process requires two In Interpreter It’s a one-step process in
steps in which firstly source code is which Source code is compiled and
translated to target program then executed. executed at the same time.
6
Bytecode
• When we write a program in Java, at first, the compiler compiles the program and
a bytecode is generated.

• When we wish to run this .class file on any other platform, we can do so.

• After the first compilation, the generated bytecode is now run by the Java Virtual
Machine and the processor is not in consideration (only java installation is
required).

• Implementation of JVM is based on stack.

7
Bytecode (Cont…)

Fig: Bytecode
8
JVM Architecture

Fig 2: JVM architecture

9
Applications of Java
• Approx. 3 Billion devices run java. Some of them are as follows:
• Desktop applications
• Web applications
• Enterprise applications (banking)
• Mobile
• Embedded system
• Smart card
• Robotics
• Games

10
Types of Java Application
• There are 4 type of java applications:
• Standalone Application: An application that install on each system (AWT and
Swing are used).
• Web Application: An application that runs on the server side and creates
dynamic page (servlet, jsp, etc are used).
• Enterprise Application: An application that is distributed in nature, such as
banking applications. It has the advantage of high level security and load
balancing.
• Mobile Application: An application that is created for mobile devices
(Android and Java ME are used).

11
History of Java
• James Gosling, Mike Sheridan, and Patrick Naughton initiated the
Java language project in June 1991. The small team of sun
engineers called Green Team.
• Firstly, it was called "Greentalk" by James Gosling and file
extension was .gt.
• After that, it was called Oak and developed as a part of the Green
project.
• In 1995, Oak was renamed as "Java“.
• JDK 1.0 released in 23 Jan 1996.

12
Features of Java
• There are many features of Java:
• Object-oriented
• Platform independent
• Secure
• Robust
• Portable
• Dynamic
• High performance
• Multithread
• Distribute

13
Simple Java Program
• To create a simple java program, you need to create a class that
contains main method.
class Hello
{
public static void main(String args[])
{
System.out.println(“Hello World”);
}
}
14
Simple Java Program (Cont…)
• Save: Save as Hello.java

• To compile: javac Hello.java

• To execute: java Hello

• Output: Hello World

15
Simple Java Program (Cont…)
• class keyword is used to declare a class.
• public keyword is an access modifier, which represents visibility. It means it is visible to
all.
• static is a keyword. If we declare any method as static, it is known as the static method.
The core advantage of the static method is that there is no need to create an object to invoke
the static method. The main method is executed by the JVM, so it doesn't require to create
an object to invoke the main method. Thus, it saves memory.
• void is the return type of the method. It means it doesn't return any value.
• main represents the starting point of the program.
• String args [] is an array of strings which stores arguments passed by command line while
starting a program.
• System.out.println() is used to print statement. Here, System is a class, out is the object of
PrintStream class, println() is the method of PrintStream class.
16
Simple Java Program (Cont…)

• The subscript notation in Java array can be used after type,


before the variable or after the variable.

➢public static void main(String[] args)

➢public static void main(String []args)

➢public static void main(String args[])

17
Simple Java Program (Cont…)
• Valid Java main method signature:

➢public static void main(String[] args)

➢public static void main(String []args)

➢public static void main(String args[])

➢public static void main(String... args)

➢static public void main(String[] args)


18
Simple Java Program (Cont…)
• Invalid Java main method signature:

❖public void main(String[] args)

❖static void main(String[] args)

❖public void static main(String[] args)

19
Simple Java Program (Cont…)
• Giving a semicolon at the end of a class is optional in Java.

class Hello
{
public static void main(String args[])
{
System.out.println(“Hello World”);
}
};
20
Class
• Class − A class can be defined as a template/blueprint that describes the
behavior/state that the object of its type support.

Syntax to declare a class:

class <class_name>{
field;
method;
}

21
Object
• Object − Objects have states and behavior.
• Example: A dog has states – color, name, breed as well as behavior – wagging the tail, barking,
eating. An object is an instance of a class.
• An object is an element (or instance) of a class; objects have the behaviors of their class. The
object is the actual component of programs, while the class specifies how instances are created
and how they behave.

Syntax:

Class _name object_name = new Class_name();

22
Method
• In Java, a method is like a function which is used to expose the behavior of an
object.
• Advantage of Method
• Code Reusability
• Code Optimization

Syntax:

<access modifier> <non access modifier><return-type><Method_name>(<Argument list>)


{

// method body

}
23
Example

24
Example (Cont..)
• In Java, a method is like a function which is used to expose the behavior of an
object.
• Advantage of Method Car object1=new Car();
Car object2=new Car();
• Code Reusability
object1.name=“Swift”;
• Code Optimization object1. model =“ZXI”;
public class Car { object1. colour =“White”;
String name; object1.Run();
String model; object1.Break();
String colour;
object2.name=“Alto”;
void Run() { object2. model =“LXI”;
} object2. colour =“Black”;
object2.Run();
void Break() { object2.Break();
} }
25
MCQs
1. Which component is used to compile, 3. Which statement is true about java?
debug and execute java program? a) Platform independent programming language
a) JVM b) Platform dependent programming language
b) JDK
c) Code dependent programming language
c) JIT
d) JRE d) Sequence dependent programming language

4. What is the extension of java code files?


2. Which component is responsible for a) .class
converting bytecode into machine specific b) .java
code?
c) .txt
a) JVM
d) .js
b) JDK
c) JIT 5. What is the extension of compiled java classes?
d) JRE a) .class b) .java
c) .txt d) .js
26
27

Comprehensive Examination 8/1/2022

You might also like