OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
MODULE – 1
An Overview of Java: Object-Oriented Programming (Two Paradigms, Abstraction, The Three
OOP Principles), Using Blocks of Code, Lexical Issues (Whitespace, Identifiers, Literals,
Comments, Separators, The Java Keywords).
Data Types, Variables, and Arrays: The Primitive Types (Integers, Floating-Point Types,
Characters, Booleans), Variables, Type Conversion and Casting, Automatic Type Promotion in
Expressions, Arrays, Introducing Type Inference with Local Variables.
Operators: Arithmetic Operators, Relational Operators, Boolean Logical Operators, The
Assignment Operator, The ? Operator, Operator Precedence, Using Parentheses.
Control Statements: Java’s Selection Statements (if, The Traditional switch), Iteration
Statements (while, do-while, for, The For-Each Version of the for Loop, Local Variable Type
Inference in a for Loop, Nested Loops), Jump Statements (Using break, Using continue, return).
An Overview of Java
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 1
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 2
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Abstraction
Abstraction can be thought of as hiding the implementation details from the
end-user. A powerful way to manage abstraction is through the use of hierarchical
classifications. This allows us to layer the semantics of complex systems, breaking
them into more manageable pieces. For example, we consider a car as a vehicle
and can be thought of as a single object. But, from inside, car is a collection of
several subsystems viz. steering, brakes, sound system, engine etc. Again, each of
these subsystems is a collection of individual parts (Ex. Sound system is a
combination of a radio and CD/tape player). As an owner of the car, we manage it as
an individual entity by achieving hierarchical abstractions.
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 3
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 4
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 5
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
A First Simple Program
Here, we will discuss the working of a Java program by taking an example –
Program 1.1 Illustration of First Java Program
class Prg1
{
public static void main(String args[ ])
{
[Link](“Hello World!!!”);
}
Save this program as [Link]. A java program source code is a text file containing one or
more class definitions is called as compilation unit and the extension of this file name
should be .java.
To compile above program, use the following statement in the command prompt –
javac [Link]
(Note: You have to store the file [Link] in the same location as that of javac compiler or
you should setthe Environment PATH variable suitably.)
Now, the javac compiler creates a file [Link] containing bytecode version of the program,
which can be understandable by JVM. To run the program, we have to use Java application
launcher called java. That is,use the command –
java Prg1
The output of the program will now be displayed as –
Hello World!!!
Note: When java source code is compiled, each class in that file will be put into separate
output file having the same name as of the respective class and with the extension of
.class. To run a java code, we need a class file containing main() function (Though, we can
write java program without main(), for the time-being you assume that we need a main()
function!!!). Hence, it is a tradition to give the name of thejava source code file as the name
of the class containing main() function.
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 6
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Let us have closer look at the terminologies used in the above program now –
class is the keyword to declare a class.
Prg1 is the name of the class. You can use any valid identifier for a class name.
main() is name of the method from which the program execution starts.
public is a keyword indicating the access specifier of the method. The public
members can be accessed from outside the class in which they have been
declared. The main() function must be declared as public as it needs to be
called from outside the class.
static The keyword static allows main() to be called without having to instantiate a
particular instance of the class. This is necessary since main() is called by the
Java Virtual Machine before any objects are made.
void indicates that main() method is not returning anything.
String args[ ] The main() method takes an array of String objects as a command-line
argument.
System is a predefined class (present in [Link] package) which gives access to the
system. It contains pre-defined methods and fields, which provides facilities
like standard input, output,etc.
out is a static final (means not inheritable) field (ie, variable)in System class
which is of the type PrintStream (a built-in class, contains methods to print the
different data values). Static fields and methods must be accessed by using the
class name, so we need to use [Link].
println is a public method in PrintStream class to print the data values. After printing
the data, the cursor will be pushed to the next line (or we can say that, the data
is followed by a new line).
Program 1.2 Illustrating usage of variables
class Prg2
{
public static void main(String args[])
{
int n;
n=25;
[Link](“The value of n is: “ + n);
n= n*3;
[Link](“The current value of n is: “ );
[Link](n);
}
}
The output will be –
The value of n is: 25
The current value of n is: 75
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 7
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
In the above program, we have declared an integer variable n and then assigned a value to it.
Now, observe the statement,
[Link](“The value of n is: “ + n);
Here, we are trying to print a string value “The value of n is:” and also value of an integer n
together. For this, we use + symbol. Truly speaking, the value of n is internally converted
into string type and then concatenated with the string “The value of n is:”. We can use +
symbol as many times as we want to print several values.
The above program uses one more method [Link]() which will keep the cursor on
the same lineafter displaying the output. That is, no new line is not included in it.
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 8
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 9
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 10
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 11
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 12
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 13
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 14
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 15
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 16
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 17
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 18
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 19
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 20
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 21
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 22
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 23
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 24
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 25
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 26
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 27
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 28
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 29
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 30
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 31
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 32
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 33
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 34
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 35
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 36
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 37
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 38
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS306A
QUESTION BANK:
1. Explain key attributes of Java programming language.
2. What is JVM? Why do we need it?
3. Briefly explain JRE and JDK.
4. Explain three OOPs principles.
5. What are Keywords and Identifiers? List the rules to write an identifier.
6. Discuss various data types used in Java.
7. What is type Conversion and Casting? Explain automatic type promotion in
expressions with rulesand a demo program.
8. Explain scope and lifetime of variables with suitable examples.
9. "Java is a strongly typed language" - Justify this statement.
10. Write a note on
a. Java class libraries
b. Literals
11. Explain array declaration and initialization in Java with suitable examples.
12. What are multi-dimensional arrays? Explain with examples.
13. What are irregular arrays in Java? Write a program to find biggest of numbers in an
irregular array with at least three rows.
14. What are different types of operators in Java? Explain any two of them.
15. Discuss ternary operator with examples.
16. Differentiate >> and >>> with suitable examples.
17. Briefly explain short-circuit logical operators with examples.
18. Explain different types of iteration statements with examples.
19. Discuss various selective control structures.
20. Write a note on jump statements in Java.
21. Discuss different versions of for - loop with examples.
22. Write a program to illustrate break statement with labels.
Gayithri N. [Link] , Dept of CSE(IOT) , Sir MVIT , Bangalore Page 39