You are on page 1of 36

Presentation on Online Course

Offered By SWAYAM

Subject : Java programming language

Presented By;
Dr. Nidhi H. Divecha,
S.K. Patel institute of Management and Computer Studies,
Kadi Sarva Vishwavidyalaya
Gandhinagar
05/12/2020 1
Details of Course
Course Title Java programming language

Course Portal SWAYAM

Starting Date 25th March,2020

Duration 15 Weeks including 43 Video Tutorials

No. of Hours Approx. 8 Hours

Name of Experts Faculties IIT BOMBAY

Course Completion 19/4/2020 : Exam Pending

05/12/2020 2
Learning Objectives

To learn why Java is useful for desktop and web applications.
To learn how to implement object-oriented designs with Java.
To identify Java language components and how they work together in
applications.
To design and program stand-alone Java applications.
To learn how to extend Java classes with inheritance and dynamic binding.
To learn how to use exception handling in Java applications.

05/12/2020 3
Course Outline

Course has three different part:

1. Videos for Beginner


Step by Step Learning Methods
Spoken Tutorial forum
2. Basic Level
Installation of Java
Installation of Eclipse
Basics of Java including Class, Object, Constructor
3. Intermediate Level
Inheritance, interface, Abstract Class, Exception Handling

05/12/2020 4
What is Java?
Java is a popular programming language, created in 1995.
It is owned by Oracle, and more than 6 billion devices run
Java.
It is used for:
Mobile applications (specially Android apps)
Desktop applications
Web applications
Web servers and application servers
Games
Database connection
And much, much more!
05/12/2020 5
Why Java?
•Its open source – FREE
•Can be run on UNIX,Windows,LINUX,etc
•GUI/Applet capabilities
•Write Once Run Any ware(WORA)

05/12/2020 6
How to run
•JDK – Java Development Kit
•Basic compiler, linker, and libraries
•JRE – Java Runtime Environment
•It is the implementation of JVM, Predefined set of java classes available to use
•JVM – Java Virtual Machine
• Loads, verifies and executes code

05/12/2020 7
How to run
Java Applications
javac – java compiler
Commandline - javac classname.java
java – executes application
Commandline - java classname
Java Applets – java applications that are embedded in
and run in a browser
Appletviewer – shows sample in browser
Commandline – appletviewer appletpage.html

05/12/2020 8
General Syntax of Java Program

/*import and package statements here*/

public/private/protected class classname


{

Member variables
Constructors
Methods (member functions)
Main Method()

}//end of class

- File must be saved as “classname.java”


- Main method is executable for an application (public static void
main(args))
- Class must be compiled before use
05/12/2020 9
Executable Example
public class Intro_Java {

// My first Java Program


// main method begins execution of Java application Command line
arguments
public static void main( String args[] )
{
System.out.println( "Welcome to Java Programming!" );

} // end method main Standard output, print with new line

} // end class Intro_Java


COMPILATION, EXECUTION & OUTPUT:
C:\> javac Intro_Java.java
C:\> java Intro_Java
Welcome to Java Programming!
05/12/2020 10
Java Variables & Naming rules
Rules :
Variable name may consist of alphabets, digits, the underscore(_) and dollor($)
sign.
They must not begin with digit
Upper and lowecase are distinct. i.e. Total ≠ TOTAL.
It should not be a keyword.
White space is not allowed.
Variable names can be of any length.
There are two types of variables in Java:
1. local variable 2. instance variable
class A{  
int _data=50;//instance variable  
void method_name(){  
int $n=90;//local variable  
}  
}//end of class
05/12/2020 11
Java Data Types

05/12/2020 12
Primitive Data Types

Data Type Size Description Default Value

byte 1 byte Stores whole numbers from -128 to 127 0

short 2 bytes Stores whole numbers from -32,768 to 32,767 0

int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647 0

Stores whole numbers from -9,223,372,036,854,775,808 to


long 8 bytes 9,223,372,036,854,775,807 0L

Stores fractional numbers. Sufficient for storing 6 to 7 decimal


float 4 bytes digits 0.0f

Stores fractional numbers. Sufficient for storing 15 decimal


double 8 bytes 0.0d
digits

boolean 1 bit Stores true or false values FALSE

char 2 bytes Stores a single character/letter or ASCII values '\u0000'

05/12/2020 13
Escape sequences

Sequence Purpose
\b Backspace
\t Horizontal tab
\n Line feed
\f Form feed
\r Carriage return
\" Double quote
\' Single quote
\\ Backslash
\uxxxx Unicode character

