CHP 22
CHP 22
Programming (OOP)
Dr, SERHANE Oussama
[email protected]
Ecole Nationale Supérieure d’Informatique de Sidi Bel Abbes (ESI-SBA)
Second Year CPI
2022/2023
Primitive Non-Primitive
(intrinsic) (Derived)
Class
Numeric Non-Numeric
Array
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.
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
06 String replace(char old, char new) It replaces all occurrences of the specified char value.
… …. …
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
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:
• 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
• Write a Java program to print the sum of all the items of 2D array.
• 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.
• 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
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.
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