Lecture 1
Lecture 1
• Java is an Object-Oriented
programming language developed by James
Gosling in the early 1990s.
• The team initiated this project to develop a
language for digital devices such as set-top
boxes, television, etc.
• Originally C++ was considered to be used in the
project but the idea was rejected for several
reasons.
• James Gosling and his team called their project
“Greentalk” and its file extension was .gt and
later became to known as “OAK”.
History of various Java versions
VERSION RELEASE DATE
JDK Beta 1995
JDK 1.0 January 1996
JDK 1.1 February 1997
J2SE 1.2 December 1998
J2SE 1.3 May 2000
J2SE 1.4 February 2002
J2SE 5.0 September 2004
JAVA SE 6 December 2006
JAVA SE 7 July 2011
JAVA SE 8 March 2014
JAVA SE 9 September 2017
JAVA SE 10 March 2018
JAVA SE 11 September 2018
JAVA SE 12 March 2019
Features of Java
• Object Oriented : In java everything is an Object.
• Platform independent: Unlike many other
programming languages including C and C++
when Java is compiled, it is not compiled into
platform specific machine, rather into platform
independent byte code.
• Simple :Java is designed to be easy to learn. If
you understand the basic concept of OOP java
would be easy to master.
• Secure : With Java's secure feature it enables to
develop virus-free, tamper-free systems.
• Architectural- neutral :Java compiler generates an
architecture-neutral object file format which
makes the compiled code to be executable on
many processors.
• Portable :being architectural neutral and having
no implementation dependent aspects of the
specification makes Java portable.
• Robust :Java makes an effort to eliminate error
prone situations by emphasizing mainly on
compile time error checking and runtime checking.
• Multi-threaded : With Java's multi-threaded
feature it is possible to write programs that can do
many tasks simultaneously.
• Interpreted :Java byte code is translated on
the fly to native machine instructions and is
not stored anywhere.
• Distributed :Java is designed for the
distributed environment of the internet.
Difference between JDK, JRE and JVM
JVM
• JVM (Java Virtual Machine) is an abstract
machine. It is a specification that provides
runtime environment in which java byte code
can be executed.
• JVMs are available for many hardware and
software platforms. JVM, JRE and JDK are
platform dependent because configuration of
each OS differs. But, Java is platform
independent.
JRE
• JRE is an acronym for Java Runtime
Environment.
• It is used to provide runtime environment. It is
the implementation of JVM.
• It physically exists.
• It contains set of libraries + other files that
JVM uses at runtime.
JRE
JDK
class OperatorExample{
public static void main(String args[]){
int x=10;
System.out.println(x++);//10 (11)
System.out.println(++x);//12
System.out.println(x--);//12 (11)
System.out.println(--x);//10
}}
Output:
10
12
12
10
class Operator
{
public static void main(String args[])
{
int a=10;
int b=10;
System.out.println(a++ + ++a);//10+12=22
System.out.println(b++ + b++);//10+11=21
}
}
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0
}}
/* Arirhmetic operator...........................*/
Import java.util.Scanner;
class Operator {
public static void main(String args[]){
int a, b;
System.out.print(“Enter two number”);
Scanner o=new Scanner(System.in);
a=o.nextInt();
b=o.nextInt();
System.out.println(“Addition” +(a+b));
System.out.println(“Sub” +(a-b));
System.out.println(“Multi” +(a*b));
System.out.println(“Div” +(a/b));
System.out.println(“Rem” +(a%b));
}}
/*Relational Operator.................................*/
Import java.util.Scanner;
class Relational {
public static void main(String args[]){
int a, b;
System.out.print(“Enter two number”);
Scanner o=new Scanner(System.in);
a=o.nextInt();
b=o.nextInt();
System.out.println(“True/False” +(a<b));
System.out.println(“True/False” +(a>b));
System.out.println(“True/False” +(a<=b));
System.out.println(“True/False” +(a=>b));
System.out.println(“True/False” +(a==b));
System.out.println(“True/False” +(a!=b));
}}
Java AND Operator Example:
Logical && and Bitwise &
}}
/*Assignment Operator...................*/
class Assignment
{
public static void main(String args[])
{
Int a;
a=10; //simple assignment
System.out.println(a);
a+=10; // compound assignment
System.out.println(a);
a-=10; // compound assignment
System.out.println(a);
}}
Java Ternary Operator
class OperatorExample{
public static void main(String args[]){
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}}
Output:
2
Java Bit wise Operator
class Bitwise {
public static void main(String[] args)
{
int a=5,b=7;
System.out.println(“AND” +(a&b));
System.out.println(“OR” +(a|b));
System.out.println(“XOR” +(a^b));
}
}
Java IF Statement
class simple
{
public static void main(String[] args)
{
int age;
System.out.print(“Enter age”);
Scanner r=new Scanner(System.in);
age=r.nextInt();
If(age>=18)
{
System.out.println(“Eligible for vote”);
}
System.out.print(“Thank you”);
}}
Java IF-Else Statement
Import java.util.Scanner;
class IFElse
{
public static void main(String[] args)
{
int n;
System.out.print(“Enter any number”);
Scanner r=new Scanner(System.in);
n=r.nextInt();
If(age>=0)
{
System.out.println(“Positive number”);
}
else
{
System.out.print(“- number”);
}
}}
Java ElseIF-ladder Statement
Import java.util.Scanner;
class ElseIF
{
public static void main(String[] args)
{
int marks;
System.out.print(“Enter marks”);
Scanner r=new Scanner(System.in);
marks=r.nextInt();
If(marks>90)
{
System.out.println(“Topper”);
}
elseif(marks<90 && marks>=60)
{
System.out.print(“First”);
}
else
{
System.out.print(“Second”);
}
}}
Java Nested if statement
• The nested if statement represents the if block within another if block. Here, the
inner if block condition executes only when outer if block condition is true.
• //Java Program to demonstrate the use of Nested If Statement.
• public class JavaNestedIfExample2 {
• public static void main(String[] args) {
• //Creating two variables for age and weight
• int age=25;
• int weight=48;
• //applying condition on age and weight
• if(age>=18){
• if(weight>50){
• System.out.println("You are eligible to donate blood");
• } else{
• System.out.println("You are not eligible to donate blood");
• }
• } else{
• System.out.println("Age must be greater than 18");
• }
• } }
• Java Switch Statement
• The Java switch statement executes one statement from multiple
conditions. It is like if-else-if ladder statement. The switch statement
works with byte, short, int, long, enum types, String and some wrapper
types like Byte, Short, Int, and Long. Since Java 7, you can use strings in
the switch statement.
• In other words, the switch statement tests the equality of a variable
against multiple values.
• There can be one or N number of case values for a switch expression.
• The case value must be of switch expression type only. The case value
must be literal or constant. It doesn't allow variables.
• The case values must be unique. In case of duplicate value, it renders
compile-time error.
• The Java switch expression must be of byte, short, int, long (with its
Wrapper type), enums and string.
• Each case statement can have a break statement which is optional. When
control reaches to the break statement, it jumps the control after the
switch expression. If a break statement is not found, it executes the next
case.
• The case value can have a default label which is optional.
public class SwitchExample {
public static void main(String[] args) {
//Declaring a variable for switch expression
int number=20;
//Switch expression
switch(number){
//Case statements
case 10: System.out.println("10");
break;
case 20: System.out.println("20");
break;
case 30: System.out.println("30");
break;
//Default case statement
default:System.out.println("Not in 10, 20 or 30");
}
}
}