0% found this document useful (0 votes)
39 views37 pages

CHP 22

The document discusses object-oriented programming and Java programming language basics including data types, variables, operators, control flow, loops, methods, arrays, casting and strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views37 pages

CHP 22

The document discusses object-oriented programming and Java programming language basics including data types, variables, operators, control flow, loops, methods, arrays, casting and strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Object-Oriented

Programming (OOP)
Dr, SERHANE Oussama
[email protected]
Ecole Nationale Supérieure d’Informatique de Sidi Bel Abbes (ESI-SBA)
Second Year CPI
2022/2023

© all right reserved Esi-sba


What is this course about?
Chapter 01 : OOP General Introduction and Motivation
Chapter 02 : Introduction to Java Programming Language
Chapter 03 : Oriented Object Approach: Class and Object
Chapter 04 : Oriented Object Approach: heritage, Polymorphism, Interface …
Chapter 05 : Collections
Chapter 06 : Exception handling and Management
Chapter 07 : Graphical interface
Chapter 08 : Streams and Files
Object-Oriented Programming Esi-sba 2022/2023 2
Chapter 02:
Introduction to Java Programming Language
Part 02

Object-Oriented Programming Esi-sba 2022/2023 3


Chapter 02 Outline
 Motivation: Why Java ?
 Introduction
 Java Platforms & Terminology
 Basic Syntax
 Hello world
 Primitive Types and Variables
 Mathematical Operators
 Flow of Control
 Loops
 Command line arguments and Command line inputs
 Java String
 Casting
 Arrays
Object-Oriented Programming Esi-sba 2022/2023 4
 Java methods
Summary of Primitive Types and Variables
Data Types in Java

Primitive Non-Primitive
(intrinsic) (Derived)

Class
Numeric Non-Numeric
Array

Integer Float Interface

Characters Boolean Object


String

Why Java String are Non-Primitive Data type ?


Non-primitive data types in Java
• Java programming language has two types of data types
1. Primitive data types (predefined data types)
2. Non-primitive data types
• Non-primitive data types are called reference types because they refer
to objects memory i.e., it refers to the memory location where an object
is placed.
• All non-primitive data types are simply called objects that are created by
instantiating a class.
• The default value of any reference variable is null.
• Whenever we are passing a non-primitive data type to a method, we
are passing the address of that object where the data is stored.
Java String
• Generally, String is a sequence of characters.
• In Java, String is basically an object that represents sequence of char values.
• The java.lang.String class is used to create a string object.
How to create a string object? What is
difference ?
There are two ways to create String object:
1. String Literal

2. By new keyword
Java String Declaration
1. String Literal
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. For example:

2. By new keyword

In such case, JVM will create a new string object in normal (non-pool) “Hello java”
The variable s will refer to the object in a non-pool.

String constant pool


Java String declaration Example 01

Output
Why String is Class in Java ?
Why String is Class in Java ?
• The java.lang.String class provides many useful methods to perform
operations on sequence of char values.
N Method Description
01 char charAt(int index) It returns char value for the particular index

02 int length() It returns string length


String substring(int beginIndex, int
03 It returns substring for given begin index and end index.
endIndex)
04 boolean isEmpty() It checks if string is empty.

05 String concat(String str) It concatenates the specified string.

06 String replace(char old, char new) It replaces all occurrences of the specified char value.

07 int indexOf(int ch) It returns the specified char value index.

… …. …
Java String manipulation Example 02
Java String manipulation Example 03

• Write a Java program that asks a user to enter his name than allow him
to get the character of the given index within the String.
Output example:
Java String manipulation (Solving)
Java String manipulation (loop)
• Java code example that transfers String by adding space between characters
Java Basic Syntax : Casting
Java Basic Syntax : Casting
• Type casting is when you assign a value of one primitive data type to another
type. For instance: int = String or String = int.

• In Java, type casting could be used both ways manually and automatically

• There are two type of casting in java manually and automatically. The automatic
conversion is done by the compiler and the manual conversion is performed by
the programmer.
• Widening casting: is done automatically when passing a smaller size type to a larger size
type e.g: byte  short  char  int  long  float  double
• Narrowing casting: Converting a higher data type into a lower one is called narrowing type
casting e.g: double  float  long  int  char  short  byte 17
Java Basic Syntax : Casting full example

Execution result

Object-Oriented Programming Esi-sba 2022/2023 18


