SlideShare a Scribd company logo
Unit-1
7/14/2017 1Ms SURBHI SAROHA(Asst.Professor)
Contents
 Introduction
 Data types
 Control structured
 Arrays
 Strings
 Vector
 Classes( Inheritance , package ,
exception handling)
 Multithreaded programming
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 2
Java – Overview
 Java programming language was
originally developed by Sun
Microsystems which was initiated by
James Gosling and released in 1995.
 The latest release of the Java Standard
Edition is Java SE 8.
 Java is guaranteed to be Write Once,
Run Anywhere .
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 3
Java is:
 Object Oriented: In Java, everything is an Object. Java can
be easily extended since it is based on the Object model.
 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. This byte code is
distributed over the web and interpreted by the Virtual
Machine (JVM) on whichever platform it is being run on.
 Simple: Java is designed to be easy to learn. If you
understand the basic concept of OOP Java, it would be
easy to master.
 Secure: With Java's secure feature it enables to develop
virus-free, tamper-free systems. Authentication techniques
are based on public-key encryption.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 4
Cont…..
 Architecture-neutral: Java compiler generates an architecture-
neutral object file format, which makes the compiled code
executable on many processors, with the presence of Java
runtime system.
 Portable: Being architecture-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.
 Multithreaded: With Java's multithreaded feature it is possible to
write programs that can perform many tasks simultaneously. This
design feature allows the developers to construct interactive
applications that can run smoothly.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 5
Cont…
 Interpreted: Java byte code is translated on the fly to native
machine instructions and is not stored anywhere. The
development process is more rapid and analytical since the
linking is an incremental and light-weight process.
 High Performance: With the use of Just-In-Time compilers,
Java enables high performance.
 Distributed: Java is designed for the distributed
environment of the internet.
 Dynamic: Java is considered to be more dynamic than C or