05/12/2020 14
Java Comment Delimiters

Start End Purpose


/* */ The enclosed text is treated as a comment.
// (none) The rest of the line is treated as a comment.
The enclosed text is treated as a comment by the
/** */ compiler but is used by JavaDoc to automatically
generate documentation.

05/12/2020 15
Arithmetic Operator
Arithmetic Multiplicative *, / , %
Operators
Arithmetic +, -

public class Test {


public static void main(String args[])
{
int a = 10, b=30;
String A = " Lock " , B = " Down “;

System.out.println( "Value of a+b is : “+ (a + b) );

System.out.println(“You are under ” + (A + B) );


} // end class Test
OUTPUT:
Value of a+b is : 30
You are under LockDown
05/12/2020 16
Java Operator Precedence

Operator Type Category Precedence


postfix expr++ expr--
Unary
prefix ++expr --expr +expr -expr ~ !

multiplicative */%
Arithmetic
additive +-
Shift shift << >> >>>
comparison < > <= >= instanceof
Relational
equality == !=
bitwise AND &
Bitwise bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
Logical
logical OR ||
Ternary ternary ?:

Assignment assignment = += -= *= /= %= &= ^= |= <<= >>=


>>>=

05/12/2020 17
Java Control Statements

Selection Statements: used for decision making


Iteration Statements: used to reduce lines of codes
Jump statements: used to reach at or to exit from particular line of code

05/12/2020 18
Working with Array

Array Primitive type: int A[10]

Find Maximum value out of 5 values


10 5 20 9 2

OUTPUT: 20

Arrays Class:

Java provides a data structure, the arrays class from “java.util” package


Just create object of it and use it.

05/12/2020 19
Working with Strings

String Primitive type: String A= “Hello“;

The String Class:


Java provides the String class, For other langauges it is only datatype but
Java provides its Object.
Just create object of it and use different method of String class.
Sr.No. Method & Description

1 char charAt(int index) - Returns the character at the specified index.

2 int compareTo(Object o) - Compares this String to another Object.

3 boolean endsWith(String suffix) - Tests if this string ends with the specified suffix.

4 boolean equals(Object anObject) - Compares this string to the specified object.

5 int length() - Returns the length of this string.

6 String toLowerCase() - Converts all of the characters in this String to lower case using the rules of the default locale.

7 String toUpperCase() - Converts all of the characters in this String to upper case using the rules of the default locale.

05/12/2020 20
Java Methods
A method is a block of code which only runs when it is called.

public class MyClass


{
static void myMethod() { System.out.println("I just got executed!"); }
Method Overloading
static int plusMethod(int x, int y) { return x + y; }

static double plusMethod(double x, double y) { return x + y; }

public static void main(String[] args) {


OUTPUT:
int myNum1 = plusMethod(8, 5);
I just got executed!
double myNum2 = plusMethod(4.3, 6.26);
int : 13
myMethod();
double : 10.59999999
System.out.println("int: " + myNum1);
System.out.println("double: " + myNum2); }
}05/12/2020 21
Java this keyword
The this keyword refers to the current object in a method or constructor.

public class MyClass


{
public Mymethod(int x)
{ int x;
this.x = x;
System.out.println("Value of x = " + this.x); }
}
public static void main(String[] args)
{
int x=10;
Mymethod(x);
}
OUTPUT: Value of x = 10

05/12/2020 22
Java Class
Syntax:
modifier class myClass {
// instance variables
//constructor
//method declarations
};

//Creating Student class.  
public class Student
{  
  int id;  //variable
 
Student() { id=0; } // default constructor

void print(int rollno) // method


{
id=rollno;
System.out.println(“Id of Student” + id);}
05/12/2020 23
} //end of class
Java Class and Object
//Creating Student class.  
class Student
{  
  int id;  //variable
  Student() { id=0; } // default constructor
void print(int rollno) // method
{
id=rollno;
System.out.println(“Id of Student” + id); }
} //end of class
//Creating another class  which contains the main method  
class Student_info
{  
public static void main(String args[])
{  
  Student s1;
s1=new Student();  
  s1.print(126);  
  }  
}  
05/12/2020 24
Constructor overloading
//Creating Student class.  
class Student
{  
  int id;  String Name; //variable
  Student (int id) // constructor with 1 parameter
{
Constructor
this.id=id; Overloading
System.out.println(“Id of Student” + id); } //end
Student (int id, String Name) // constructor with 2 parameter
{
this.id=id; thhis.Name;
System.out.println(“Student id: ” + id + “Name:” + Name); } //end

} //end of class
//Creating another class  which contains the main method  
class Student_info
{  
public static void main(String args[])
{  
  Student s1 =new Student(126);  
Student s1 =new Student(126,”Nidhi”);   }  
}  05/12/2020 25
Inheritance in Java
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

Sub Class/Child Class: Subclass is a class which inherits the other class.

Super Class/Parent Class: Superclass is the class from where a subclass inherits the features.

syntax of Java Inheritance

class Subclass-name extends Superclass-name  
{  
   //methods and fields  
}  

05/12/2020 26
Types of inheritance in Java
On the basis of class, there can be three types of inheritance in
java:

1. Single Class : Use Extends

2. Multilevel
3. Hierarchical.

On the basis of class and interface there can be two types of


inheritance in java:

4. Multiple Class : Use Extends


5. Hybrid Interface : Use implements

05/12/2020 27
Single Inheritance
When a class inherits another class, it is known as a single inheritance.

class Employee{  
  float salary=40000;  
}  
class Programmer extends Employee
{  
 int bonus=10000;  
 public static void main(String args[])
{  
   Programmer p=new Programmer();  
   System.out.println("Programmer salary is:"+p.salary);  
   System.out.println("Bonus of Programmer is:"+p.bonus);  
}  
}  

05/12/2020 28
Multilevel Inheritance
When there is a chain of inheritance, it is known as multilevel inheritance.
class Animal{  
void eat(){System.out.println("eating...");}  
}  
class Dog extends Animal{  
void bark(){System.out.println("barking...");}  
}  
class BabyDog extends Dog{  
void weep(){System.out.println("weeping...");}  
}  
class TestInheritance2{  
public static void main(String args[])
{  
BabyDog d=new BabyDog();  
d.weep();  
d.bark();  
d.eat();  
}
05/12/2020 29
}  
Hierarchical Inheritance
When two or more classes inherits a single class, it is known as hierarchical inheritance.
class Animal{  
void eat(){System.out.println("eating...");}  
}  
class Dog extends Animal{  
void bark(){System.out.println("barking...");}  
}  
class Cat extends Animal{  
void meow(){System.out.println("meowing...");}  
}  
class TestInheritance3{  
public static void main(String args[])
{  
Cat c=new Cat();  
Dog d=new Dog();
c.eat();
c.meow();  
d.eat();  
d.bark();
}
} 05/12/2020 30
Multiple Inheritance
When one class inherits multiple classes, it is known as multiple inheritance.
interface Printable
{  
void print();  
}  
interface Showable
{  
void show();  
}  
class A7 implements Printable,Showable
{  
public void print(){System.out.println("Hello");}  
public void show(){System.out.println("Welcome");}  
  
public static void main(String args[])
{  
A7 obj = new A7();  
obj.print();  
obj.show();  
 }  
}  05/12/2020 31
Hybrid Inheritance
When combines more than one type of inheritance, it is known as multiple inheritance.
interface A {
public void methodA();
}
interface B extends A {
public void methodB();
}
interface C extends A {
public void methodC();
}
class HybridInheritance implements B, C {
public void methodA() {
System.out.println("Calling methodA");
}
public void methodB() {
System.out.println("Calling methodB");
}
public void methodC() {
System.out.println("Calling methodC");
}
public static void main(String args[]) {
HybridInheritance obj = new HybridInheritance();
obj.methodA();
obj.methodB();
obj.methodC();
}
}
05/12/2020 32
Abstract class

A class which contains abstract methods is must declared as an abstract


class.

Abstract Class:

An abstract class must be declared with an abstract keyword.


It needs to be extended and its method implemented.
It can have abstract and non-abstract methods.
It cannot be instantiated.

05/12/2020 33
Exception Handling

The Exception Handling in Java is one of the powerful mechanism to handle


the runtime errors.

There are mainly two types of exceptions:

Checked Exception - are checked at compile-time.


e.g. IOException,
SQLException

Unchecked Exception - are checked at runtime.


e.g. ArithmeticException,
ArrayIndexOutOfBoundsException

05/12/2020 34
Learning Outcomes

At the end of the course I am able to:


Install the Java Development Kit on laptop/computer.
Install an IDE on laptop/computer.
Know the difference between compiled and interpreted code.
Design, develop and test Java programs using classes, methods, conditions,
loops, etc.
Be able to develop simple data structures in object oriented form.
Develop substantial Java programs.
Describe the features of classes and interfaces in Java.
Describe what exceptions are in Java.
Handle exceptions in programs.

05/12/2020 35
THANK YOU

05/12/2020 36

You might also like