0% found this document useful (0 votes)
8 views

lecture 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

lecture 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Lecture 1:

Introduction to Java

Object Oriented Programming


1
Introduction
Object-Oriented Programming or OOPs refers
to languages that uses objects in programming.
• What is Java?
• Java is a general-purpose, class-based, object-oriented programming
language designed for having lesser implementation dependencies. It is a
computing platform for application development. Java is fast, secure, and
reliable, therefore. It is widely used for developing Java applications in
laptops, data centers, game consoles, scientific supercomputers, cell
phones, etc.

• What is Java Platform?


• Java Platform is a collection of programs that help programmers to
develop and run Java programming applications efficiently. It includes an
execution engine, a compiler, and a set of libraries in it. It is a set of
computer software and specifications. James Gosling developed the Java
platform at Sun Microsystems, and the Oracle Corporation later acquired
it.

• 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.

• What is Java used for?


• Here are some important Java applications:
– It is used for developing Android Apps
– Helps you to create Enterprise Software
– Wide range of Mobile java Applications
– Scientific Computing Applications
– Use for Big Data Analytics
– Java Programming of Hardware devices
– Used for Server-Side Technologies like Apache, JBoss, GlassFish, etc.

3
Introduction
• History of Java Programming Language
• Here are important landmarks from the history of the Java language:

– The Java language was initially called OAK.


– Originally, it was developed for handling portable devices and set-top boxes.
Oak was a massive failure.
– In 1995, Sun changed the name to "Java" and modified the language to take
advantage of the burgeoning www (World Wide Web) development business.
– Later, in 2009, Oracle Corporation acquired Sun Microsystems and took
ownership of three key Sun software assets: Java, MySQL, and Solaris.

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.

• Components Of Java Programming Language


• A Java Programmer writes a program in a human-readable language
called Source Code.
• These computers or chips understand only one thing, which is called
machine language or code. These machine codes run at the CPU level.

5
Java Development kit
• 3 Java platform components:
– Java Development kit (JDK)
– Java Virtual Machine (JVM)
– Java Runtime Environment (JRE)

• Java Development kit (JDK)


– JDK is a software development environment used for making Java applications.
The full form of JDK is Java Development Kit. Java developers can use it on
Windows, macOS and Linux. JDK helps them to code and run Java programs.

• Why use JDK?


• Here are the main reasons for using JDK:
– JDK contains tools required to write Java programs and JRE to execute them.
– It includes a compiler, Java application launcher, Appletviewer, etc.
– Compiler converts code written in Java into byte code.

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.

• Why use JRE?


– JRE contains class libraries, JVM, and other supporting files. It does not
include any tool for Java development like a debugger, compiler, etc.
– It uses important package classes like math, swing, util, lang, awt, and runtime
libraries.
– If you have to run Java applets, then JRE must be installed in your system.

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)

• A version of the Java Runtime Environment, which incorporates a Java


Virtual Machine, is required to execute the bytecode and the Java library
packages. Thus a JRE must be present on any machine which is to run
Java programs.

• The Java bytecode is standard and platform independent and as JRE’s


have
• been created for most computing devices (including PCs, laptops, mobile
devices, mobile phones, internet devices etc) this makes Java programs
highly portable.

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

• Step 4) Select the PATH for Java installation and


click next.

• Step 5) Once installation is


complete click Close

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

• The CLASSPATH variable gives location of the Library Files.

• Let's look into the steps to set the PATH and CLASSPATH

• Step 1) Right Click on the My Computer


and Select the properties

13
How to set Environment Variables in
Java: Path and Classpath
• Step 2) Click on advanced system settings

• Step 3) Click on Environment Variables

14
How to set Environment Variables in
Java: Path and Classpath
• Step 4) Click on new Button of User variables

• Step 5) Type PATH in the Variable name.

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

• CLASSPATH = <JDK installation directory>\lib\tools.jar;

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.

• If you see a screen like below, Java is installed.

19
How to Download and Install Eclipse
to Run Java
• Following is a step by step guide to download and install Eclipse IDE:

