OOPs With Java - Introduction To Java
OOPs With Java - Introduction To Java
Introduction to Java
Syllabus
● Introduction to Java, Program structure, Class Libraries, Using Java class libraries, data
types, control structures in Java, simple Java programs,
● Java applications and Applets, Drawing graphical shapes,
● Objects and Classes, Attributes, Methods, Visibility, Class Diagram Class Relationships,
Association, Aggregation, Composition
● Inheritance and Polymorphism, Base class, Derived class, Redefined methods, Abstract
methods and classes
● Java Interfaces
● Exception Handling, try, catch, and finally blocks, Types of Exceptions, Creating exception
classes I/O streams, Character streams, Byte Streams, File Streams, Reading and writing
to a file, Persistent objects, Object streams, Reading and writing objects to a file
● GUI Programming-1: Components, containers, events, listeners, dialog boxes, menu
items
● GUI Programming-2: Frames, Layouts, Swing components and Hierarchies
Evaluation Pattern:
Quizzes (10%), Lab Internals (30%), Mid term (20%),
End term (30%), Project (10%)
Text books:
1. Java 7 Programming - Black Book, By Kogent Learning Solutions Inc., Freamtech Publications
2. Head First Java 2nd Edition by Kathy Sierra, Oreilly Publication
3. T.Budd “Understanding OOP with Java” updated Edition, pearson education, ISBN:10:0201612739
4. Herbert schildt “Java the complete reference”, 7th Edition, TMH,ISBN:0072263857
REFERENCE BOOKS:
1. Y. Daniel Liang “Introduction to Java programming” 6th Edition, pearson education,ISBN:10:0132221586
2. R.A. Johnson-An introduction to Java programming and object oriented application development,
Thomson, ISBN:-10:0619217464
WEB LINKS:
1. www.tatamcgrawhill.com/html/9780070636774.html
2. http://nptel.iitm.ac.in
3. https://www.cl.cam.ac.uk/teaching/0910/OOProg/OOP.pdf
What is Java
1. Desktop Applications
2. Web Applications
3. Mobile
4. Enterprise Applications
5. Smart Card
6. Embedded System
7. Games
8. Robotics etc
History of Java
Features of Java:
● Object Oriented
● Platform Independent
● Simple
● Secure
● Architecture-neutral
● Portable.
● Robust
● Multithreaded
● Interpreted
● High Performance
● Distributed
● Dynamic
Object Oriented Programming System(OOPS)
Object means a real word entity such as pen, chair, table etc. Object- Oriented Programming is a
methodology or paradigm to design a program
using classes and objects. It simplifies the software development and maintenance by providing some
concepts:
● Object
● Class
● Inheritance
● Polymorphism
● Abstraction
● Encapsulation
Procedure to write simple java Program
To create a simple java program, you need to create a class that contains
main method. Let's understand the requirement first.
https://www.oracle.com/in/java/technologies/downloads/
Go to MyComputer properties -> advanced tab -> environment variables -> new
tab of user variable -> write path in variable name -> write path of bin folder in
variable value -> ok -> ok -> ok
Setting Up the Path for Linux, UNIX, Solaris, FreeBSD
Environment variable PATH should be set to point to where the Java binaries have
been installed. Refer to your shell documentation, if you have trouble doing this.
For Example if you use bash as your shell, then you would add the following line
to the end of your '.bashrc:
Notepad − On Windows machine, you can use any simple text editor like Notepad
(Recommended for this tutorial), TextPad.
● JDK and JRE physically exists but JVM is abstract machine it means it does
not exists physically.
JAVA VIRTUAL MACHINE
JVM (Java Virtual Machine)
It is:
1. A specification: where working of Java Virtual Machine is specified. But implementation
provider is independent to choose the algorithm. Its implementation has been provided by
Sun and other companies.
2. An implementation: Its implementation is known as JRE (Java Runtime Environment).
3. Runtime Instance: Whenever you write java command on the command prompt to run
the java class, an instance of JVM is created.
What it does:
type methodname1(parameter-list)
● A class is declared by use of the class keyword. A simplified
general form of a class definition is shown here: { // body of method }
● The data, or variables, defined within a class are called type methodname2(parameter-list)
instance variables.
{ // body of method }
● The code is contained within methods. Collectively, the
// ... type methodnameN(parameter-list)
methods and variables defined within a class are called
members of the class. In most classes, the instance variables { // body of method }
are acted upon and accessed by the methods defined for that
}
class
Simple Class
Class Sample
{
int len, float ht
void get()
{ // body
}}
Here a class Sample contains two variable len and ht
Object in Java
● Object is the physical as well as logical entity whereas class is the logical entity only.
● value of the ID is not visible to the external user. But, it is used internally by the JVM to
identify each object uniquely.
Object is an instance of a class. Class is a template or blueprint from
which objects are created. So object is the instance(result) of a class.
Object Definitions:
Object is a real world entity.
Object is a run time entity.
Object is an entity which has state and behavior.
Object is an instance of a class.
Sample s=new Sample() here s is an object for the class Sample
new operator is used to create an object
Methods
A method is basically a behavior. A class can contain many methods. It is in methods where the
logics are written, data is manipulated and all the actions are executed.
1. class Sample{
4. }
5. }
● All Java components require names. Names used for classes, variables, and
methods are called identifiers.
These are of variable size & are usually declared with a ‘new’ keyword.
Eg. Array, String
System.out.println(“number ”+ num);
System.out.println(num +”is the number”);
}
}
User Input
Different classes:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
String userName;
● 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.
Example
1. public class A
2. {
3. static int m=100;//static variable
4. public int data1=50;//instance variable
5. void method()
6. {
7. int n=90;//local variable
8. }
9. public static void main(String args[])
10. {
11. int data=50;//instance variable
12. }
13. }//end of class
Java Tokens
Java Tokens are the smallest individual building block or smallest unit of a Java
program, it is used by the Java compiler for constructing expressions and
statements. Java program is collection different types of tokens, comments, and
white spaces.
○ Keywords
○ Identifiers
○ Literals
○ Operators
○ Separators
○ Comments
Java Literals
Literal Type
23 int
9.86 double
"xyzabc" String
Operator Symbols
Arithmetic +,-,/,*,%
Unary ++ , - - , !
Assignment = , += , -= , *= , /= , %= , ^=
Logical && , ||
Bitwise &,|,^,~
also known as punctuators. There are nine separators in Java, are as follows:
separator <= ; | , | . | ( | ) | { | } | [ | ]
○ Square Brackets []: It is used to define array elements. A pair of square brackets represents the
single-dimensional array, two pairs of square brackets represent the two-dimensional array.
○ Parentheses (): It is used to call the functions and parsing the parameters.
○ Curly Braces {}: The curly braces denote the starting and ending of a code block.
○ Comma (,): It is used to separate two values, statements, and parameters.
○ Assignment Operator (=): It is used to assign a variable and constant.
○ Semicolon (;): It is the symbol that can be found at end of the statements. It separates the two statements.
○ Period (.): It separates the package name form the sub-packages and class. It also separates a variable or
method from a reference variable.
Comments
specify information about the program inside our Java code. Java compiler recognizes these comments as tokens but
excludes it from further processing. The Java compiler treats comments as whitespaces. Java provides the following two
types of comments:
2. if-else statement
3. if-else-if ladder
4. Nested if-statement
If Statement
1.
2.
3. if-else-if ladder
4. Nested if-statement
Switch Statement
Loop Statements
1. for loop
2. while loop
3. do-while loop
for loop
Syntax:
1. for(initialization, condition, increment/decrement) {
2. //block of statements
3. }
Example:
1. for(int j = 1; j<=10; j++) {
2. sum = sum + j;
3. }
for-each loop
traverse the data structures like array or collection. In the for-each loop, we don't need to
update the loop variable
Syntax:
1. for(data_type var : array_name/collection_name){
2. //statements
3. }
Example:
1. String[] names = {"Java","C","C++","Python","JavaScript"};
2. System.out.println("Printing the content of the array names:\n");
3. for(String name:names) {
4. System.out.println(name);
5. }
While loop
Syntax:
1. while(condition){
2. //looping statements
3. }
Example:
1. while(i<=10) {
2. System.out.println(i);
3. i = i + 2;
4. }
do-while loop
Syntax: 1. do
2. {
3. //statements
4. } while (condition);
Example: 1. do {
2. System.out.println(i);
3. i = i + 2;
4. }while(i<=10);
Jump Statements
break statement: break the current flow of the program and transfer the control to the next statement outside a loop
or switch statement. However, it breaks only the inner loop in the case of the nested loop.
Example:
continue statement: it skips the specific part of the loop and jumps to the next iteration of the loop immediately.
Example: