Module 1
Module 1
Module 1
Syllabus
MODULE I: PROCEDURAL ORIENTED
PROGRAMMING
Introduction to Java
Identifiers
Variables
Assignment Statements
Assignment Expressions
Constants(final keyword)
Data types and Operations
Console Input using Scanner class
Selections
Looping
Arrays.
MODULE II: OBJECT ORIENTED
PROGRAMMING
Objects and classes
Introduction
Defining classes for Objects
Constructing Objects Using Constructors
Constructor Overloading
MODULE III: Data field
Encapsulation and Built in
classes
Visibility Modifiers
Data Field Encapsulation
Static Variables
Constants and Methods
String class
Array class
MODULE IV: Inheritance
Inheritance introduction
Super class
Sub classes
Types of inheritance
Using the super keyword
Overriding Methods
MODULE V: Polymorphism and Exception
handling
Overloading Vs Overriding
Polymorphism
Static Vs Dynamic Binding
Exception Handling overview
Exception-Handling Advantages
Exception Types
Command-Line Arguments
Introduction
JAVA was developed by James Gosling at Sun Microsystems Inc
in the year 1995
It is a simple programming language.
Java makes writing, compiling, and debugging programming
easy.
It helps to create reusable code and modular programs.
Java is a class-based, object-oriented programming language
and is designed to have as few implementation dependencies as
possible.
A general-purpose programming language made for developers
to write once run anywhere that is compiled Java code can run
on all platforms that support Java
Applications of Java
• Mobile applications (specially Android apps)
• Desktop applications
• Web applications
• Web servers and application servers
• Games
• Database connection
• And much, much more!
Example Program
Parameters used in First Java Program
Let's see what is the meaning of class, public, static,
void, main, String[], System.out.println().
• There are certain rules for defining a valid java identifier. These rules must be
followed, otherwise we get compile-time error. These rules are also valid for
other languages like C,C++.
• The only allowed characters for identifiers are all alphanumeric characters([A-Z],
[a-z],[0-9]), ‘$‘(dollar sign) and ‘_‘ (underscore).For example “geek@” is not a
valid java identifier as it contain ‘@’ special character.
• Identifiers should not start with digits([0-9]). For example “123geeks” is a not a
valid java identifier.
• Java identifiers are case-sensitive.
• There is no limit on the length of the identifier but it is advisable to use an
optimum length of 4 – 15 letters only.
• Reserved Words can’t be used as an identifier. For example “int while = 20;” is an
invalid statement as while is a reserved word. There are 53 reserved words in
Java.
Examples of valid identifiers :
MyVariable
MYVARIABLE
Myvariable
xI
x1
i1
_myvariable
$myvariable
sum_of_array
geeks123
Examples of invalid identifiers :
My Variable // contains a space
123geeks // Begins with a digit
a+c // plus sign is not an alphanumeric
character
variable-2 // hyphen is not an alphanumeric
character
sum_&_difference // ampersand is not an
alphanumeric character
Java Variables
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
Example:
byte a = 10;
byte b = 20 ;
Short Data Type
• 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.
• The short data type can also be used to save
memory just like byte data type.
• A short data type is 2 times smaller than an
integer.
• Example:
short s = 10;
Int Data Type
• Its value-range lies between - 2,147,483,648 to
2,147,483,647 . Its minimum value is - 2,147,483,648
and maximum value is 2,147,483,647. Its default
value is 0.
• The int data type is generally used as a default data
type for integer values
Example:
int a = 1000;
Long Data Type
Its value-range lies between -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807(inclusive). Its minimum value is -
9,223,372,036,854,775,808and maximum value is
9,223,372,036,854,775,807.
Its default value is 0. The long data type is used when you need
a range of values more than those provided by int.
Example:
long a = 100000L
Float Data Type
Its recommended to use a float (instead of double) if you need to save memory
in large arrays of floating point numbers. The float data type should never be
used for precise values, such as currency. Its default value is 0.0F.
Example:
float p = 234.5;
Double Data Type
Its value range is unlimited. The double data type is generally used for
decimal values just like float. The double data type also should never be used
for precise values, such as currency. Its default value is 0.0d.
Example:
double a = 12.34567;
Char Data Type
The char data type is used to store characters.
Example:
char letter = 'A'
Operators in Java
• 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 and
Assignment Operator.
Operator Type Category Precedence
additive +-
equality == !=
bitwise exclusive OR ^
bitwise inclusive OR |
logical OR ||
Ternary ternary ?:
== Equal to x == y
!= Not equal x != y
import java.util.Scanner;
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String n ;
System.out.println("Enter username");
userName = sc.nextLine();
System.out.println(“username is: " + n);
}
}
Input Types
In the example above, we used the nextLine() method, which is used to read
Strings. To read other types, look at the table below:
Method Description
import java.util.Scanner;
The wildcard import imports all the classes in a package by using the
asterisk as the
wildcard.
For example, the following statement imports all the classes from the
package java.util.
import java.util.*;
The import statement simply tells the compiler where to locate the
classes.
import java.util.Scanner;
public class Compute
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a number for radius: ");
double radius = input.nextDouble();
double area = radius * radius * 3.14159;
System.out.println("The area for the circle of radius " +radius+ " is " +
area);
}
}
Enter a number for radius: 2.5
The area for the circle of radius 2.5 is 19.6349375
The program reads three numbers and displays their average.
import java.util.Scanner;
public class ComputeAverage
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter three numbers: ");
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();
double average = (number1 + number2 + number3) / 3;
System.out.println("The average of these 3 numbers " is " + average);
}
}
output
Enter three numbers: 10.5
11
11.5
The average of these 3 numbers is 11.0