Professional Documents
Culture Documents
4/6/2024
Overview
Most important basic elements for programming
languages are:
Programming Environment
Data Types, Variables, Keywords
If else conditions
Loops
Methods
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.
4/6/2024
7
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();
4/6/2024
Instance and Static members
8
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.
4/6/2024
9
Example
int cal;
float min=1;
void display(int x)
{
….
}
4/6/2024
10
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.
4/6/2024
11
Example
static int cal;
static float min=1;
4/6/2024
Instance and Static Variables
13
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.
4/6/2024
14
4/6/2024
15
4/6/2024
16
Example:
Class_name.cal=10;
Class_name.min=2;
Class_name.display(4);
4/6/2024
Methods and their components
17
4/6/2024
18
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.
4/6/2024
19
Example
4/6/2024
20
Method Calling
Forusing 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.
4/6/2024
21
4/6/2024
22
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
4/6/2024
23
4/6/2024
Constructor
24
4/6/2024
25
4/6/2024
26
4/6/2024
27
Example
4/6/2024
28
4/6/2024
29
Example
4/6/2024
Exercise 1:
30
4/6/2024
Exercise 2:
31
4/6/2024
Exercise 3:
32
4/6/2024
Exercise 5:
34
Method: checkNearest
◼ Sample Output:
Enter the first number: 89
Enter the second number: 92
92
4/6/2024
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
4/6/2024
Access Modifiers
37
4/6/2024
38
4/6/2024
Example
39
4/6/2024
40 4/6/2024
41
4/6/2024
Example
42
4/6/2024
43
4/6/2024
44 4/6/2024
45
4/6/2024
46 4/6/2024
47 4/6/2024
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
4/6/2024
Accessors and Mutators
49
4/6/2024
50
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.
4/6/2024
51
Example
4/6/2024
52
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.
4/6/2024
53
Example
4/6/2024
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
4/6/2024
Array in Java
55
4/6/2024
56
4/6/2024
57
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.
4/6/2024
58
4/6/2024
59
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, myList
holds ten double values and the indices are from 0 to 9.
4/6/2024
60
4/6/2024
61
4/6/2024
62
Array Length
InJava, 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.
4/6/2024
63
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.
4/6/2024
64
4/6/2024
Exercise:
65
4/6/2024
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. 4/6/2024
67
Example
int [][] myArray = new int [3][4];
int [][] myArray = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
4/6/2024
68
4/6/2024
70
4/6/2024
71
4/6/2024
73
4/6/2024
String in Java
74
4/6/2024
75
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
4/6/2024
76
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).
4/6/2024
77
String constructors
Class String provides constructors for initializing String
objects in a variety of ways.
4/6/2024
78
4/6/2024
79
4/6/2024
80
4/6/2024
Miscellaneous String Methods
81
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.
4/6/2024
Exercise:
82
4/6/2024
83
Questions?
4/6/2024
84
Thank You
4/6/2024