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

Lecture 2 Java Basics

The document provides an overview of Java programming, detailing its history, development phases, and the role of the Java Development Kit (JDK), Java Runtime Environment (JRE), Java Virtual Machine (JVM), and Just-In-Time (JIT) compiler. It explains the structure of Java programs, including classes, methods, and the use of the Scanner class for user input. Additionally, it highlights Java's platform independence and its compilation process, emphasizing that Java is both a compiled and interpreted language.

Uploaded by

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

Lecture 2 Java Basics

The document provides an overview of Java programming, detailing its history, development phases, and the role of the Java Development Kit (JDK), Java Runtime Environment (JRE), Java Virtual Machine (JVM), and Just-In-Time (JIT) compiler. It explains the structure of Java programs, including classes, methods, and the use of the Scanner class for user input. Additionally, it highlights Java's platform independence and its compilation process, emphasizing that Java is both a compiled and interpreted language.

Uploaded by

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

OBJECT ORIENTED

PROGRAMMING
JAVA
BASICS
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.

◾ Initially designed for small, embedded systems in electronic appliances like set-top
boxes.

◾ Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt.
◾ After that, it was called Oak and was developed as a part of the Green project. The
file extension was .o c k
HISTORY OF JAVA

◾ Why Oak? Oak is a symbol of strength and chosen as a national tree of many
countries like the U.S.A.,
France, Germany, Romania, etc.

◾ In 1995, Oak was renamed as "Java" because it was already a trademark by Oak
Technologies.

◾ Java is an island of Indonesia where the first coffee was produced (called java coffee). It is
a kind of
espresso bean. Java name was chosen by James Gosling while having coffee near his
office.
THE JAVA LANGUAGE
◾ Java programs
◾ Consist of pieces called classes
◾ Classes contain methods (which perform tasks) and data
◾ Class libraries
◾ Also known as Java API (Applications Programming Interface)
◾ Rich collection of predefined classes, which you can use
◾ Two parts to learning Java
◾ Learning the language, so you can create your own classes
◾ Learning how to use the existing classes in the libraries
BASICS OF A TYPICAL JAVA ENVIRONMENT
 Java programs have five phases

1. Edit
◾ Use an editor to type Java program
◾ Eclipse/ NetBeans / VSCode
◾ .java extension: for example MyProgram.java
2. Compile
◾ Translates program into bytecodes, understood by Java
interpreter
◾ javac command: javac MyProgram.java

BASICS OF A TYPICAL JAVA ENVIRONMENT
3.Loading

◾ Class loader transfers .class file into main memory

◾ Classes loaded and executed by interpreter with java command


◾ java MyProgram
4.Verify

◾ Bytecode verifier makes sure bytecodes are valid and do not


violate security
◾ Java must be secure (viruses)

5.Execute

◾ Computer interprets program one bytecode at a time

◾ Performs actions specified in program


Program is created in
Phase 1 Editor Disk the editor and stored
on disk.
Compiler creates
Phase 2 Compiler Disk bytecodes and
Primary stores
them on disk.
Memory
Phase 3 Class Loader

Class loader puts


Disk bytecodes in
..
.. memory.
..
Primary

Phase 4 Bytecode Verifier Memory


Bytecode verifier
confirms that all
bytecodes are valid
and do not violate
.. Java’s security
.. restrictions.
..
Primary
Interpreter Memory
Phase 5 Interpreter reads
bytecodes and
translates them into a
language that the
computer can
.. understand, possibly
.. storing data values as
.. the program executes.
A C++ PROGRAM COMPILATION
JAVA PROGRAM COMPILATION

Interpreter

• The JVM is integrated into the JDK. When you run a Java program, the JDK uses
the JVM to execute the bytecode, making your program functional.
JDK: Provides a platform for developing Java
applications. Develop and compiles program.
JRE: Loads bytecode into main memory & Runs Java
programs.
JVM: Loads, verifies bytecode and Executes When I said "platform-independent" in the context of the
bytecode in a platform-independent environment. JVM, I meant that the JVM can run Java bytecode on any
JIT: Interpreted Java bytecode into machine code at device or operating system that has a JVM, without the need
runtime. Also helps JVM to find OS. for recompilation of the code.
JDK: Provides a platform for developing Java
applications. Develop and compiles program.
JRE: Loads bytecode into main memory & Runs Java
programs.
JVM: Loads, verifies bytecode and Executes
bytecode in a platform-independent environment.
JIT: Interpreted Java bytecode into machine code at
runtime. Also helps JVM to find OS.
JD
K

◾ Define the JDK, JRE, JVM and JIT concepts and its
purpose

◾ Why Java is called platform independent language?


JR
E
When I said "platform-independent" in the context of the
JVM, I meant that the JVM can run Java bytecode on any
device or operating system that has a JVM, without the need
JV for recompilation of the code.
M
JIT
JAVA A COMPILER OR INTERPRETER
◾ Is Java a compiler or interpreter Language?

◾ Java is a compiled programming language, but rather than compile straight to