C++ since it is designed to adapt to an evolving
environment. Java programs can carry extensive amount of
run-time information that can be used to verify and resolve
accesses to objects on run-time.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 6
First Java Program
 public class MyFirstJavaProgram {
 /* This is my first java program.
 * This will print 'Hello World' as the output
 */
 public static void main(String []args) {
 System.out.println("Hello World"); // prints
Hello World
 }
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 7
Setting Up the Path for
Windows
 Assuming you have installed Java in
c:Program Filesjavajdk directory:
 Right-click on 'My Computer' and select
'Properties'.
 Click the 'Environment variables' button under
the 'Advanced' tab.
 Now, alter the 'Path' variable so that it also
contains the path to the Java executable.
Example, if the path is currently set to
'C:WINDOWSSYSTEM32', then change your
path to read
'C:WINDOWSSYSTEM32;c:Program
Filesjavajdkbin
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 8
Save the file, compile and run
the program
 Open notepad and add the code as above.
 Save the file as: MyFirstJavaProgram.java.
 Open a command prompt window and
. Java – Basic Syntax
 go to the directory where you saved the
class. Assume it's C:.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 9
Cont….
 Type 'javac MyFirstJavaProgram.java' and
press enter to compile your code. If there are
no errors in your code, the command prompt
will take you to the next line (Assumption : The
path variable is set).
 Now, type ' java MyFirstJavaProgram ' to run
your program.
 You will be able to see ' Hello World ' printed
on the window.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 10
Cont….
 C:> javac MyFirstJavaProgram.java
 C:> java MyFirstJavaProgram
 Hello World
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 11
Basic Syntax
 Case Sensitivity - Java is case sensitive, which means identifier Helloand hello
would have different meaning in Java.
 Class Names - For all class names the first letter should be in Upper Case. If
several words are used to form a name of the class, each inner word's first letter
should be in Upper Case.
 Example: class MyFirstJavaClass
 Method Names - All method names should start with a Lower Case letter. If
several words are used to form the name of the method, then each inner word's
first letter should be in Upper Case.
 Example: public void myMethodName()
 Program File Name - Name of the program file should exactly match the class
name. When saving the file, you should save it using the class name (Remember
Java is case sensitive) and append '.java' to the end of the name (if the file name
and the class name do not match, your program will not compile). Example:
Assume 'MyFirstJavaProgram' is the class name. Then the file should be saved
as 'MyFirstJavaProgram.java'
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 12
Java is an Object-Oriented
Language
 Java supports the following fundamental
concepts:
 Polymorphism
 Inheritance
 Encapsulation
 Abstraction
 Classes
 Objects
 Instance
 Method
 Message Parsing
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 13
Classes in Java
 A class is a blueprint from which individual objects
are created.
 public class Dog{
 String breed;
 int ageC
 String color;
 void barking(){
 }
 void hungry(){
 }
 void sleeping(){
 }
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 14
Constructors
 The main rule of constructors is that they should
have the same name as the class. A class can have
more than one constructor.
 Following is an example of a constructor:
 public class Puppy{
 public Puppy(){
 }
 public Puppy(String name){
 // This constructor has one parameter, name.
 }
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 15
Basic Datatypes
 There are two data types available in
Java:
 Primitive Datatypes
 Reference/Object Datatypes
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 16
Primitive Datatypes
 Byte
 short
 int
 long
 float
 double
 boolean
 char
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 17
Reference Datatypes
 A reference variable can be used to
refer any object of the declared type or
any compatible type.
 Example: Animal animal = new
Animal("giraffe");
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 18
Java Access Modifiers
 The four access levels are:
 Visible to the package, the default. No
modifiers are needed.
 Visible to the class only (private).
 Visible to the world (public).
 Visible to the package and all
subclasses (protected).
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 19
Abstract Class
 An abstract class can never be
instantiated. If a class is declared as
abstract then the sole purpose is for the
class to be extended.
 A class cannot be both abstract and final
(since a final class cannot be extended). If
a class contains abstract methods then the
class should be declared abstract.
Otherwise, a compile error will be thrown.
 An abstract class may contain both
abstract methods as well normal methods.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 20
Example
 abstract class Caravan{
 private double price;
 private String model;
 private String year;
 public abstract void goFast(); //an
abstract method
 public abstract void changeColor();
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 21
Loop Control
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 22
Decision Making
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 23
Cont….
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 24
Strings Class
 Strings, which are widely used in Java
programming, are a sequence of
characters.
 Creating Strings
 The most direct way to create a string is
to write:
 String greeting = "Hello world!";
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 25
Example
 public class StringDemo{
 public static void main(String args[]){
 char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
 String helloString = new
String(helloArray);
 System.out.println( helloString );
 }
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 26
Output
 This will produce the following result:
 hello.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 27
String Length
 public class StringDemo {
 public static void main(String args[]) {
 String palindrome = "Dot saw I was Tod";
 int len = palindrome.length();
 System.out.println( "String Length is : " +
len );
 }
 }
 This will produce the following result:
 String Length is : 17
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 28
Concatenating Strings
String compareTo(String anotherString)
Method
 The String class includes a method for
concatenating two strings:
 string1.concat(string2);
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 29
Arrays
 Java provides a data structure, the
array, which stores a fixed-size
sequential collection of elements of the
same type.
 An array is used to store a collection of
data, but it is often more useful to think
of an array as a collection of variables of
the same type.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 30
Java – Exceptions
 An exception (or exceptional event) is a
problem that arises during the execution
of a program. When an Exception
occurs the normal flow of the program is
disrupted and the program/Application
terminates abnormally, which is not
recommended, therefore, these
exceptions are to be handled.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 31
Exception Hierarchy
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 32
Catching Exceptions
 A method catches an exception using a
combination of the try and catch
keywords.
 try
 {
 //Protected code
 }catch(ExceptionName e1)
 {
 //Catch block
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 33
Java – Inner Classes
 Nested Classes
 In Java, just like methods, variables of a
class too can have another class as its
member. Writing a class within another
is allowed in Java. The class written
within is called the nested class, and
the class that holds the inner class is
called the outer class.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 34
Syntax to write a nested
class
 Here, the class Outer_Demo is the
outer class and the class Inner_Demo
is the nested class.
 class Outer_Demo{
 class Nested_Demo{
 }
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 35
Java – Inheritance
 The class which inherits the properties
of other is known as subclass (derived
class, child class) and the class whose
properties are inherited is known as
superclass (base class, parent class).
 extends Keyword
 extends is the keyword used to inherit
the properties of a class. Following is
the syntax of extends keyword.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 36
Syntax of extends keyword
 class Super{
 .....
 .....
 }
 class Sub extends Super{
 .....
 .....
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 37
Types of Inheritance
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 38
Cont….
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 39
Note:-
 A very important fact to remember is that
Java does not support multiple
inheritance. This means that a class
cannot extend more than one class.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 40
Java – Packages
 A Package can be defined as a grouping
of related types (classes, interfaces,
enumerations and annotations )
providing access protection and
namespace management.
 Some of the existing packages in Java are:
 java.lang - bundles the fundamental
classes
 java.io - classes for input, output
functions are bundled in this package
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 41
Creating a Package
 While creating a package, you should
choose a name for the package and
include a package statement along with
that name at the top of every source file
that contains the classes, interfaces,
enumerations, and annotation types
that you want to include in the package.
 The package statement should be the first
line in the source file. There can be only
one package statement in each source file,
and it applies to all types in the file.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 42
Package example
 Following package example contains
interface named animals:
 /* File name : Animal.java */
 package animals;
 interface Animal {
 public void eat();
 public void travel();
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 43
Exception Handling
 A Java exception is an object that
describes an exceptional (that is, error)
condition that has occurred in a piece of
code.
 Java exception handling is managed via
five keywords: try, catch, throw,
throws, and finally.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 44
General form of an exception-
handling block:
try {
 // block of code to monitor for errors
 }
 catch (ExceptionType1 exOb) {
 // exception handler for ExceptionType1
 }
 catch (ExceptionType2 exOb) {
 // exception handler for ExceptionType2
 }
 // ...
 finally {
 // block of code to be executed after try block ends
 }
 Here, ExceptionType is the type of exception that has occurred
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 45
Multithreaded Programming
 Unlike many other computer languages,
Java provides built-in support for
multithreaded programming. A
multithreaded program contains two or
more parts that can run concurrently.
 Each part of such a program is called a
thread, and each thread defines
a separate path of execution. Thus,
multithreading is a specialized form of
multitasking.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 46
Creating Multiple Threads
 // Create multiple threads.
 class NewThread implements Runnable {
 String name; // name of thread
 Thread t;
 NewThread(String threadname) {
 name = threadname;
 t = new Thread(this, name);
 System.out.println("New thread: " + t);
 t.start(); // Start the thread
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 47
Cont….
 // This is the entry point for thread.
 public void run() {
 try {
 for(int i = 5; i > 0; i--) {
 System.out.println(name + ": " + i);
 Thread.sleep(1000);
 }
 } catch (InterruptedException e) {
 System.out.println(name + "Interrupted");
 }
 System.out.println(name + " exiting.");
 }
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 48
Cont….
 class MultiThreadDemo {
 public static void main(String args[]) {
 new NewThread("One"); // start threads
 new NewThread("Two");
 new NewThread("Three");
 try {
 // wait for other threads to end
 Thread.sleep(10000);
 } catch (InterruptedException e) {
 System.out.println("Main thread Interrupted");
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 49
Cont…
 System.out.println("Main thread
exiting.");
 }
 }
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 50
Output
 New thread: Thread[One,5,main]
 New thread: Thread[Two,5,main]
 New thread: Thread[Three,5,main]
 One: 5
 Two: 5
 Three: 5
 One: 4
 Two: 4
 Three: 4
 One: 3
 Three: 3
 Two: 3
 One: 2
 Three: 2
 Two: 2
 One: 1
 Three: 1
 Two: 1
 One exiting.
 Two exiting.
 Three exiting.
 Main thread exiting.
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 51
 THANK YOU 
7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 52

More Related Content

What's hot (20)

Introduction to java
Introduction to java
Sandeep Rawat
 
6. static keyword
6. static keyword
Indu Sharma Bhardwaj
 
Introduction to java 8 stream api
Introduction to java 8 stream api
Vladislav sidlyarevich
 
Array in c#
Array in c#
Prem Kumar Badri
 
Jdbc ppt
Jdbc ppt
Vikas Jagtap
 
Java Presentation
Java Presentation
aitrichtech
 
Strings in Java
Strings in Java
Hitesh-Java
 
Core java complete ppt(note)
Core java complete ppt(note)
arvind pandey
 
Static Members-Java.pptx
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
Java tokens
Java tokens
shalinikarunakaran1
 
JDBC – Java Database Connectivity
JDBC – Java Database Connectivity
Information Technology
 
Wrapper classes
Wrapper classes
Kongu Engineering College, Perundurai, Erode
 
Oops ppt
Oops ppt
abhayjuneja
 
9. Input Output in java
9. Input Output in java
Nilesh Dalvi
 
JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced Functions
WebStackAcademy
 
Methods and constructors in java
Methods and constructors in java
baabtra.com - No. 1 supplier of quality freshers
 
Presentation on Core java
Presentation on Core java
mahir jain
 
Data Types, Variables, and Operators
Data Types, Variables, and Operators
Marwa Ali Eissa
 
Introduction to class in java
Introduction to class in java
kamal kotecha
 
constructors in java ppt
constructors in java ppt
kunal kishore
 

Similar to Java programming(unit 1) (20)

Core_java_ppt.ppt
Core_java_ppt.ppt
SHIBDASDUTTA
 
java
java
Kunal Sunesara
 
Presentation to java
Presentation to java
Ganesh Chittalwar
 
M251_Meeting 1(M251_Meeting 1_updated.pdf)
M251_Meeting 1(M251_Meeting 1_updated.pdf)
hossamghareb681
 
java introduction
java introduction
Kunal Sunesara
 
LECTURE 2 -Object oriented Java Basics.pptx
LECTURE 2 -Object oriented Java Basics.pptx
AOmaAli
 
Introduction to java Programming Language
Introduction to java Programming Language
rubyjeyamani1
 
Sep 15
Sep 15
Zia Akbar
 
Core java Basics
Core java Basics
RAMU KOLLI
 
Sep 15
Sep 15
dilipseervi
 
Java SpringMVC SpringBOOT (Divergent).ppt
Java SpringMVC SpringBOOT (Divergent).ppt
Aayush Chimaniya
 
Java notes
Java notes
Upasana Talukdar
 
Java Basics 1.pptx
Java Basics 1.pptx
TouseeqHaider11
 
Java
Java
Zeeshan Khan
 
Basic Java Programming
Basic Java Programming
Math-Circle
 
Modern_2.pptx for java
Modern_2.pptx for java
MayaTofik
 
Basics java programing
Basics java programing
Darshan Gohel
 
Unit-1_GHD.pptxguguigihihihihihihoihihhi
Unit-1_GHD.pptxguguigihihihihihihoihihhi
40NehaPagariya
 
Java tutorial for beginners-tibacademy.in
Java tutorial for beginners-tibacademy.in
TIB Academy
 
Unit 1
Unit 1
LOVELY PROFESSIONAL UNIVERSITY
 
M251_Meeting 1(M251_Meeting 1_updated.pdf)
M251_Meeting 1(M251_Meeting 1_updated.pdf)
hossamghareb681
 
LECTURE 2 -Object oriented Java Basics.pptx
LECTURE 2 -Object oriented Java Basics.pptx
AOmaAli
 
Introduction to java Programming Language
Introduction to java Programming Language
rubyjeyamani1
 
Core java Basics
Core java Basics
RAMU KOLLI
 
Java SpringMVC SpringBOOT (Divergent).ppt
Java SpringMVC SpringBOOT (Divergent).ppt
Aayush Chimaniya
 
Basic Java Programming
Basic Java Programming
Math-Circle
 
Modern_2.pptx for java
Modern_2.pptx for java
MayaTofik
 
Basics java programing
Basics java programing
Darshan Gohel
 
Unit-1_GHD.pptxguguigihihihihihihoihihhi
Unit-1_GHD.pptxguguigihihihihihihoihihhi
40NehaPagariya
 
Java tutorial for beginners-tibacademy.in
Java tutorial for beginners-tibacademy.in
TIB Academy
 
Ad

More from Dr. SURBHI SAROHA (20)

Deep learning(UNIT 3) BY Ms SURBHI SAROHA
Deep learning(UNIT 3) BY Ms SURBHI SAROHA
Dr. SURBHI SAROHA
 
MOBILE COMPUTING UNIT 2 by surbhi saroha
MOBILE COMPUTING UNIT 2 by surbhi saroha
Dr. SURBHI SAROHA
 
Mobile Computing UNIT 1 by surbhi saroha
Mobile Computing UNIT 1 by surbhi saroha
Dr. SURBHI SAROHA
 
DEEP LEARNING (UNIT 2 ) by surbhi saroha
DEEP LEARNING (UNIT 2 ) by surbhi saroha
Dr. SURBHI SAROHA
 
Introduction to Deep Leaning(UNIT 1).pptx
Introduction to Deep Leaning(UNIT 1).pptx
Dr. SURBHI SAROHA
 
Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2
Dr. SURBHI SAROHA
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptx
Dr. SURBHI SAROHA
 
Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)
Dr. SURBHI SAROHA
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptx
Dr. SURBHI SAROHA
 
Introduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptx
Dr. SURBHI SAROHA
 
JAVA (UNIT 5)
JAVA (UNIT 5)
Dr. SURBHI SAROHA
 
DBMS (UNIT 5)
DBMS (UNIT 5)
Dr. SURBHI SAROHA
 
DBMS UNIT 4
DBMS UNIT 4
Dr. SURBHI SAROHA
 
JAVA(UNIT 4)
JAVA(UNIT 4)
Dr. SURBHI SAROHA
 
OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)
Dr. SURBHI SAROHA
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
Dr. SURBHI SAROHA
 
DBMS UNIT 3
DBMS UNIT 3
Dr. SURBHI SAROHA
 
JAVA (UNIT 3)
JAVA (UNIT 3)
Dr. SURBHI SAROHA
 
Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)
Dr. SURBHI SAROHA
 
DBMS (UNIT 2)
DBMS (UNIT 2)
Dr. SURBHI SAROHA
 
Deep learning(UNIT 3) BY Ms SURBHI SAROHA
Deep learning(UNIT 3) BY Ms SURBHI SAROHA
Dr. SURBHI SAROHA
 
MOBILE COMPUTING UNIT 2 by surbhi saroha
MOBILE COMPUTING UNIT 2 by surbhi saroha
Dr. SURBHI SAROHA
 
Mobile Computing UNIT 1 by surbhi saroha
Mobile Computing UNIT 1 by surbhi saroha
Dr. SURBHI SAROHA
 
DEEP LEARNING (UNIT 2 ) by surbhi saroha
DEEP LEARNING (UNIT 2 ) by surbhi saroha
Dr. SURBHI SAROHA
 
Introduction to Deep Leaning(UNIT 1).pptx
Introduction to Deep Leaning(UNIT 1).pptx
Dr. SURBHI SAROHA
 
Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2
Dr. SURBHI SAROHA
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptx
Dr. SURBHI SAROHA
 
Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)
Dr. SURBHI SAROHA
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptx
Dr. SURBHI SAROHA
 
Introduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptx
Dr. SURBHI SAROHA
 
Ad

Recently uploaded (20)

Great power lithium iron phosphate cells
Great power lithium iron phosphate cells
salmankhan835951
 
TEA2016AAT 160 W TV application design example
TEA2016AAT 160 W TV application design example
ssuser1be9ce
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
4th International Conference on Computer Science and Information Technology (...
4th International Conference on Computer Science and Information Technology (...
ijait
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
Water demand - Types , variations and WDS
Water demand - Types , variations and WDS
dhanashree78
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
grade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quiz
norfapangolima
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank
Guru Nanak Technical Institutions
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
Great power lithium iron phosphate cells
Great power lithium iron phosphate cells
salmankhan835951
 
TEA2016AAT 160 W TV application design example
TEA2016AAT 160 W TV application design example
ssuser1be9ce
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
4th International Conference on Computer Science and Information Technology (...
4th International Conference on Computer Science and Information Technology (...
ijait
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
Water demand - Types , variations and WDS
Water demand - Types , variations and WDS
dhanashree78
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
grade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quiz
norfapangolima
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 

Java programming(unit 1)

  • 1. Unit-1 7/14/2017 1Ms SURBHI SAROHA(Asst.Professor)
  • 2. Contents  Introduction  Data types  Control structured  Arrays  Strings  Vector  Classes( Inheritance , package , exception handling)  Multithreaded programming 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 2
  • 3. Java – Overview  Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995.  The latest release of the Java Standard Edition is Java SE 8.  Java is guaranteed to be Write Once, Run Anywhere . 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 3
  • 4. Java is:  Object Oriented: In Java, everything is an Object. Java can be easily extended since it is based on the Object model.  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. This byte code is distributed over the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.  Simple: Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.  Secure: With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 4
  • 5. Cont…..  Architecture-neutral: Java compiler generates an architecture- neutral object file format, which makes the compiled code executable on many processors, with the presence of Java runtime system.  Portable: Being architecture-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.  Multithreaded: With Java's multithreaded feature it is possible to write programs that can perform many tasks simultaneously. This design feature allows the developers to construct interactive applications that can run smoothly. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 5
  • 6. Cont…  Interpreted: Java byte code is translated on the fly to native machine instructions and is not stored anywhere. The development process is more rapid and analytical since the linking is an incremental and light-weight process.  High Performance: With the use of Just-In-Time compilers, Java enables high performance.  Distributed: Java is designed for the distributed environment of the internet.  Dynamic: Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 6
  • 7. First Java Program  public class MyFirstJavaProgram {  /* This is my first java program.  * This will print 'Hello World' as the output  */  public static void main(String []args) {  System.out.println("Hello World"); // prints Hello World  }  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 7
  • 8. Setting Up the Path for Windows  Assuming you have installed Java in c:Program Filesjavajdk directory:  Right-click on 'My Computer' and select 'Properties'.  Click the 'Environment variables' button under the 'Advanced' tab.  Now, alter the 'Path' variable so that it also contains the path to the Java executable. Example, if the path is currently set to 'C:WINDOWSSYSTEM32', then change your path to read 'C:WINDOWSSYSTEM32;c:Program Filesjavajdkbin 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 8
  • 9. Save the file, compile and run the program  Open notepad and add the code as above.  Save the file as: MyFirstJavaProgram.java.  Open a command prompt window and . Java – Basic Syntax  go to the directory where you saved the class. Assume it's C:. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 9
  • 10. Cont….  Type 'javac MyFirstJavaProgram.java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption : The path variable is set).  Now, type ' java MyFirstJavaProgram ' to run your program.  You will be able to see ' Hello World ' printed on the window. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 10
  • 11. Cont….  C:> javac MyFirstJavaProgram.java  C:> java MyFirstJavaProgram  Hello World 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 11
  • 12. Basic Syntax  Case Sensitivity - Java is case sensitive, which means identifier Helloand hello would have different meaning in Java.  Class Names - For all class names the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.  Example: class MyFirstJavaClass  Method Names - All method names should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case.  Example: public void myMethodName()  Program File Name - Name of the program file should exactly match the class name. When saving the file, you should save it using the class name (Remember Java is case sensitive) and append '.java' to the end of the name (if the file name and the class name do not match, your program will not compile). Example: Assume 'MyFirstJavaProgram' is the class name. Then the file should be saved as 'MyFirstJavaProgram.java' 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 12
  • 13. Java is an Object-Oriented Language  Java supports the following fundamental concepts:  Polymorphism  Inheritance  Encapsulation  Abstraction  Classes  Objects  Instance  Method  Message Parsing 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 13
  • 14. Classes in Java  A class is a blueprint from which individual objects are created.  public class Dog{  String breed;  int ageC  String color;  void barking(){  }  void hungry(){  }  void sleeping(){  }  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 14
  • 15. Constructors  The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor.  Following is an example of a constructor:  public class Puppy{  public Puppy(){  }  public Puppy(String name){  // This constructor has one parameter, name.  }  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 15
  • 16. Basic Datatypes  There are two data types available in Java:  Primitive Datatypes  Reference/Object Datatypes 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 16
  • 17. Primitive Datatypes  Byte  short  int  long  float  double  boolean  char 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 17
  • 18. Reference Datatypes  A reference variable can be used to refer any object of the declared type or any compatible type.  Example: Animal animal = new Animal("giraffe"); 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 18
  • 19. Java Access Modifiers  The four access levels are:  Visible to the package, the default. No modifiers are needed.  Visible to the class only (private).  Visible to the world (public).  Visible to the package and all subclasses (protected). 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 19
  • 20. Abstract Class  An abstract class can never be instantiated. If a class is declared as abstract then the sole purpose is for the class to be extended.  A class cannot be both abstract and final (since a final class cannot be extended). If a class contains abstract methods then the class should be declared abstract. Otherwise, a compile error will be thrown.  An abstract class may contain both abstract methods as well normal methods. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 20
  • 21. Example  abstract class Caravan{  private double price;  private String model;  private String year;  public abstract void goFast(); //an abstract method  public abstract void changeColor();  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 21
  • 22. Loop Control 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 22
  • 23. Decision Making 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 23
  • 24. Cont…. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 24
  • 25. Strings Class  Strings, which are widely used in Java programming, are a sequence of characters.  Creating Strings  The most direct way to create a string is to write:  String greeting = "Hello world!"; 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 25
  • 26. Example  public class StringDemo{  public static void main(String args[]){  char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};  String helloString = new String(helloArray);  System.out.println( helloString );  }  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 26
  • 27. Output  This will produce the following result:  hello. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 27
  • 28. String Length  public class StringDemo {  public static void main(String args[]) {  String palindrome = "Dot saw I was Tod";  int len = palindrome.length();  System.out.println( "String Length is : " + len );  }  }  This will produce the following result:  String Length is : 17 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 28
  • 29. Concatenating Strings String compareTo(String anotherString) Method  The String class includes a method for concatenating two strings:  string1.concat(string2); 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 29
  • 30. Arrays  Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type.  An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 30
  • 31. Java – Exceptions  An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 31
  • 32. Exception Hierarchy 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 32
  • 33. Catching Exceptions  A method catches an exception using a combination of the try and catch keywords.  try  {  //Protected code  }catch(ExceptionName e1)  {  //Catch block  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 33
  • 34. Java – Inner Classes  Nested Classes  In Java, just like methods, variables of a class too can have another class as its member. Writing a class within another is allowed in Java. The class written within is called the nested class, and the class that holds the inner class is called the outer class. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 34
  • 35. Syntax to write a nested class  Here, the class Outer_Demo is the outer class and the class Inner_Demo is the nested class.  class Outer_Demo{  class Nested_Demo{  }  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 35
  • 36. Java – Inheritance  The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).  extends Keyword  extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 36
  • 37. Syntax of extends keyword  class Super{  .....  .....  }  class Sub extends Super{  .....  .....  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 37
  • 38. Types of Inheritance 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 38
  • 39. Cont…. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 39
  • 40. Note:-  A very important fact to remember is that Java does not support multiple inheritance. This means that a class cannot extend more than one class. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 40
  • 41. Java – Packages  A Package can be defined as a grouping of related types (classes, interfaces, enumerations and annotations ) providing access protection and namespace management.  Some of the existing packages in Java are:  java.lang - bundles the fundamental classes  java.io - classes for input, output functions are bundled in this package 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 41
  • 42. Creating a Package  While creating a package, you should choose a name for the package and include a package statement along with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package.  The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 42
  • 43. Package example  Following package example contains interface named animals:  /* File name : Animal.java */  package animals;  interface Animal {  public void eat();  public void travel();  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 43
  • 44. Exception Handling  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of code.  Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 44
  • 45. General form of an exception- handling block: try {  // block of code to monitor for errors  }  catch (ExceptionType1 exOb) {  // exception handler for ExceptionType1  }  catch (ExceptionType2 exOb) {  // exception handler for ExceptionType2  }  // ...  finally {  // block of code to be executed after try block ends  }  Here, ExceptionType is the type of exception that has occurred 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 45
  • 46. Multithreaded Programming  Unlike many other computer languages, Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run concurrently.  Each part of such a program is called a thread, and each thread defines a separate path of execution. Thus, multithreading is a specialized form of multitasking. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 46
  • 47. Creating Multiple Threads  // Create multiple threads.  class NewThread implements Runnable {  String name; // name of thread  Thread t;  NewThread(String threadname) {  name = threadname;  t = new Thread(this, name);  System.out.println("New thread: " + t);  t.start(); // Start the thread  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 47
  • 48. Cont….  // This is the entry point for thread.  public void run() {  try {  for(int i = 5; i > 0; i--) {  System.out.println(name + ": " + i);  Thread.sleep(1000);  }  } catch (InterruptedException e) {  System.out.println(name + "Interrupted");  }  System.out.println(name + " exiting.");  }  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 48
  • 49. Cont….  class MultiThreadDemo {  public static void main(String args[]) {  new NewThread("One"); // start threads  new NewThread("Two");  new NewThread("Three");  try {  // wait for other threads to end  Thread.sleep(10000);  } catch (InterruptedException e) {  System.out.println("Main thread Interrupted");  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 49
  • 50. Cont…  System.out.println("Main thread exiting.");  }  } 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 50
  • 51. Output  New thread: Thread[One,5,main]  New thread: Thread[Two,5,main]  New thread: Thread[Three,5,main]  One: 5  Two: 5  Three: 5  One: 4  Two: 4  Three: 4  One: 3  Three: 3  Two: 3  One: 2  Three: 2  Two: 2  One: 1  Three: 1  Two: 1  One exiting.  Two exiting.  Three exiting.  Main thread exiting. 7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 51
  • 52.  THANK YOU  7/14/2017 Ms SURBHI SAROHA(Asst.Professor) 52