KEMBAR78
Object oriented programming_Unit1_Introduction.ppt
CSE3391: Object Oriented
Programming
What is object-oriented
programming?
• Object-oriented programming (OOP) is a
computer programming model that
organizes software design around data, or
objects, rather than functions and logic. An
object can be defined as a data field that has
unique attributes and behavior
Overview of OOP
• The object-oriented programming (called
"OOP") is a programming paradigm in which
the programs are organized as sets of objects.
Each object represents an instance of a given
class, all the classes being members of a class
hierarchy unified by inheritance relationships.
Therefore, objects are used by the OOP.
• Each object is an instance of a given class.
• The classes are linked between themselves by
the inheritance relationships.
Object-Oriented Programming
Paradigm
• The major motivating factor in the invention of object-oriented
approach is to remove some of the flaws encountered in the
procedural approach.
• OOP treats data as a critical element in the program development
and does not allow it to flow freely around the systems.
• It ties data more closely to the functions that operate on it, and
protects it from accidental modification from outside functions.
• OOP allows decomposition of a problem into a number of entities
called objects and then builds data and functions around these objects.
• The data of an object can be accessed only by the function associated
with that object.
• However, functions of one object can access the the functions of other
objects.
Object Oriented Programming
Software Engineering Department
The Object-Oriented Paradigm
Features of Object-Oriented Programming
• Objects
• Classes
• Data Abstraction
• Data Encapsulation
• Inheritance
• Polymorphism
• Message passing
Object Oriented Programming
Software Engineering Department
Classes
• User defined data types on which objects are
created.
• Objects with similar properties and methods are
grouped together to form a Class.
• For example, mango,
apple and orange are
members of class fruit.
Object Oriented Programming
Software Engineering Department
Class is defined by three elements.
1.a unique class name
2.data members or attributes
3.methods
Class Student
{
int rollno;
char name[10];
Void getdata()
{
}
Void display()
{
}
Object Oriented Programming
Software Engineering Department
Objects
• Run time entities that may represent a person, place,
bank account or any item.
• Each object contains data, and code to manipulate
the data.
• Data represents the attributes of that object and
functions represents the behaviour of that object
Student s;
s.rollno=101//state
s.name=“ram”//state or attribute
s.getdata();//behaviour
Objects Contd.
Object Oriented Programming
Software Engineering Department
Data Abstraction
• Abstraction refers to the act of representing
essential features without including the background
details or explanations.
• For class Person
• Person name,dateofbirth, age can be taken
• What he or she wears,whether they reside,whether
they married or not need not be included
Object Oriented Programming
Software Engineering Department
Data Encapsulation
• Wrapping of data and functions into a single unit is
known as data encapsulation.
• Data is not accessible to the outside world, only
those functions which are wrapped in the class can
access it.
Object Oriented Programming
Software Engineering Department
classStudent
Data members
Rollno
Name
Method
Inputdata()
Display()
Inheritance
• Process of deriving a new class from an
existing one is called Inheritance.
• The existing class is known as base class,
parent class or super class.
• The new class that is formed is called derived
class, child class or sub class.
• Derived class has all the features of the base
class plus it has some extra features also.
Object Oriented Programming
Software Engineering Department
Example of Inheritance
Automobile
Two Wheeler Three Wheeler Four Wheeler
Scooter Motor Bike Auto Rickshaw Car Bus
Object Oriented Programming
Software Engineering Department
Types of Inheritance
Types of Inheritance
• Single level
• Multi level
• Multiple
• Hierarchical
• Hybrid
Object Oriented Programming
Software Engineering Department
Polymorphism
• Ability to take more than one form.
• Two types of Polymorphism
• compile Time Polymorphism
– Method Overloading
• Runtime Polymorphism
– Method Overriding
Introduction
 Java is a Programming language and a Platform.
 Java is a high level, robust, secured and object-oriented
programming language.
 Developed by Sun Microsystems (James Gosling).
Platform: Any hardware or software environment in
which a program runs, is known as a platform.
Where it is Used?
 According to Sun, 3 billion devices run java.
• Desktop Applications such as acrobat reader, media player,
antivirus etc.
• Web Applications such as irctc.co.in, javatpoint.com etc.
• Enterprise Applications such as banking applications.
• Mobile Applications
• Embedded System, Smart Card, Robotics, Games etc.
Characteristics/Features of Java/Buzz
Words of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
There are 3 Java Platform Editions:
1. Java 2 Platform, Standard Edition (J2SE)
• Core Java Platform targeting applications running on
workstations
2. Java 2 Platform, Enterprise Edition (J2EE)
• Component-based approach to developing distributed,
multi-tier enterprise applications
3. Java 2 Platform, Micro Edition (J2ME)
• Targeted at small, stand-alone or connectable consumer
and embedded devices
Brief History of Java
• In 1990, Sun Microsystems began an internal project known as
the Green Project to work on a new technology.
• In 1992, the Green Project was spun off and its interest directed
toward building highly interactive devices for the cable TV
industry. This failed to materialize.
• In 1994, the focus of the original team was re-targeted, this time
to the use of Internet technology. A small web browser called
HotJava was written.
• Oak was renamed to Java after learning that Oak had already
been trademarked.
• In 1995, Java was first publicly released.
• In 1996, Java Development Kit (JDK) 1.0 was released.
• In 2002, JDK 1.4 (codename Merlin) was released, the most
widely used version.
• In 2004, JDK 5.0 (codename Tiger) was released.
• The latest version of java is jdk 8.0.
Features of Java/Java Buzz
Words(12)
1. Simple: According to Sun, Java language is
simple because syntax is based on C++ and
there is Automatic Garbage Collection in java.
2. Object-oriented: java is a true object
oriented language. Almost everything in java
is an object. All program ode and data reside
within objects and classes.
Basic concepts of OOPs are:
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
3. Platform Independent:
• A platform is the hardware or software environment in which a program
runs.
• There are two types of platforms software-based and hardware-based.
Java provides software-based platform.
• The Java platform differs from most other platforms in the sense that it is
a software-based platform that runs on the top of other hardware-based
platforms. It has two components:
• Runtime Environment
• API(Application Programming Interface)
• Java code can be run on multiple platforms e.g. Windows, Linux, Sun
Solaris, Mac/OS etc. Java code is compiled by the compiler and converted
into bytecode. This bytecode is a platform-independent code because it
can be run on multiple platforms i.e. Write Once and Run
Anywhere(WORA).
4. Secured: Java is secured because:
• No explicit pointer
• Java Programs run inside virtual machine sandbox
Java virtual machine is a virtual machine that enables
computer to run java programs
It is a specification that provides runtime environment in
which java byte code an be executed.
5. Robust: Robust simply means strong. Java uses strong
memory management. There is automatic garbage
collection in java. There is exception handling and type
checking mechanism in java. All these points makes
java robust.
6. Architecture-neutral: There is no implementation
dependent features e.g. size of primitive types is fixed.
• In C programming, int data type occupies 2 bytes of
memory for 32-bit architecture and 4 bytes of memory
for 64-bit architecture. But in java, it occupies 4 bytes
of memory for both 32 and 64 bit architectures.
7. Portable: java programs can be moved from one
compute to another anywhere anytime.We may
carry the java bytecode to any platform.
8. High-performance: Java is faster than traditional
interpretation since byte code is "close" to native
code still somewhat slower than a compiled
language (e.g., C++)
9. Distributed: We can create distributed
applications in java. RMI and EJB are used for
creating distributed applications. We may access
files by calling the methods from any machine on
the internet.
10. Multi-Threaded: A thread is like a separate
program, executing concurrently.
• We can write Java programs that deal with
many tasks at once by defining multiple
threads.
• Eg:we can listen to an audio clip while
scrolling a page
• The main advantage of multi-threading is that
it doesn't occupy memory for each thread. It
shares a common memory area.
C++ Vs Java
JDK Versions
• JDK Alpha and Beta (1995)
• JDK 1.0 (23rd Jan, 1996)
• JDK 1.1 (19th Feb, 1997)
• J2SE 1.2 (8th Dec, 1998)
• J2SE 1.3 (8th May, 2000)
• J2SE 1.4 (6th Feb, 2002)
• J2SE 5.0 (30th Sep, 2004)
• Java SE 6 (11th Dec, 2006)
• Java SE 7 (28th July, 2011)
• Java SE 8 (18th March, 2014)
Understanding JDK, JRE and JVM
Understanding JDK & JRE
JDK
 JDK is an acronym for Java Development Kit.
 It physically exists. It contains JRE and development tools.
JRE
 JRE is an acronym for Java Runtime Environment.
 It is the implementation of JVM and used to provide runtime
environment.
 It contains set of libraries and other files that JVM uses at
runtime.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Understanding 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.
 The JVM performs following main tasks:
• Loads code
• Verifies code
• Executes code
• Provides runtime environment
Internal Architecture of JVM
How Java is Platform-independent?
How Java is Platform-independent?
How Java is Platform-independent?
 The source code (program) written in java is saved as a
file with .java extension.
 The java compiler “javac” compiles the source code and
produces the platform independent intermediate code
called BYTE CODE. It is a highly optimized set of
instructions designed to be executed by the JVM.
How Java is Platform-independent?
 The byte code is not native to any platform because java
compiler doesn’t interact with the local platform while
generating byte code.
 It means that the Byte code generated on Windows is
same as the byte code generated on Linux for the same
java code.
 The Byte code generated by the compiler would be saved
as a file with .class extension. As it is not generated for
any platform, can’t be directly executed on any CPU.
First Java Program
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
To compile: javac Simple.java
To execute: java Simple
Save the above file as Simple.java.
Output: Hello Java
• Parameters used in First Java Program
• Let's see what is the meaning of class, public, static, void, main, String[],
System.out.println().
• class keyword is used to declare a class in Java.
• public keyword is an access modifier that represents visibility. It means it is
visible to all.
• static is a keyword. If we declare any method as static, it is known as the
static method. The core advantage of the static method is that there is no
need to create an object to invoke the static method. The main() method is
executed by the JVM, so it doesn't require creating an object to invoke the
main() method. So, it saves memory.
• void is the return type of the method. It means it doesn't return any value.
• main represents the starting point of the program.
• String[] args or String args[] is used for command line argument.
• System.out.println() is used to print statement. Here, System is a class, out
is an object of the PrintStream class, println() is a method of the PrintStream
class.
Datatypes
• Data Types in Java
• Data types specify the different sizes and values that can be
stored in the variable. There are two types of data types in
Java:
• Primitive data types: The primitive data types include
boolean, char, byte, short, int, long, float and double.
• Non-primitive data types: The non-primitive data types
include Classes, Interfaces, and Arrays
Java Primitive Data Types
• In Java language, primitive data types are the
building blocks of data manipulation. These
are the most basic data types available in
Java language
• There are 8 types of primitive data types:
• boolean data type
• byte data type
• char data type
• short data type
• int data type
• long data type
• float data type
• double data type
Datatypes
Boolean Data Type
• The Boolean data type is used to store only
two possible values: true and false. This data
type is used for simple flags that track
true/false conditions.
Example:
• Boolean one = false
Byte Data Type
• The byte data type is an example of primitive
data type. It is an 8-bit signed two's
complement integer. Its value-range lies
between -128 to 127 (inclusive). Its minimum
value is -128 and maximum value is 127. Its
default value is 0.
• byte a = 10, byte b = -20
Short Data Type
• The short data type is a 16-bit signed two's
complement integer. Its value-range lies
between -32,768 to 32,767 (inclusive). Its
minimum value is -32,768 and maximum value
is 32,767. Its default value is 0.
• Example:
• short s = 10000, short r = -5000
Int Data Type
• The int data type is a 32-bit signed two's
complement integer. Its value-range lies
between - 2,147,483,648 (-2^31) to
2,147,483,647 (2^31 -1) (inclusive). Its minimum
value is - 2,147,483,648and maximum value is
2,147,483,647. Its default value is 0.
• Example:
• int a = 100000, int b = -200000
Long Data Type
• The long data type is a 64-bit two's
complement integer. Its value-range lies
between -9,223,372,036,854,775,808(-2^63)
to 9,223,372,036,854,775,807(2^63 -1)(
• Example:
• long a = 100000L, long b = -200000L
Float Data Type
• The float data type is a single-precision 32-bit
IEEE 754 floating point.Its value range is
unlimited. It is recommended to use a float
(instead of double) if you need to save
memory in large arrays of floating point
numbers.
• float f1 = 234.5f
Double Data Type
• The double data type is generally used for
decimal values just like float.
• double d1 = 12.3
Char Data Type
• The char data type is a single 16-bit Unicode
character. Its value-range lies between '
u0000' (or 0) to 'uffff' (or 65,535
inclusive).The char data type is used to store
characters.
• Example:
• char letterA = 'A'
Variables
• A variable is the name of a reserved area
allocated in memory. In other words, it is a
name of the memory location. It is a
combination of "vary + able" which means its
value can be changed.
• There are three types of variables in java:
local, instance and static.
JAVA-NLP Lecture series
Variables
• A variables can be considered as a name given to
the location in memory where values are stored.
• One syntax of variable declaration
dataType variableName;
dataType variableName;
• Do you imagine that the variable can change its
value – yes, that is why it is called as variable
JAVA-NLP Lecture series
Rules for Naming Variable
• Use only the characters 'a' through 'z', 'A' through 'Z', '0'
through '9', character '_', and character '$'. A name can’t
contain space character.
• Do not start with a digit. A name can be of any length.
• Upper and lower case count as different characters. So
SUM and Sum are different names.
• A name can not be a reserved word.
• A name must not already be in use in this part of the
program.
• A reserved word is a word which has a predefined meaning
in Java. For example int, double, true, and import are
reserved words.
Variable
Types of Variables
• There are three types of variables in Java:
• local variable
• instance variable
• static variable
Variable Types
• 1) Local Variable
• A variable declared inside the body of the method is called local variable. You can
use this variable only within that method and the other methods in the class aren't
even aware that the variable exists.
• A local variable cannot be defined with "static" keyword.
• 2) Instance Variable
• A variable declared inside the class but outside the body of the method, is called an
instance variable. It is not declared as static.
• It is called an instance variable because its value is instance-specific and is not shared
among instances.
• 3) Static variable
• A variable that is declared as static is called a static variable. It cannot be local. You
can create a single copy of the static variable and share it among all the instances
of the class. Memory allocation for static variables happens only once when the
class is loaded in the memory.
Program
public class A
{
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
public static void main(String args[])
{
int data=50;//instance variable
}
}//end of class
Java Variable Example: Add Two
Numbers
public class Simple{
public static void main(String[] args){
int a=10;
int b=10;
int c=a+b;
System.out.println(c);
}
}
Output:
20
Operators
• Operators are special symbols (characters)
that carry out operations on operands
(variables and values). For example, + is an
operator that performs addition. Operator in
java is a symbol that is used to perform
operations. For example: +, -, *, / etc.
•
• There are many types of operators in java which
are given below:
• Unary Operator
• Arithmetic Operator
• Shift Operator
• Relational Operator
• Bitwise Operator
• Logical Operator
• Ternary Operator
• Assignment Operator.
• Java Operator Precedence
Unary Operators-
• Unary Operators- The Java unary operators
require only one operands. Unary operators
are used to perform various operations.
• +Unary plus (not necessary to use since
numbers are positive without using it)
• -Unary minus; inverts the sign of an
expression
• ++Increment operator; increments value by 1
• --decrement operator; decrements value by 1
• !Logical complement operator; inverts the
value of a boolean
Example
class Operator{
public static void main(String args[])
{
int x=10;
System.out.println(x++);
System.out.println(++x);
System.out.println(x--);
System.out.println(--x);
}}
Output
10
12
12
10
Arithmetic Operators-
• Arithmetic operators are used to perform
mathematical operations like addition,
subtraction, multiplication etc.
Example
class Operator{
public static void main(String agrs[]){
int x=10;
int y=5;
System.out.println(x+y);
System.out.println(x-y);
System.out.println(x*y);
System.out.println(x/y);
System.out.println(x%y);
}}
Output
15
5
50
2
0
Shift Operator-
• The java left shift operators << is used to shift
all of the bits in value to the left side and the
right shift operators >> is used to move left
operands value to right by the number of bits
to specified number of times
class Operator{
public static void main(String agrs[])
{
System.out.println(10<<2);
System.out.println(10<<3);
System.out.println(10>>2);
System.out.println(10>>3);
}}
Output
40
80
2
5
Relational Operators-
• The equality and relational operators
determines the relationship between two
operands. It checks if an operand is greater
than, less than, equal to, not equal to and so
on. Depending on the relationship, it results to
either true or false.
• public class Test {
• public static void main(String args[])
• { int a = 10; int b = 20;
• System.out.println("a == b = " + (a == b) );
• System.out.println("a != b = " + (a != b) );
• System.out.println("a > b = " + (a > b) );
• System.out.println("a < b = " + (a < b) );
• System.out.println("b >= a = " + (b >= a) );
• System.out.println("b <= a = " + (b <= a) ); } }
• This would produce the following result:
• a == b = false
• a != b = true
• a > b = false
• a < b = true b >= a = true b <= a = false
THE BITWISE OPERATORS
• : Java defines several bitwise operators, which
can be applied to the integer types, long, int,
short, char, and byte.
• Bitwise operator works on bits and performs
bit by bit operation.
‐ ‐
• Assume if a = 60; and b = 13; now in binary
format they will be a = 0011 1100 and b
= 0000 1101. a&b = 0000 1100 a|b = 0011 1101
a^b = 0011 0001 ~a = 1100 0011
Object oriented programming_Unit1_Introduction.ppt

