MODULE -1
Object Oriented Programming with JAVA
Course Code: BCS306A
Prepared By:
Dinesh S
Dept of AI&ML
Tuesday 26 November 2024 Department of AI&ML, NCEH. 1
MODULE -1
Type Inference with Local Variables
Recently, an exciting new feature called local variable type inference was added to the Java
language.
To begin, let’s review two important aspects of variables.
• First, all variables in Java must be declared prior to their use.
• Second, a variable can be initialized with a value when it is declared.
Furthermore, when a variable is initialized, the type of the initializer must be the same as (or
convertible to) the declared type of the variable.
Thus, in principle, it would not be necessary to specify an explicit type for an initialized variable
because it could be inferred by the type of its initializer.
Of course, in the past, such inference was not supported, and all variables required an explicitly
declared type, whether they were initialized or not.
Today, that situation has changed.
Tuesday 26 November 2024 Department of AI&ML, NCEH. 2
MODULE -1
Tuesday 26 November 2024 . 3
MODULE -1
The Basic Arithmetic Operators
Tuesday 26 November 2024 . 4
MODULE -1
Tuesday 26 November 2024 . 5
MODULE -1
Tuesday 26 November 2024 . 6
MODULE -1
Tuesday 26 November 2024 . 7
MODULE -1
Java’s Selection Statements
if
Tuesday 26 November 2024 . 8
MODULE -1
Here is a program that
uses an if-else-if
ladder to determine
which season a
particular month is in.
Tuesday 26 November 2024 . 9
MODULE -1
Tuesday 26 November 2024 . 10
MODULE -1
Tuesday 26 November 2024 . 11
MODULE -1
Tuesday 26 November 2024 . 12
MODULE -1
Jump Statements
Java supports three jump statements:
break,
continue,
and return.
Tuesday 26 November 2024 . 13
MODULE -1
Using break
In Java, the break statement has three uses.
First, as you have seen, it terminates a statement sequence in a switch statement.
Second, it can be used to exit a loop.
Third, it can be used as a “civilized” form of goto.
Tuesday 26 November 2024 . 14
MODULE -1
Tuesday 26 November 2024 . 15
MODULE -1
Tuesday 26 November 2024 . 16
MODULE -1
Tuesday 26 November 2024 . 17
MODULE -1
Tuesday 26 November 2024 . 18
MODULE -1
Introducing Classes
The class is at the core of Java.
It is the logical construct upon which the entire Java language is built
because it defines the shape and nature of an object.
As such, the class forms the basis for object-oriented programming in Java.
Any concept you wish to implement in a Java program must be
encapsulated within a class.
Tuesday 26 November 2024 . 19
MODULE -1
Class Fundamentals The General Form of a Class
A class is that it defines a new data type.
Once defined, this new type can be used to create objects
of that type.
Thus, a class is a template for an object, and an object
is an instance of a class.
Tuesday 26 November 2024 . 20