Lecture 2 Java Basics
Lecture 2 Java Basics
PROGRAMMING
JAVA
BASICS
HISTORY OF JAVA
◾ James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project
in June 1991.
◾ 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
5.Execute
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
.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
◾ 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.
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
◾ 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
import java.util.Scanner;
Scanner obj = new Scanner(System.in)
◾ 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
◾ 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.