Lab-1 Introduction To Java - 1148858255
Lab-1 Introduction To Java - 1148858255
Lab-1 Introduction To Java - 1148858255
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
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
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).
7
Bytecode (Cont…)
Fig: Bytecode
8
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
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…)
17
Simple Java Program (Cont…)
• Valid Java main method signature:
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.
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:
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:
// 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