Object oriented programming_Unit1_Introduction.ppt

  • 1.
  • 2.
    What is object-oriented programming? •Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior
  • 3.
    Overview of OOP •The object-oriented programming (called "OOP") is a programming paradigm in which the programs are organized as sets of objects. Each object represents an instance of a given class, all the classes being members of a class hierarchy unified by inheritance relationships. Therefore, objects are used by the OOP. • Each object is an instance of a given class. • The classes are linked between themselves by the inheritance relationships.
  • 4.
    Object-Oriented Programming Paradigm • Themajor motivating factor in the invention of object-oriented approach is to remove some of the flaws encountered in the procedural approach. • OOP treats data as a critical element in the program development and does not allow it to flow freely around the systems. • It ties data more closely to the functions that operate on it, and protects it from accidental modification from outside functions. • OOP allows decomposition of a problem into a number of entities called objects and then builds data and functions around these objects. • The data of an object can be accessed only by the function associated with that object. • However, functions of one object can access the the functions of other objects.
  • 5.
    Object Oriented Programming SoftwareEngineering Department The Object-Oriented Paradigm
  • 6.
    Features of Object-OrientedProgramming • Objects • Classes • Data Abstraction • Data Encapsulation • Inheritance • Polymorphism • Message passing Object Oriented Programming Software Engineering Department
  • 7.
    Classes • User defineddata types on which objects are created. • Objects with similar properties and methods are grouped together to form a Class. • For example, mango, apple and orange are members of class fruit. Object Oriented Programming Software Engineering Department
  • 8.
    Class is definedby three elements. 1.a unique class name 2.data members or attributes 3.methods Class Student { int rollno; char name[10]; Void getdata() { } Void display() { }
  • 9.
    Object Oriented Programming SoftwareEngineering Department Objects • Run time entities that may represent a person, place, bank account or any item. • Each object contains data, and code to manipulate the data. • Data represents the attributes of that object and functions represents the behaviour of that object Student s; s.rollno=101//state s.name=“ram”//state or attribute s.getdata();//behaviour
  • 10.
    Objects Contd. Object OrientedProgramming Software Engineering Department
  • 11.
    Data Abstraction • Abstractionrefers to the act of representing essential features without including the background details or explanations. • For class Person • Person name,dateofbirth, age can be taken • What he or she wears,whether they reside,whether they married or not need not be included Object Oriented Programming Software Engineering Department
  • 12.
    Data Encapsulation • Wrappingof data and functions into a single unit is known as data encapsulation. • Data is not accessible to the outside world, only those functions which are wrapped in the class can access it. Object Oriented Programming Software Engineering Department classStudent Data members Rollno Name Method Inputdata() Display()
  • 13.
    Inheritance • Process ofderiving a new class from an existing one is called Inheritance. • The existing class is known as base class, parent class or super class. • The new class that is formed is called derived class, child class or sub class. • Derived class has all the features of the base class plus it has some extra features also. Object Oriented Programming Software Engineering Department
  • 14.
    Example of Inheritance Automobile TwoWheeler Three Wheeler Four Wheeler Scooter Motor Bike Auto Rickshaw Car Bus Object Oriented Programming Software Engineering Department
  • 15.
  • 16.
    Types of Inheritance •Single level • Multi level • Multiple • Hierarchical • Hybrid Object Oriented Programming Software Engineering Department
  • 17.
    Polymorphism • Ability totake more than one form. • Two types of Polymorphism • compile Time Polymorphism – Method Overloading • Runtime Polymorphism – Method Overriding
  • 18.
    Introduction  Java isa Programming language and a Platform.  Java is a high level, robust, secured and object-oriented programming language.  Developed by Sun Microsystems (James Gosling). Platform: Any hardware or software environment in which a program runs, is known as a platform.
  • 19.
    Where it isUsed?  According to Sun, 3 billion devices run java. • Desktop Applications such as acrobat reader, media player, antivirus etc. • Web Applications such as irctc.co.in, javatpoint.com etc. • Enterprise Applications such as banking applications. • Mobile Applications • Embedded System, Smart Card, Robotics, Games etc.
  • 20.
    Characteristics/Features of Java/Buzz Wordsof Java • Java Is Simple • Java Is Object-Oriented • Java Is Distributed • Java Is Interpreted • Java Is Robust • Java Is Secure • Java Is Architecture-Neutral • Java Is Portable • Java's Performance • Java Is Multithreaded • Java Is Dynamic
  • 21.
    There are 3Java Platform Editions: 1. Java 2 Platform, Standard Edition (J2SE) • Core Java Platform targeting applications running on workstations 2. Java 2 Platform, Enterprise Edition (J2EE) • Component-based approach to developing distributed, multi-tier enterprise applications 3. Java 2 Platform, Micro Edition (J2ME) • Targeted at small, stand-alone or connectable consumer and embedded devices
  • 22.
    Brief History ofJava • In 1990, Sun Microsystems began an internal project known as the Green Project to work on a new technology. • In 1992, the Green Project was spun off and its interest directed toward building highly interactive devices for the cable TV industry. This failed to materialize. • In 1994, the focus of the original team was re-targeted, this time to the use of Internet technology. A small web browser called HotJava was written. • Oak was renamed to Java after learning that Oak had already been trademarked.
  • 23.
    • In 1995,Java was first publicly released. • In 1996, Java Development Kit (JDK) 1.0 was released. • In 2002, JDK 1.4 (codename Merlin) was released, the most widely used version. • In 2004, JDK 5.0 (codename Tiger) was released. • The latest version of java is jdk 8.0.
  • 24.
    Features of Java/JavaBuzz Words(12)
  • 25.
    1. Simple: Accordingto Sun, Java language is simple because syntax is based on C++ and there is Automatic Garbage Collection in java. 2. Object-oriented: java is a true object oriented language. Almost everything in java is an object. All program ode and data reside within objects and classes. Basic concepts of OOPs are: • Object • Class • Inheritance • Polymorphism • Abstraction • Encapsulation
  • 26.
    3. Platform Independent: •A platform is the hardware or software environment in which a program runs. • There are two types of platforms software-based and hardware-based. Java provides software-based platform. • The Java platform differs from most other platforms in the sense that it is a software-based platform that runs on the top of other hardware-based platforms. It has two components: • Runtime Environment • API(Application Programming Interface) • Java code can be run on multiple platforms e.g. Windows, Linux, Sun Solaris, Mac/OS etc. Java code is compiled by the compiler and converted into bytecode. This bytecode is a platform-independent code because it can be run on multiple platforms i.e. Write Once and Run Anywhere(WORA).
  • 28.
    4. Secured: Javais secured because: • No explicit pointer • Java Programs run inside virtual machine sandbox Java virtual machine is a virtual machine that enables computer to run java programs It is a specification that provides runtime environment in which java byte code an be executed.
  • 29.
    5. Robust: Robustsimply means strong. Java uses strong memory management. There is automatic garbage collection in java. There is exception handling and type checking mechanism in java. All these points makes java robust. 6. Architecture-neutral: There is no implementation dependent features e.g. size of primitive types is fixed. • In C programming, int data type occupies 2 bytes of memory for 32-bit architecture and 4 bytes of memory for 64-bit architecture. But in java, it occupies 4 bytes of memory for both 32 and 64 bit architectures.
  • 30.
    7. Portable: javaprograms can be moved from one compute to another anywhere anytime.We may carry the java bytecode to any platform. 8. High-performance: Java is faster than traditional interpretation since byte code is "close" to native code still somewhat slower than a compiled language (e.g., C++) 9. Distributed: We can create distributed applications in java. RMI and EJB are used for creating distributed applications. We may access files by calling the methods from any machine on the internet.
  • 31.
    10. Multi-Threaded: Athread is like a separate program, executing concurrently. • We can write Java programs that deal with many tasks at once by defining multiple threads. • Eg:we can listen to an audio clip while scrolling a page • The main advantage of multi-threading is that it doesn't occupy memory for each thread. It shares a common memory area.
  • 32.
  • 34.
    JDK Versions • JDKAlpha and Beta (1995) • JDK 1.0 (23rd Jan, 1996) • JDK 1.1 (19th Feb, 1997) • J2SE 1.2 (8th Dec, 1998) • J2SE 1.3 (8th May, 2000) • J2SE 1.4 (6th Feb, 2002) • J2SE 5.0 (30th Sep, 2004) • Java SE 6 (11th Dec, 2006) • Java SE 7 (28th July, 2011) • Java SE 8 (18th March, 2014)
  • 35.
  • 36.
    Understanding JDK &JRE JDK  JDK is an acronym for Java Development Kit.  It physically exists. It contains JRE and development tools. JRE  JRE is an acronym for Java Runtime Environment.  It is the implementation of JVM and used to provide runtime environment.  It contains set of libraries and other files that JVM uses at runtime. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 37.
    Understanding 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.  The JVM performs following main tasks: • Loads code • Verifies code • Executes code • Provides runtime environment
  • 38.
  • 39.
    How Java isPlatform-independent?
  • 40.
    How Java isPlatform-independent?
  • 41.
    How Java isPlatform-independent?  The source code (program) written in java is saved as a file with .java extension.  The java compiler “javac” compiles the source code and produces the platform independent intermediate code called BYTE CODE. It is a highly optimized set of instructions designed to be executed by the JVM.
  • 42.
    How Java isPlatform-independent?  The byte code is not native to any platform because java compiler doesn’t interact with the local platform while generating byte code.  It means that the Byte code generated on Windows is same as the byte code generated on Linux for the same java code.  The Byte code generated by the compiler would be saved as a file with .class extension. As it is not generated for any platform, can’t be directly executed on any CPU.
  • 43.
    First Java Program classSimple{ public static void main(String args[]){ System.out.println("Hello Java"); } } To compile: javac Simple.java To execute: java Simple Save the above file as Simple.java. Output: Hello Java
  • 44.
    • Parameters usedin First Java Program • Let's see what is the meaning of class, public, static, void, main, String[], System.out.println(). • class keyword is used to declare a class in Java. • public keyword is an access modifier that represents visibility. It means it is visible to all. • static is a keyword. If we declare any method as static, it is known as the static method. The core advantage of the static method is that there is no need to create an object to invoke the static method. The main() method is executed by the JVM, so it doesn't require creating an object to invoke the main() method. So, it saves memory. • void is the return type of the method. It means it doesn't return any value. • main represents the starting point of the program. • String[] args or String args[] is used for command line argument. • System.out.println() is used to print statement. Here, System is a class, out is an object of the PrintStream class, println() is a method of the PrintStream class.
  • 45.
    Datatypes • Data Typesin Java • Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java: • Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double. • Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays
  • 46.
    Java Primitive DataTypes • In Java language, primitive data types are the building blocks of data manipulation. These are the most basic data types available in Java language • There are 8 types of primitive data types: • boolean data type • byte data type • char data type
  • 47.
    • short datatype • int data type • long data type • float data type • double data type
  • 48.
  • 50.
    Boolean Data Type •The Boolean data type is used to store only two possible values: true and false. This data type is used for simple flags that track true/false conditions. Example: • Boolean one = false
  • 51.
    Byte Data Type •The byte data type is an example of primitive data type. It is an 8-bit signed two's complement integer. Its value-range lies between -128 to 127 (inclusive). Its minimum value is -128 and maximum value is 127. Its default value is 0. • byte a = 10, byte b = -20
  • 52.
    Short Data Type •The short data type is a 16-bit signed two's complement integer. Its value-range lies between -32,768 to 32,767 (inclusive). Its minimum value is -32,768 and maximum value is 32,767. Its default value is 0. • Example: • short s = 10000, short r = -5000
  • 53.
    Int Data Type •The int data type is a 32-bit signed two's complement integer. Its value-range lies between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is - 2,147,483,648and maximum value is 2,147,483,647. Its default value is 0. • Example: • int a = 100000, int b = -200000
  • 54.
    Long Data Type •The long data type is a 64-bit two's complement integer. Its value-range lies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)( • Example: • long a = 100000L, long b = -200000L
  • 55.
    Float Data Type •The float data type is a single-precision 32-bit IEEE 754 floating point.Its value range is unlimited. It is recommended to use a float (instead of double) if you need to save memory in large arrays of floating point numbers. • float f1 = 234.5f
  • 56.
    Double Data Type •The double data type is generally used for decimal values just like float. • double d1 = 12.3
  • 57.
    Char Data Type •The char data type is a single 16-bit Unicode character. Its value-range lies between ' u0000' (or 0) to 'uffff' (or 65,535 inclusive).The char data type is used to store characters. • Example: • char letterA = 'A'
  • 58.
    Variables • A variableis the name of a reserved area allocated in memory. In other words, it is a name of the memory location. It is a combination of "vary + able" which means its value can be changed. • There are three types of variables in java: local, instance and static.
  • 59.
    JAVA-NLP Lecture series Variables •A variables can be considered as a name given to the location in memory where values are stored. • One syntax of variable declaration dataType variableName; dataType variableName; • Do you imagine that the variable can change its value – yes, that is why it is called as variable
  • 60.
    JAVA-NLP Lecture series Rulesfor Naming Variable • Use only the characters 'a' through 'z', 'A' through 'Z', '0' through '9', character '_', and character '$'. A name can’t contain space character. • Do not start with a digit. A name can be of any length. • Upper and lower case count as different characters. So SUM and Sum are different names. • A name can not be a reserved word. • A name must not already be in use in this part of the program. • A reserved word is a word which has a predefined meaning in Java. For example int, double, true, and import are reserved words.
  • 61.
  • 62.
    Types of Variables •There are three types of variables in Java: • local variable • instance variable • static variable
  • 63.
    Variable Types • 1)Local Variable • A variable declared inside the body of the method is called local variable. You can use this variable only within that method and the other methods in the class aren't even aware that the variable exists. • A local variable cannot be defined with "static" keyword. • 2) Instance Variable • A variable declared inside the class but outside the body of the method, is called an instance variable. It is not declared as static. • It is called an instance variable because its value is instance-specific and is not shared among instances. • 3) Static variable • A variable that is declared as static is called a static variable. It cannot be local. You can create a single copy of the static variable and share it among all the instances of the class. Memory allocation for static variables happens only once when the class is loaded in the memory.
  • 64.
    Program public class A { staticint m=100;//static variable void method() { int n=90;//local variable } public static void main(String args[]) { int data=50;//instance variable } }//end of class
  • 65.
    Java Variable Example:Add Two Numbers public class Simple{ public static void main(String[] args){ int a=10; int b=10; int c=a+b; System.out.println(c); } } Output: 20
  • 66.
    Operators • Operators arespecial symbols (characters) that carry out operations on operands (variables and values). For example, + is an operator that performs addition. Operator in java is a symbol that is used to perform operations. For example: +, -, *, / etc. •
  • 67.
    • There aremany types of operators in java which are given below: • Unary Operator • Arithmetic Operator • Shift Operator • Relational Operator • Bitwise Operator • Logical Operator • Ternary Operator • Assignment Operator. • Java Operator Precedence
  • 68.
    Unary Operators- • UnaryOperators- The Java unary operators require only one operands. Unary operators are used to perform various operations.
  • 69.
    • +Unary plus(not necessary to use since numbers are positive without using it) • -Unary minus; inverts the sign of an expression • ++Increment operator; increments value by 1 • --decrement operator; decrements value by 1 • !Logical complement operator; inverts the value of a boolean
  • 70.
    Example class Operator{ public staticvoid main(String args[]) { int x=10; System.out.println(x++); System.out.println(++x); System.out.println(x--); System.out.println(--x); }} Output 10 12 12 10
  • 71.
    Arithmetic Operators- • Arithmeticoperators are used to perform mathematical operations like addition, subtraction, multiplication etc.
  • 72.
    Example class Operator{ public staticvoid main(String agrs[]){ int x=10; int y=5; System.out.println(x+y); System.out.println(x-y); System.out.println(x*y); System.out.println(x/y); System.out.println(x%y); }} Output 15 5 50 2 0
  • 73.
    Shift Operator- • Thejava left shift operators << is used to shift all of the bits in value to the left side and the right shift operators >> is used to move left operands value to right by the number of bits to specified number of times
  • 74.
    class Operator{ public staticvoid main(String agrs[]) { System.out.println(10<<2); System.out.println(10<<3); System.out.println(10>>2); System.out.println(10>>3); }} Output 40 80 2 5
  • 75.
    Relational Operators- • Theequality and relational operators determines the relationship between two operands. It checks if an operand is greater than, less than, equal to, not equal to and so on. Depending on the relationship, it results to either true or false.
  • 77.
    • public classTest { • public static void main(String args[]) • { int a = 10; int b = 20; • System.out.println("a == b = " + (a == b) ); • System.out.println("a != b = " + (a != b) ); • System.out.println("a > b = " + (a > b) ); • System.out.println("a < b = " + (a < b) ); • System.out.println("b >= a = " + (b >= a) ); • System.out.println("b <= a = " + (b <= a) ); } } • This would produce the following result: • a == b = false • a != b = true • a > b = false • a < b = true b >= a = true b <= a = false
  • 78.
    THE BITWISE OPERATORS •: Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte. • Bitwise operator works on bits and performs bit by bit operation. ‐ ‐ • Assume if a = 60; and b = 13; now in binary format they will be a = 0011 1100 and b = 0000 1101. a&b = 0000 1100 a|b = 0011 1101 a^b = 0011 0001 ~a = 1100 0011

Editor's Notes

  • #22 In 1990, Sun Microsystems began an internal project known as the Green Project to work on a new technology. Its team originally considered C++ as the language to use but soon abandoned it for an entirely new language called Oak. In 1992, the Green Project was spun off and its interest directed toward building highly interactive devices for the cable TV industry. This failed to materialize. In 1994, the focus of the original team was re-targeted, this time to the use of Internet technology. A small web browser called HotJava was written. Oak was renamed to Java after learning that Oak had already been trademarked. In 1995, Java was first publicly released In 1996, Java Development Kit (JDK) 1.0 was released In 2002, JDK 1.4 (codename Merlin) was released, the most widely used version In 2004, JDK 5.0 (codename Tiger) was released, the latest version The name Java was coined at a local coffee shop frequented by some of the members. It is not clear whether the name is an acronym or not. However some accounts claim that it stands for the names of James Gosling, Arthur Van Hoff, and Andy Bechtolsheim. Others, that it is an acronym for Just Another Vague Acronym.