lecture 1
lecture 1
Introduction to Java
• https://www.youtube.com/watch?v=2Xa3Y4xz8_s
2
Introduction
• Java Definition and Meaning
• Java is a multi-platform, object-oriented, and network-centric language. It
is among the most used programming language. Java is also used as a
computing platform.
• It is considered as one of the fast, secure, and reliable programming
languages preferred by most organizations to build their projects.
3
Introduction
• History of Java Programming Language
• Here are important landmarks from the history of the Java language:
4
Components Of Java
• Java Features
• Here are some important Java features:
– It is one of the easy-to-use programming languages to learn.
– Write code once and run it on almost any computing platform.
– Java is platform-independent. Some programs developed in one machine can
be executed in another machine.
– It is designed for building object-oriented applications.
5
Java Development kit
• 3 Java platform components:
– Java Development kit (JDK)
– Java Virtual Machine (JVM)
– Java Runtime Environment (JRE)
6
Java Virtual Machine
• Java Virtual Machine (JVM):
– Java Virtual Machine (JVM) is an engine that provides a runtime environment
to drive the Java Code or applications.
– It converts Java bytecode into machine language. JVM is a part of the Java
Run Environment (JRE).
– In other programming languages, the compiler produces machine code for a
particular system. However, the Java compiler produces code for a Virtual
Machine known as Java Virtual Machine.
• Why JVM?
– JVM provides a platform-independent way of executing Java source code.
– It has numerous libraries, tools, and frameworks.
– Once you run a Java program, you can run on any platform and save lots of
time.
– JVM comes with JIT (Just-in-Time) compiler that converts Java source code
into low-level machine language. Hence, it runs faster than a regular
application.
7
Java Runtime Environment
• Java Runtime Environment (JRE)
– JRE is a piece of software that is designed to run other software. It contains the
class libraries, loader class, and JVM. In simple terms, if you want to run a
Java program, you need JRE.
– If you are not a programmer, you don't need to install JDK, but just JRE to run
Java programs.
https://youtu.be/G1ubVOl9IBw
8
Compiler, Interpreter, Byte Code
• Compilation
– The compiler converts the source code into machine code (at one go) for the
relevant hardware/OS combination.
– Strictly speaking there are two stages: compilation of the program units,
followed by linking when the complete executable program is put together
including the separate program units and relevant library code etc.
• Interpretation
– In interpretation an interpreter reads the source code and performs the actions
it specifies(translate line by line). No re-compilation is required after changing
the code, but the interpretation inflicts a significant impact on execution speed.
• Intermediate Code
– This model is a hybrid between the previous two. Compilation takes place to
convert the source code into a more efficient intermediate representation which
can be executed by a ‘run-time system’ more quickly than direct interpretation
of the source code. However, the use of an intermediate code which is then
executed by run-time system software allows the compilation process to be
independent of the OS/hardware platform. 9
Java Bytecode
• To run Java programs, we must first generate intermediate code (called
bytecode) using a compiler available as part of the Java Development Kit
(JDK)
10
How to Download & Install Java JDK 8
in Windows
• Following are steps to install Java in Windows
• Step 1) Go to https://www.oracle.com/java/technologies/javase-
downloads.html. Click on Download JDK. For java latest version.
• Step 2) Next,
– Accept License Agreement
– Download latest Java JDK for your version(32 or 64 bit) of java for Windows.
11
How to Download & Install Java JDK 8 in Windows
• Step 3) Once the download is complete, run the exe for install JDK. Click
Next
12
How to set Environment Variables in
Java: Path and Classpath
• The PATH variable gives the location of executables like javac, java etc. It
is possible to run a program without specifying the PATH but you will need
to give full path of executable like C:\Program Files\Java\jdk-
13.0.1\bin\javac A.java instead of simple javac A.java
• Let's look into the steps to set the PATH and CLASSPATH
13
How to set Environment Variables in
Java: Path and Classpath
• Step 2) Click on advanced system settings
14
How to set Environment Variables in
Java: Path and Classpath
• Step 4) Click on new Button of User variables
15
How to set Environment Variables in
Java: Path and Classpath
• Step 6) Copy the path of bin folder which is installed in JDK folder.
• Step 7) Paste Path of bin folder in Variable value and click on OK Button.
• Note: In case you already have a PATH variable created in your PC, edit the PATH variable to
• PATH = <JDK installation directory>\bin;%PATH%;
16
• Here, %PATH% appends the existing path variable to our new value
How to set Environment Variables in
Java: Path and Classpath
• Step 8) You can follow a similar process to set CLASSPATH.
• Note: In case you java installation does not work after installation, change
classpath to
17
How to set Environment Variables in
Java: Path and Classpath
• Step 9) Click on OK button
18
How to set Environment Variables in
Java: Path and Classpath
• Step 10) Go to command prompt and type javac commands.
19
How to Download and Install Eclipse
to Run Java
• Following is a step by step guide to download and install Eclipse IDE:
20
How to Download and Install Eclipse to Run Java
21
How to Download and Install Eclipse to Run Java
22
How to Download and Install Eclipse to Run Java
23
How to Download and Install Eclipse to Run Java
24
How to Download and Install Eclipse to Run Java
25
How to Download and Install Eclipse to Run Java
26
How to Download and Install Eclipse to Run Java
27
How to Download and Install Eclipse to Run Java
28
How to Download and Install Eclipse to Run Java
• Goto "src".
• Click on "New".
• Click on "Package".
29
How to Download and Install Eclipse to Run Java
30
How to Download and Install Eclipse to Run Java
31
How to Download and Install Eclipse to Run Java
32
How to Download and Install Eclipse to Run Java
33
How to Download and Install Eclipse to Run Java
34
Java Program Structure
35
Java Program Structure
36
A small Java Program
37
Your First Java Program
• Steps to Compile and Run first Java program
• Step 1) Open Eclipse
• Step 2) Create a Source Code for your Program
class A {
public static void main(String args[]){
System.out.println("Hello World");
}
}
38
Your First Java Program
• Step 3) Run
• https://youtu.be/I2wvhRUVNTM
39
System.out.println()
• System.out.println() in Java
• In Java, System.out.println() is a statement which prints the argument
passed to it. The println() method display results on the monitor.
• Example
• The following example, the println() method display the string in two
separate lines.
class Demo
{
public static void main(String args[])
{
System.out.println("Hello!");
System.out.println("Java");
}
}
Output
Hello!
Java 40
Comments
• Java supports single-line and multi-line comments very similar to C
and C++. All characters available inside any comment are ignored
by Java compiler.
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");
• Method Description
• nextBoolean() Reads a boolean value from the user
• nextByte() Reads a byte value from the user
• nextDouble() Reads a double value from the user
• nextFloat() Reads a float value from the user
• nextInt() Reads a int value from the user
• nextLine() Reads a String value from the user
• nextLong() Reads a long value from the user
43
Java User Input
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
// String input
String name = myObj.nextLine();
// Numerical input
int age = myObj.nextInt();
double salary = myObj.nextDouble();
• Pseudo code
– Pseudocode is an informal way of programming description that does not
require any strict programming language syntax or underlying technology
considerations.
– It’s simply an implementation of an algorithm in the form of annotations and
informative text written in plain English. It has no syntax like any of the
programming language and thus can’t be compiled or interpreted by the
computer.
45
Syntax
• What is Syntax in Programming?
– Syntax is the set of rules that define what the various combinations of symbols
mean. This tells the computer how to read the code. Syntax refers to a concept
in writing code dealing with a very specific set of words and a very specific
order to those words when we give the computer instructions.
46