Java Conversion
• To convert String to an int in java using Integer.parseInt()
• To convert int to String in java using String.valueOf() or Integer.toString()
• Note: We can use String.format() method, string concatenation operator
 this method is used to format given arguments into String.

Visit https://www.javatpoint.com/java-int-to-string website form more examples


Java Basic Syntax : Arrays
Java Basic Syntax : Arrays
• In Java an array is a collection of similar types of data.
• Arrays are used to store a set of values in a single variable, instead of declaring
separate variables for each value.
Java array Syntax Java array Syntax with initialization
dataType [] arrayName; dataType [] arrayName = {value1, value2, …};

Java code example Java code example

But, how many elements can array this hold?


Java Basic Syntax : Arrays
• In java to define the number of elements that an array can hold, we
have to allocate memory for this array. For example:
Case01: declare then allocate memory Case 02: declare and allocate memory

Case 03: declare and allocate memory then Initialize

Notes:
• In java array indices always start from 0. Therefore,
the first element of an array is at index 0.
If the size of an array is n, then the last element of
the array will be at index n-1.
Java Basic Syntax : loop through an Array
• The following example outputs all elements in the cars array, using a
"for, for-each and while" loop:

Using a for loop For-Each loop (Syntax)

Using For-Each loop


Using a while loop
Java Basic Syntax : Arrays exercises
Todo:
1. Write a Java Program to print the sum of all the items of an array.

2. Write a Java Program to Sort an array.

3. Write a Java Program to Remove Duplicates From array.


Java Basic Syntax : Multidimensional Arrays
• In java, a multidimensional array is array that its value is an other array. The
following a declaration example of an array with 2 dimensions.
myNumbers matrix
Declare in Java
0 4 5 9
1 9 8 0
2 7 1 4
0 1 2

• To access the elements of the array, we should specify the two indexes:
one for the array, and the second for the element inside that array.
Java Basic Syntax : Multidimensional Arrays (code example)

myNumbers matrix

0 4 5 9
i 1 9 8 0
index
2 7 1 4
0 1 2

j
index

double nested loop (one loop Output result:


For jumping a line when print
for each index)
the output
Java Basic Syntax : 2D Arrays Challenge
Todo:
• Write a Java program to print the Length of 2D array.

• Write a Java program to print the sum of all the items of 2D array.

• Write a Java program to Sort an 2D array.

• Write a Java program to remove duplicates from 2D array.


Summary (java array)
• In java, arrays represent collections of related data all of the same data type.

• The size of an array is established at the time of creation and cannot be changed.

• Arrays can store either primitive data (int, String ...) or object reference data (See later in
the next chapter).

• When an array is created using the keyword new, all of its elements are initialized with a
specific value based on the type of elements for example:
 Type int are initialized to 0
 Type double are initialized to 0.0
 Type boolean are initialized to false
 Type String are initialized to null … etc.
Summary (java 2D array)
• In java, 2D array is stored as an array of arrays.

• 2D arrays are declared and created with syntax:


datatype[ ] [ ] variableName = new datatype[numberRows][numberCols];

• The square brackets [row][col] are used to access and modify an element in
a 2D array.
Java Methods
Java Methods
• In java, we called a method each a block of code that only runs when it is called.
• Methods or often called as function are used to perform certain actions. Therefore, we can
pass data, known as parameters, into a method.
To call a method

• myFirstMethod is the name of my function


• static means that the method belongs to the class and not an object.
• void means that this method does not have a return value.
Java Methods code example
• In the following example we declare a function with a print a statement
and then call it three time.

Execution result
Java Methods (Parameters and Arguments)
Code example

• Method Signature: in java, each method has a method signature. It is a part of the method
declaration that includes the method name and parameter list.

• Return Type: is a data type that the method will return. It may have a primitive data type,
object, void, etc. If the method does not return anything, we use the void keyword.

• The Access Specifier: see in the chapter 03


Java methods: Example 01

• Write a Java method to display the first 50 numbers.


Execution result

is there any possible improvements ?


Java methods: calculator code example

Execution result
Java methods: Challenge

• Write a Java method to find the smallest number among three numbers.
Possible solution
References
• https://www.geeksforgeeks.org/data-types-in-java/

• https://www.scaler.com/topics/non-primitive-data-types-in-java/

• https://www.javatpoint.com/non-primitive-data-types-in-java

• https://www.javatpoint.com/java-string

• https://www.javatpoint.com/string-comparison-in-java

• https://runestone.academy/ns/books/published/csawesome/Unit8-
2DArray/toctree.html?mode=browsing

You might also like