Professional Documents
Culture Documents
2
Outline
Conversion Between
Java Data Types
3
Conversion
Outline
Between Types
➢ The process of converging from one type to another is called
casting. It needed when some method return data type in
different form we needed.
There two type of casting :
➢ Widening casting (implicit): e.g. int i = 'A';
Byte Short
int long float double
char
4
Conversion Between
Outline
Types: Example 1
5
Conversion Between
Outline
Types: exercise
6
Java
Outline
Arrays
Arrays
7
Java Programming
Outline Language
8
Outline
❖ The above code line will order the computer to reserves five storage locations as shown
below: X[0]
X[1]
X[2]
X[3]
X[4]
9
Java Programming
Outline Language
❖ Notes :
1. In Java subscription starts with the value [0].
2. The following are valid statements;
❑ aNumber = number[0] + 10;
❑ number[4] = number[0] + number[2];
❑ number[2] = x[5] + y[10];
❑ value[6] = number[i] * 3;
10
Java Programming
Outline Language
❑creating an array:
❑ To create an array we need to follow the steps bellow:
➢ Declaration of the array.
➢ Creating the memory locations.
➢ Putting values into the memory locations.
➢ Implementing any type operations on the array.
11
Java Programming
Outline Language
Declaration of an array:
❖ Array's in Java can be decleared in two forms:
✓ " type " " arrayname[]";
✓ " type " [] " arrayname ";
For example:
✓ int number[];
✓ float average[];
✓ int [] counter;
✓ float [] marks;
▪ Note :
Do not enter the size of the array in the declaration section.
12
Java Programming
Outline Language
13
Java Programming
Outline Language
Initialization of an array:
❖ Up to now we created an array an allocated the necessary memory for it, to initialize
the array we need to write the following code line:
" arrayname[memory location] = value "
❖ For example:
✓ number[0] = 35;
✓ number[1] = 40;
✓ and so on until all values are enterd to the array.
14
Java Programming
Outline Language
▪ Note :
❖ In Java the array must start with (0), num[0] but not num[1] or any other numbers.
❖ Unlike C, Java protect array's from ( overruns) and ( underruns), trying to access an array
bound, its boundaries will generate error massage.
❖ We can also initialize arrays automatically in the same way as the ordinary variables when
they are declared, as shown:
" type arrayname[] = { list of values}; "
For example:
int number[] = {35, 40, 20, 57, 19};
as a result of the above code line the both arrays will have the same values.
15
Java Programming
Outline Language
❖ Loops are used to initialize the arrays to make it easy to enter the values to the array, as
shown below:
) وعداها سوف تكون القيمة واحد لكل القيم49 الى0 ( في هذا المثال المخرج سوف يكون صفر لكل العناصر التي تبدا من
.)99 - 50 ( التي في النطاق
16
Java Programming
Outline Language
▪ Note :
We can extract the size of an assay directly by implementing the following code
lene;
" int aSize = a.length; "
▪ H.W. :
Write a code to create two arrays and implement
the following operations :
1. Summation [] = A[] + B[].
2. Multiplication [] = A[] * B[].
17
Java Programming
Outline Language
Arrays
18
Java Programming
Outline Language
• Two dimensional arrays:
▪ So far we have learned how to store a list of values using Java, but what if we have a
table of values we need to store it as shown below:
Item 1 Item 2 Item 3
Sales Personal 1 310 275 365
Sales Personal 2 210 190 325
▪ The above table contains a 12 values, three each line, so it is not useful to declare an
array for each line, so we need to declare a two dimension array, the way of handling it
is not very different from the type of one dimensional array as it will be shown below.
19
Java Programming
Outline Language
▪ For creating 2D arrays, we must follow the same steps as that of 1D arrays, we
may create a 2D array like the following code lines:
Note that the <data type> is the kind of values the array will store, after giving
the data type give the array name and don’t declare any size for it, in the next
line we will use the "new" operator to initialize the size of the array, more over
"r" is the number of "rows", and "c" is the size of the columns.
20
Java Programming
Outline Language
Example:
int myArray [ ] [ ];
myArray = new int [3] [4];
OR
int table [ ] [ ] = new int[3] [4];
This will create a table that can store 12 integer values, four across and three down.
21
Java Programming
Outline Language
▪ Like the one dimensional array's the two dimensional array's may be initialized by
the following their declaration with a list of initial values enclosed in braces, for
example:
The following gives the same result:
1. int table [2] [3] = (0,0,0,1,1,1);
2. int table [] [] = { {0,0,0} , {1,1,1} };
3. int table [] [] = {
{0,0,0},
{1,1,1}
};
22
Java Programming
Outline Language
X[0] [1]
X[0] = new int [2];
X[1][3]
X[1] = new int [4];
X[2][2]
X[2] = new int [3];
23
Java Programming
Outline Language
• H.W.:
Create an array of 2D and build the following
array:
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
24
THANK YOU
25