• Step 1) Installing Eclipse

• Open your browser and type https://www.eclipse.org/

• Step 2) Click on "Download" button.

20
How to Download and Install Eclipse to Run Java

• Step 3) Click on "Download 64 bit" button

• Step 4) Click on "Download" button

21
How to Download and Install Eclipse to Run Java

• Step 4) Install Eclipse.


• Click on "downloads" in Windows file explorer.
• Click on "eclipse-inst-win64.exe" file.

• Step 5) Click on Run button

22
How to Download and Install Eclipse to Run Java

• Step 6) Click on "Eclipse IDE for Java Developers"

23
How to Download and Install Eclipse to Run Java

• Step 7) Click on "INSTALL" button

24
How to Download and Install Eclipse to Run Java

• Step 8) Click on "LAUNCH" button.

25
How to Download and Install Eclipse to Run Java

• Step 9) Click on "Launch" button.

26
How to Download and Install Eclipse to Run Java

• Step 10) Click on "Create a new Java project" link.

27
How to Download and Install Eclipse to Run Java

• Step 11) Create a new Java Project

• Write project name.


• Click on "Finish button".

28
How to Download and Install Eclipse to Run Java

• Step 12) Create Java Package.

• Goto "src".
• Click on "New".
• Click on "Package".

29
How to Download and Install Eclipse to Run Java

• Step 13) Writing package name.

• Write name of the package


• Click on Finish button.

30
How to Download and Install Eclipse to Run Java

• Step 14) Creating Java Class


• Click on package you have created.
• Click on "New".
• Click on "Class".

31
How to Download and Install Eclipse to Run Java

• Step 15) Defining Java Class.


• Write class name
• Click on "public static void
main (String[] args)" checkbox.
• Click on "Finish" button.

32
How to Download and Install Eclipse to Run Java

• Helloword.java file will be created as shown below:

• Step 16) Click on "Run" button.

33
How to Download and Install Eclipse to Run Java

• Output will be displayed as shown below.

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

– Declare a class with name A.


– Declare the main method public static void main(String args[]){
– Now Type the System.out.println("Hello World"); which displays the text Hello
World.

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.

public class MyFirstJavaProgram {


/* This is my first java program.
* This will print 'Hello World' as the output
* This is an example of multi-line comments.
*/

public static void main(String []args) {


// This is an example of single line comment
/* This is also an example of single line comment. */
System.out.println("Hello World");
}
}
• Output 41
• Hello World
Java User Input
• The Scanner class is used to get user input, and it is found in the java.util
package.
• To use the Scanner class, create an object of the class and use any of the
available methods found in the Scanner class documentation.
• In our example, we will use the nextLine() method, which is used to read
Strings:
• Example

import java.util.Scanner; // Import the Scanner class

class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");

String userName = myObj.nextLine(); // Read user input


System.out.println("Username is: " + userName); // Output user input
}
} 42
Java User Input
• In the example above, we used the nextLine() method, which is
used to read Strings. To read other types, look at the table below:

• 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);

System.out.println("Enter name, age and salary:");

// String input
String name = myObj.nextLine();

// Numerical input
int age = myObj.nextInt();
double salary = myObj.nextDouble();

// Output input by user


System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
44
}
Algo & Pseudo
• Algorithm
– a process or set of rules to be followed in problem-solving operations,
especially by a computer.
– An algorithm is a set of instructions designed to perform a specific task. This
can be a simple process, such as multiplying two numbers, or a complex
operation, such as playing a compressed video file. ... In computer
programming, algorithms are often created as functions.
– In essence, algorithms are simply a series of instructions that are followed, step
by step, to do something useful or solve a problem. You could consider a cake
recipe an algorithm for making a cake, for example.

• 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.

– This order and this strict structure is what enables us to communicate


effectively with a computer. Syntax is to code, like grammar is to English or any
other language. A big difference though is that computers are really exacting in
how we structure that grammar or our syntax.

– This syntax is why we call programming coding.

46

You might also like