executable machine code, it compiles to an intermediate binary form called
JVM byte code.The byte code is then compiled and/or interpreted to run the
program.

◾ W hy Java is both compiler and interpreter language?

.exe on Windows
◾ Java compiler compiles the source code into bytecode. JVM i.e. Java .bin or .elf on Linux
virtual machine is an interpreter which interprets the byte code. .macho on macOS
Bytecode make Java a platform independent language
Source Code Byte code Machine Code

‘.java’ ‘.class’
A SIMPLE PROGRAM: PRINTING A LINE OF TEXT

The main function is the entry


point of the program, where
execution begins.
JAVA PROGRAM EXAMPLE

◾ public:
◾ public keyword is an access modifier which represents visibility, it means it is visible to all.
◾ To call by JVM from anywhere.
◾ This makes the main method public that means that we can call the method from outside the
class.
◾ static:
◾ static is a keyword, if we declare any method as static, it is known as static method.
◾ The core advantage of static method is that there is no need to create object to invoke the static
method.
◾ The main method is executed by the JVM, so it doesn't require to create object to invoke the
main method. So it saves memory.
String[] args:

◾ When a program is run, the operating system passes command-line arguments to the
main function, so that function could work properly.
 There is no as such use of this argument in main function but this must be
included as it is a signature to satisfy the Java compiler.

◾ Here we are defining a String array to pass arguments at command line.


args is the variable name of the String array. It can be changed to
anything such as String [] a etc.
A SIMPLE PROGRAM: PRINTING A LINE OF TEXT

Program
O utput
EXAMPLE 2
/* Here is another short
example. C all this file
"Example2.java". */ ◾ Java is case-sensitive.
class Example2
{
public static void main(String args[])
{
int num; / this declares a variable called
num = num
100; / this assigns num the value
System.o 100
ut.println
("This is Output
num: " + This is num: 100
num);
The value of num * 2 is
num =
num * 2; 200
A CLOSE LOOK AT EXAMPLE2

System.out.println("This is num: " +


num);
◾ This line outputs the value of num preceded by the string “This is
num: ”
◾ In this statement, the plus sign causes the value of num to be
appended to the string that precedes it, and then the resulting
string is output.
◾ Using the + operator, we can print together as many items as we
want within a single println() statement.
System.out.println("This is num: " + num+ “Hello
World”);
A CLOSE LOOK AT EXAMPLE2
System.out.print("The value of num * 2 is
");
◾ System.out.println(num);
The next two lines compares print() and println() methods.
◾ First, the built-in method print() is used to display the string “The value of num * 2 is
”.
◾ This string is not followed by a new line.
◾ This means when the next output is generated, it will start on the same line.
◾ The print() method is just like println(), except that it does not output a newline
character after each call.

◾ Both print() and println() can be used to output values of any of Java’s built-in types.
Lab 1. Activity 1:
INPUT A N D SYSTEM.IN

◾ interactive program: Reads input from the console.


◾ While the program runs, it asks the user to type input.
◾ The input typed by the user is stored in variables in the
code.

◾ Scanner: An object that can read input from many sources.


◾ Communicates with System.in (the opposite of
System.out)
How To Take Input From User in JAVA?
Built-in Packages (packages from the Java API)
User-defined Packages (create your own
Package packages)
Class

import java.util.Scanner;
Scanner obj = new Scanner(System.in)

Object of Scanner class Constructor

int age = obj.nextInt();

reads an int number from the user


SCANNER CLASS

◾ The Scanner class is mainly used to get the user input


◾ It belongs to the java.util package.

◾ In order to use the Scanner class, you can create an object of the class
and use any of the Scanner class methods.
◾ Scanner class allows user to take input from console.
◾ System.in is passed as a parameter in Scanner class.

◾ It tells the java compiler that system input will be provided through
console(keyboard)
SCANNER SYNTA X

◾ The Scanner class is found in the java.util


package.

import java.util.*; // so you can use Scanner

◾ Constructing a Scanner object to read console input:

Scanner name = new


Scanner(System.in);

◾ Example:
Scanner console = new Scanner(System.in);
SCANNER METHODS
Method Description
nextInt() reads an int from the user and returns it
nextDouble() reads a double from the user
next() reads a one-word String from the user
nextLine() reads a one-line String from the user
A static method in Java belongs to the class rather than
to any specific object of the class. This means you can
call a static method without creating an object of the
class.

We don’t need to call static function via class name if


there is only 1 class in your program and main
function is within the same class.
SCANNER EXAMPLE
import java.util.*; // so that I can use Scanner

public class ScannerMultiply {


public static void main(String[] args) {
Scanner console = new Scanner(System.in);

System.out.print("Please type two numbers: ");


int num1 = console.nextInt();
int num2 = console.nextInt();

int product = num1 * num2;


System.out.println("The product is " + product);
}
}
◾ Output (user input underlined):

Please type two numbers: 8 6


The product is 48
◾ The Scanner can read multiple values from one line.
Few more Libraries

Check Formatting Date and Time here:


https://www.w3schools.com/java/java_date.asp

You might also like