Professional Documents
Culture Documents
Output Operation
System.out.println();
Input Operation
Import scanner package
import java.util.Scanner;
Create scanner object
Scanner reader=new Scanner(System.in)
Read the value
int num1=reader.nextInt();
11/18/2021
Class declaration and Objects instantiation
5
11/18/2021
7
Creating objects
A class provides the blueprint for objects: you create
an object from a class.
In Java, you create an object by creating an instance of a
class or, in other words, instantiating a class.
Often, you will see a Java object created with a
statement like this one:
Rectangle r1= new Rectangle();
This single statement actually performs three actions:
declaration, instantiation, and initialization.
11/18/2021
8
Declaring an object
Declaring a variable to hold an object is just like declaring a
variable to hold a value of primitive type: type name
Rectangle r1;
Instantiating an Object
The new operator instantiates a class by allocating memory
for a new object of that type.
new Rectangle();
Initializing an Object
Rectangle r1= new Rectangle();
11/18/2021
Instance and Static members
9
Instance members
Each object created will have its own copies of the
fields defined in its class. The fields of an object are
called instance variables.
The methods of an object define its behavior. These
methods are called instance methods. It is important
to note that these methods pertain to each object of the
class.
Instance variables and instance methods, which belong
to objects, are collectively called instance members.
11/18/2021
10
Example
int cal;
float min=1;
void display(int x)
{
….
}
11/18/2021
11
Static members
Methods and variables defined inside the class are called an
instance methods and instance variables. That is, a separate
copy of them is created upon creation of each new object.
But in some cases it si necessary to define a member that is
common to all the objects and accessed without using a
particular object.
That is, the member belongs to the class as a whole rather
than the objects created form the class. Such members can
be created by preceding them with the keyword static.
11/18/2021
12
Example
static int cal;
static float min=1;
static void display (int x) { …}
When a member is declared static, it can be
accessed before any objects of its class are created,
and without reference to any object.
We can declare both method and variables to be
static
11/18/2021
13
11/18/2021
Instance and Static Variables
14
Instance variables
Instance variables are declared in a class, but outside a
method, constructor or any block.
Instance variables are created when an object is created
with the use of the keyword 'new' and destroyed when the
object is destroyed.
Instance variables can be accessed directly by calling the
variable name inside the class.
However, within static methods (when instance variables are given
accessibility), they should be called using the fully qualified name.
ObjectReference.VariableName.
11/18/2021
15
11/18/2021
16
11/18/2021
17
Example:
Class_name.cal=10;
Class_name.min=2;
Class_name.display(4);
11/18/2021
Methods and their components
18
11/18/2021
19
Creating Method
Considering the following example to explain the syntax of a method −
Syntax
[modifier] returnType nameOfMethod ([Parameter List])
{ // method body }
Here,
modifier − It defines the access type of the method and it is optional to use.
returnType − Method may return a value.
nameOfMethod − This is the method name. The method signature consists of the
method name and the parameter list.
Parameter List − The list of parameters, it is the type, order, and number of
parameters of a method. These are optional, method may contain zero parameters.
method body − The method body defines what the method does with the
statements.
11/18/2021
20
Example
11/18/2021
21
Method Calling
For using a method, it should be called. There are two
ways in which a method is called i.e., method returns a
value or returning nothing (no return value).
The process of method calling is simple. When a program
invokes a method, the program control gets transferred to
the called method.
This called method then returns control to the caller in two
conditions, when −
the return statement is executed.
it reaches the method ending closing brace.
11/18/2021
22
11/18/2021
23
Method Overloading
When a class has two or more methods by the same
name but different parameters, it is known as method
overloading.
Itis different from overriding. In overriding, a method has
the same method name, type, number of parameters, etc.
Example
11/18/2021
24
11/18/2021
Constructor
25
11/18/2021
26
11/18/2021
27
11/18/2021
28
Example
11/18/2021
29
11/18/2021
30
Example
11/18/2021
Exercise 1:
31
Write a Java program that accept two integers and return true
if either one is 5 or their sum or difference is 5.
Class name: Program2, TestProgram2(Main method)
Attributes: number1 and number2
Method: checkIf5
Sample Output:
Enter the first number: 5
Enter the second number: 3
True
11/18/2021
Exercise 3:
33
11/18/2021
Exercise 5:
35
11/18/2021
Contents
1. Overview
2. Class declaration and Objects instantiation
3. Static and Instance members
4. Methods and their components
5. Constructors (Default, parameterized and
Overloaded)
6. Access modifiers
7. Accessors and Mutators
8. Array ad String
11/18/2021
Access Modifiers
38
11/18/2021
39
11/18/2021
Example
40
11/18/2021
41 11/18/2021
42
11/18/2021
Example
43
11/18/2021
44
11/18/2021
45 11/18/2021
46
11/18/2021
47 11/18/2021
48 11/18/2021
Contents
1. Overview
2. Class declaration and Objects instantiation
3. Static and Instance members
4. Methods and their components
5. Constructors (Default, parameterized and
Overloaded)
6. Access modifiers
7. Accessors and Mutators
8. Array ad String
11/18/2021
Accessors and Mutators
50
11/18/2021
51
Accessors
An Accessor method is commonly known as a get
method or simply a getter.
They are declared as public.
A naming scheme is followed by accessors, in other words
they add a word to get in the start of the method name.
They are used to return the value of a private field.
The same data type is returned by these methods
depending on their private field.
11/18/2021
52
Example
11/18/2021
53
Mutator
A Mutator method is commonly known as a set method or
simply a setter.
A Mutator method mutates things, in other words change things. It
shows us the principle of encapsulation.
They are also known as modifiers. They are easily spotted
because they started with the word set.
They are declared as public.
Mutator methods do not have any return type and they also
accept a parameter of the same data type depending on their
private field. After that it is used to set the value of the private
field.
11/18/2021
54
Example
11/18/2021
Contents
1. Overview
2. Class declaration and Objects instantiation
3. Static and Instance members
4. Methods and their components
5. Constructors (Default, parameterized and
Overloaded)
6. Access modifiers
7. Accessors and Mutators
8. Array ad String
11/18/2021
Array in Java
56
11/18/2021
57
Syntax
dataType [] arrayRefVar; // preferred way. or
dataType arrayRefVar []; //works but not preferred way.
Example
double[] myList; or double myList[];
11/18/2021
58
Creating Arrays
You can create an array by using the new operator with
the following syntax −
Syntax
arrayRefVar = new dataType[arraySize];
The above statement does two things −
It creates an array using new dataType[arraySize].
It assigns the reference of the newly created array to the
variable arrayRefVar.
11/18/2021
59
11/18/2021
60
Example
Following statement declares an array variable,
myList, creates an array of 10 elements of double type
and assigns its reference to myList −
double[] myList = new double[10];
Following picture represents array myList. Here,
11/18/2021
61
11/18/2021
62
Array Length
In Java, the array length is the number of elements
that an array can holds. There is no predefined method
to obtain the length of an array.
We can find the array length in Java by using the
array attribute length. We use this attribute with the
array name.
The length property can be invoked by using the dot
(.) operator followed by the array name.
11/18/2021
64
Processing Arrays
When processing array elements, we often use
either for loop or for-each loop because all of the
elements in an array are of the same type and the size
of the array is known.
11/18/2021
65
11/18/2021
Exercise:
66
Multidimensional arrays
Multidimensional arrays with two dimensions are often
used to represent tables of values consisting of information
arranged in rows and columns.
To identify a particular table element, we must specify two
indices. By convention, the first identifies the element’s row and
the second its column.
Arrays that require two indices to identify a particular
element are called two-dimensional arrays.
(Multidimensional arrays can have more than two
dimensions.)
In general, an array with m rows and n columns is called
an m-by-n array. 11/18/2021
68
Example
int [][] myArray = new int [3][4];
int [][] myArray = { {1,2,3,4}, {5,6,7,8},
{9,10,11,12}};
11/18/2021
69
11/18/2021
71
11/18/2021
72
11/18/2021
74
11/18/2021
String in Java
75
11/18/2021
76
String Literal
Java String literal is created by using double quotes.
For Example:
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.
String s1="Welcome";
String s2="Welcome";//It doesn't create a new instance
11/18/2021
77
By new keyword
String s=new String("Welcome");
//creates two objects and one reference variable
In such case, JVM will create a new string object in normal
(non-pool) heap memory, and the literal "Welcome" will be
placed in the string constant pool. The variable s will refer
to the object in a heap (non-pool).
11/18/2021
78
String constructors
Class String provides constructors for initializing
String objects in a variety of ways.
11/18/2021
79
11/18/2021
80
11/18/2021
81
11/18/2021
Miscellaneous String Methods
82
Method Description
boolean contains(CharSequence s) returns true or false after matching the sequence
of char value.
String replace(char old, char new) replaces all occurrences of the specified char
value.
String toLowerCase() returns a string in lowercase.
String toUpperCase() returns a string in uppercase.
String trim() removes beginning and ending spaces of this
string.
char[] toCharArray() returns a char array containing a copy of the
string’s characters.
11/18/2021
Exercise:
83
11/18/2021
84
Questions?
11/18/2021
85
Thank You
11/18/2021