Module 1
Module 1
Module I
Module I-Syllabus
• The main aim of OOP is to bind together the data and the
functions that operate on them so that no other part of the
code can access this data except that function.
Object Oriented Programming Vs
Procedure Oriented Programming
OOP POP
Object Oriented Structure Oriented
Bottom UP Approach Top Down Approach
Program is divided into Objects Program is divided into functions
Object can move and communicate with Data can move freely from function to
each other through member functions function
Data and Functions of a particular object Every function contains different data
will act as a single unit
OOP CONCEPTS
• Object-Oriented Programming is a methodology or paradigm
to design a program using classes and objects.
• Class
• Object
• Polymorphism
• Inheritance
• Encapsulation
• Abstraction
Class:
• Example 2:
• Class/Object: House
• State: Address, Color, Area
• Behavior: Open door, close door
• Polymorphism:
• The language itself borrows much syntax from C and C++ but
has a simpler object model and fewer low-level facilities.
• Classloader:
– Classloader in Java is a part of the Java Runtime Environment(JRE)
which is used to load Java classes into the Java Virtual Machine
dynamically.
– It adds security by separating the package for the classes of the local
file system from those that are imported from network sources.
• Bytecode Verifier:
– It checks the code fragments for illegal code that can violate access
right to objects.
• Security Manager:
– It determines what resources a class can access such as reading and
writing to the local disk.
• Robust
• Distributed
• We can write Java programs that deal with many tasks at once
by defining multiple threads.
JRE
• To run, the main class file (the class that contains the method
main) is passed to the JVM, and then goes through three main
stages before the final machine code is executed. These
stages are:
– Class Loader
– Byte Code Verifier
– Just In Time Compiler
• Class Loader:
• The main class is loaded into the memory by passing its ‘.class’
file to the JVM, through invoking the latter.
• When a method has been compiled, the JVM calls the compiled
code of that method directly instead of interpreting it.
JAVA PROGRAMMING
Object and Class Example
//Java Program to illustrate how to define a class and fields
class Student{
//defining fields
int id;
String name;
public static void main(String args[])
{
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
• Output
0
null
How to compile and execute
• If there are no errors in your code, the command prompt will take
you to the next line
– Parameterized constructor
Default Constructor
}
}
• Output
5
5
JAVA DATA TYPES
Primitive Data Types
1. Local Variables
2. Instance Variables
3. Static Variables
Local Variables:
Student age is : 5
Instance Variables:
}
}
• Output:
Marks for first object:
50
80
90
Static variables
• The static variable gets memory only once in the class area at the
time of class loading.
• Arithmetic Operators
• + Additive operator (also used for String concatenation)
• - Subtraction operator
• * Multiplication operator
• / Division operator
• % Remainder operator
• Unary Operators
• + Unary plus operator; indicates positive value (numbers
are positive without this, however)
• - Unary minus operator; negates an expression
• ++ Increment operator; increments a value by 1
• -- Decrement operator; decrements a value by 1
• ! Logical complement operator; inverts the value of a
boolean
• Equality and Relational Operators
• == Equal to
• != Not equal to
• > Greater than
• >= Greater than or equal to
• < Less than
• <= Less than or equal to
• Conditional Operators
• && Conditional-AND
• || Conditional-OR
• ?: Ternary (shorthand for if-then-else statement)
• Type Comparison Operator
• instanceof Compares an object to a specified type
class Simple{
public static void main(String args[]){
Simple s=new Simple();
System.out.println(s instanceof Simple);
}
}
// Outputs true
class D{
public static void main(String args[]){
D d=null;
System.out.println(d instanceof D);
}
}
//Outputs false
JAVA WRAPPER CLASSES
• The wrapper class in Java provides the mechanism to convert
primitive into object and object into primitive.
• The wrapper classes are part of the java. lang package, which
is imported by default into all Java programs.
Primitive Data Type Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character
Wrapper class Example: Primitive to Wrapper
int a=20;
Integer i=Integer.valueOf(a);
Integer j=a;//autoboxing
System.out.println(a+" "+i+" "+j);
}
}
• Output:
20 20 20
Example: Wrapper to Primitive
• These are data types that are built into the Java language and
identified by Java keywords.
• A primitive variable is a direct reference to its datum, meaning
that the datum is stored immediately within the variable’s
associated memory location.
• Example:
• double radius = 6.25;
• double diameter = 2 * radius;
• int topScore = 100;
Reference variable
class GetInputFromUser
{
public static void main(String args[])
{
// Using Scanner for Getting Input from User
Scanner in = new Scanner(System.in);
String s = in.nextLine();
System.out.println("You entered string "+s);
int a = in.nextInt();
System.out.println("You entered integer "+a);
float b = in.nextFloat();
System.out.println("You entered float "+b);
}
}
Difference between Scanner class and BufferedReader
class
import java.io.Console;
class ReadStringTest{
public static void main(String args[]){
Console c=System.console();
System.out.println("Enter your name: ");
String n=c.readLine();
System.out.println("Welcome "+n);
}
}
• Output
Enter your name: Nakul Jain
Welcome Nakul Jain
Example2
import java.io.Console;
class ReadPasswordTest{
public static void main(String args[]){
Console c=System.console();
System.out.println("Enter password: ");
char[] ch=c.readPassword();
String pass=String.valueOf(ch);//converting char array into string
1. By string literal
2. By new keyword
String Literal
• String s="welcome";
• Each time you create a string literal, the JVM checks the
"string constant pool" first.
• If the string already exists in the pool, a reference to the
pooled instance is returned.
• If the string doesn't exist in the pool, a new string instance is
created and placed in the pool.
Example
• String s1="Welcome";
• String s2="Welcome";//It doesn't create a new instance
class Test{
public static void main(String args[]){
String s="Sachin";
s.concat(" Tendulkar");
System.out.println(s);
}
}
• Output
Sachin
Sachin is not changed but a new object is created as Sachin Tendulkar
Example2
class Test {
public static void main(String args[])
{
String s="Sachin";
s=s.concat(" Tendulkar");
System.out.println(s);
} }
• Output
Sachin Tendulkar
Output
string length is: 4
string length is: 6
charAt()
Output
o
substring()
class ContainsExample{
public static void main(String args[]){
String name="what do you know about me";
System.out.println(name.contains("do you know"));
System.out.println(name.contains("about"));
System.out.println(name.contains("hello"));
}}
Output
true
true
false
equals()
Output
true
valueOf()
Output
3010
compareTo()
• https://docs.oracle.com/javase/7/docs/api/java/lang/String.h
tml
STRINGBUFFER CLASS IN JAVA
• Java StringBuffer class is used to create mutable (modifiable)
string.
• StringBuffer()
– creates an empty string buffer with the initial capacity of
16.
• StringBuffer(String str)
– creates a string buffer with the specified string.
• StringBuffer(int capacity)
– creates an empty string buffer with the specified capacity
as length.
Example
java
• The insert() method inserts the given string with this string at
the given position.
class Main {
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.insert(1,"Java"); //now original string is changed
System.out.println(sb); //prints HJavaello
}
}
replace()
class Main {
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.replace(1,3,"Java");
System.out.println(sb); //prints HJavalo
}
}
delete()
class Main {
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.delete(1,3);
System.out.println(sb);//prints Hlo
}
}
reverse()
class Main {
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.reverse();
System.out.println(sb); //prints olleH
}
}
capacity()
class Main {
public static void main(String args[]){
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());
sb.append("Hello");
System.out.println(sb.capacity());
sb.append("java is my favourite language");
System.out.println(sb.capacity());
}
}
• Output
16
16
34
ensureCapacity()