3 - Java Intro
3 - Java Intro
Introduction to Java
kav@ucsc.cmb.ac.lk
Slides adapted from Mr. Shavindra Wickramathilaka
1
ILOs
▷ Describe “Why Java is necessary?”
▷ Describe Java Language
▷ Describe Java Virtual Machine
▷ Design a simple program in Java
2
What is PC?
▷ A computer is an electronic device capable of performing computations, and we all
know that it is composed of a monitor, keyboard, mouse, and memory to store
information.
▷ But the most important component of the computer is a PROCESSOR.
▷ Which does all thinking of computer, but the question is how the computer does this
thinking? How does it understand text, images, videos, etc.?
3
What is Assembly Language?
▷ The computer is an electronic device, and it can only understand electronic signals or
binary signals.
▷ For example, the 5-volt electronic signal may represent binary number 1 while 0 volts
may represent binary number 0.
▷ So your PC is continuously bombarded with these signals.
4
What is Assembly Language?
cont.
▷ Eight bits of such signals are group together to interpret Text, numerical and symbols.
▷ This is known as 8-bit computing.
▷ Current day processor is capable of decoding 64-bit time.
▷ But what is the relation of this concept with the programming language JAVA? Let
understand these as an example.
▷ Suppose if you want to tell the computer to add two number (1+2) which is
represented by some binary numbers (10000011), how are you going to tell the
computer?
▷ Yes, we going to use assembly language to get our code executed.
5
What is Assembly Language?
cont.
▷ "Assembly Language is the most elementary form of software development
languages."
▷ We are going to give the command to a computer in this format as shown below.
▷ Your code to add two numbers in this language would be in this order.
6
What is Assembly Language?
cont.
▷ But how are we going to do this? Back in 1950's when computers were huge and
consumed a great deal of power, you would convert your assembly code into
corresponding machine code to 1 and 0's using mapping sheets.
▷ Later this code will be punched into the machine cards and feed to the computer.
▷ The computer will read these code and execute the program. These would be a long
process then until ASSEMBLER came to help.
7
What are Assembler and
Compiler?
▷ With the advancement in technology i/o devices were invented, you could directly type
your program into the PC using a program called ASSEMBLER.
▷ It converts it into corresponding machine code (110001..) and feeds to your processor.
▷ So coming back to our example addition of (1+2), the assembler will convert this code
into machine code and give the output.
8
What are Assembler and
Compiler? cont.
▷ But alone assembler is not involved in this whole process; it also requires the compiler
to compile the long code into a small chunk of codes.
▷ With advancement in software development languages, this entire assembly code
could shrink into just one line printf (1+2) with the help of software called COMPILER.
▷ It is used to convert your c language code into assembly code, and the assembler
converts it into corresponding machine code, and this machine code will be
transmitted to the processor.
9
What are Assembler and
Compiler? cont.
▷ Though present-day compilers come bundled with assembler can directly convert your
higher language code into machine code.
▷ Now, suppose Windows operating system is running on this Intel processor, a
combination of Operating System plus the processor is called the PLATFORM.
10
Activity 1
1. What are the processors you know?
2. What are the OS s you know?
6868 4927
▷ https://www.menti.com/2hqrbkjy33
11
Activity 1
12
Activity 1
13
What are Assembler and
Compiler? cont.
▷ Examples for processors
○ Intel
○ AMD
○ Apple M
▷ Example for OS
○ Windows
○ MacOS
○ Linux
14
What are Assembler and
Compiler? cont.
▷ Now, with a change in processor, the assembly instructions will also change.
○ ADD - Intel
○ ADDITION - AMD
▷ And obviously, with a change in Operating System, the level and nature of O.S level
calls will also change.
▷ As a developer, I want my software program to work on all platforms available, to
maximize my revenues.
▷ So I would have to buy separate compilers which convert my print f command into the
native machine code.
15
What are Assembler and
Compiler? cont.
16
What is Java?
▷ Java is a programming language and a computing platform for application
development.
▷ It was first released by Sun Microsystem in 1995 and later acquired by Oracle
Corporation.
▷ It is one of the most used programming languages.
By Mark Anderson (work for hire for Sun Microsystems) - http://www.logoeps.com/java-eps-vector-logo/40925/, Fair use, https://en.wikipedia.org/w/index.php?curid=51298819
17
What is Java Platform?
▷ Java platform is a collection of programs that help to develop and run programs
written in the Java programming language.
▷ Java platform includes an execution engine, a compiler, and a set of libraries.
▷ JAVA is platform-independent language.
▷ It is not specific to any processor or operating system.
▷ To understand JAVA programming language, we need to understand some basic
concept of how a computer program can run a command and execute the action.
18
How Java Virtual Machine works?
By using Java Virtual Machine, this problem can be solved.
But how it works on different processors and O.S.
19
How Java Virtual Machine works?
cont
Step 1) The code to display addition of two numbers is System.out.println(1+2), and saved as
.java file.
Step 2) Using the java compiler the code is converted into an intermediate code called the
bytecode. The output is a .class file.
Step 3) This code is not understood by any platform, but only a virtual platform called the
Java Virtual Machine.
Step 4) This Virtual Machine resides in the RAM of your operating system. When the Virtual
Machine is fed with this bytecode, it identifies the platform it is working on and converts the
bytecode into the native machine code.
20
How Java Virtual Machine works?
cont
But what makes java lucrative is that code once compiled can run not only on all PC
platforms but also mobiles or other electronic gadgets supporting java.
Hence,
"Java is a programming language as well as a Platform"
21
How is Java Platform Independent?
▷ Like C compiler, Java compiler does not produce native executable code for a
particular machine.
▷ Instead, Java produces a unique format called bytecode.
▷ It executes according to the rules laid out in the virtual machine specification.
▷ Bytecode is understandable to any JVM installed on any OS.
▷ In short, the java source code can run on all operating systems.
22
JAVA Virtual Machine
23
What is JVM (Java Virtual
Machine)?
▷ JVM is an engine that provides runtime environment to drive the Java code or
applications.
▷ It converts Java bytecode into machine language
▷ JVM is a part of JRE (Java Run Environment)
▷ Other programming languages, the compiler produces machine code
▷ Java compiler produces code for a Virtual Machine known as Java Virtual Machine
▷ First, Java code is complied into bytecode. This bytecode gets interpreted on different
machines
▷ Between host system and Java source, Bytecode is an intermediary output
24
JVM Architecture
25
JVM Architecture cont
1) Class loader
The class loader is a subsystem used for loading class files.
It performs three major functions. Loading, Linking, and Initialization.
2) Method area
JVM Method Area stores class structures like metadata, the constant runtime pool, and the code
for methods.
3) Heap
All the Objects, their related instance variables, and arrays are stored in the heap.
This memory is common and shared across multiple threads.
26
JVM Architecture cont
4) JVM language stacks
Java language Stacks store local variables, and it’s partial results.
Each thread has its own JVM stack, created simultaneously as the thread is created.
A new frame is created whenever a method is invoked, and it is deleted when method invocation
process is complete.
5) PC registers
PC register store the address of the Java virtual machine instruction which is currently
executing.
In Java, each thread has its separate PC register.
6) Native method stacks
Native method stacks hold the instruction of native code depends on the native library.
It is written in another language instead of Java. 27
JVM Architecture cont
7) Execution engine
It is a type of software used to test hardware, software, or complete systems.
The test execution engine never carries any information about the tested product.
8) Native method interface
The Native Method Interface is a programming framework.
It allows Java code which is running in a JVM to call by libraries and native applications.
9) Native method libraries
Native Libraries is a collection of the Native Libraries(C, C++) which are needed by the Execution
Engine.
28
Software Code Compilation &
Execution process
In order to write and execute a software program, you need the following
29
C code compilation and
execution process
30
C code compilation and
execution process cont.
▷ f1() in a2.c
▷ f2() in a3.c
31
C code compilation and
execution process cont.
▷ All these files, i.e., a1.c, a2.c, and a3.c, is
fed to the compiler.
▷ Whose output is the corresponding
object files which are the machine
code.
32
C code compilation and
execution process cont.
▷ The next step is integrating all these
object files into a single .exe file with
the help of linker.
▷ The linker will put all these files
together and produces the .exe file.
33
C code compilation and
execution process cont.
▷ During program run, a loader program
will load a.exe into the RAM for the
execution.
34
Java code Compilation and Execution in Java VM
35
Java code Compilation and Execution in Java VM
cont
36
Java code Compilation and Execution in Java VM
cont
NOTE: JIT or Just-in-time compiler is the part of the Java Virtual Machine (JVM). It interprets part of the Byte Code that has
similar functionality at the same time.
37
Why is Java both Interpreted and
Compiled Language?
Programming languages are classified as,
▷ Higher Level Language
○ C++, Java
▷ Middle-Level Languages
○ C
▷ Low-Level Language
○ Assembly
▷ Machine Language.
38
Why is Java both Interpreted and
Compiled Language? cont
▷ A compiler is a program which converts a program from one level of language to
another.
○ Conversion of C++ program into machine code.
▷ The java compiler converts high-level java code into bytecode (which is also a type of
machine code).
▷ An interpreter is a program which converts a program at one level to another
programming language at the same level.
○ Conversion of Java program into C++
▷ In Java, the Just In Time Code generator converts the bytecode into the native
machine code which are at the same programming levels.
39
Why is Java slow?
The two main reasons behind the slowness of Java are
1. Dynamic Linking
Unlike C, linking is done at run-time, every time the program is run in Java.
2. Run-time Interpreter
The conversion of byte code into native machine code is done at run-time in Java
which furthers slows down the speed
The latest version of Java has addressed the performance bottlenecks to a great extent.
40
First Java Programme
41
How to Create Your First Java
Program
▷ You need the following 2 software to create your first Java Program
○ The Java SE Development Kit
■ Download and install JDK
▷ A Text Editor
○ It is not recommended to use an IDE
42
Step 1
▷ Open your favourite text editor
43
Step 1
▷ Add the following code
class HelloWorld {
public static void main(String args[]){
System.out.println("Hello World!");
}
}
44
Step 2
▷ Save the file as HelloWorld.java
45
Step 3
▷ Open the folder using the Terminal / Command Prompt
46
Step 3
▷ Run command “javac HelloWorls.java”
47
Step 4
▷ Run command “java HelloWorld”
48
About Java programs
▷ Case sensitive
▷ Filename should be an exact match to the class name
▷ public static void main(String[] args) is required
49
“
Thank You
50