KEMBAR78
Lecture 2 Java | PDF | Data Type | Integer (Computer Science)
0% found this document useful (0 votes)
9 views34 pages

Lecture 2 Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views34 pages

Lecture 2 Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Programming Language II

CSE-215
Prof. Dr. Mohammad Abu Yousuf
yousuf@juniv.edu

1
Data Types, Variables, and Arrays

2
• In java, there are two types of data types
– primitive data types
– non-primitive data types

3
• Java defines eight primitive types of data: byte, short, int, long,
char, float, double, and boolean. The primitive types are also
commonly referred to as simple types. These can be put in four
groups:
– Integers This group includes byte, short, int, and long,
which are for whole-valued signed numbers.
– Floating-point numbers This group includes float and
double, which represent numbers with fractional precision.
– Characters This group includes char, which represents
symbols in a character set, like letters and numbers.
– Boolean This group includes boolean, which is a special
type for representing true/false values.
4
Integers

• Java defines four integer types: byte, short, int, and long.
All of these are signed, positive and negative values.

5
Integers

byte:
• The smallest integer type is byte. This is a signed 8-bit
type that has a range from −128 to 127.

• Byte variables are declared by use of the byte keyword.


For example, the following declares two byte variables
called b and c:
byte b, c;

6
Integers

short:
• short is a signed 16-bit type. It has a range from −32,768
to 32,767. It is probably the least-used Java type. Here
are some examples of short variable declarations:
short s;
short t;

7
Integers

long:
• long is a signed 64-bit type and is useful for those
occasions where an int type is not large enough to hold
the desired value.

• For example, the following is a program that computes


the number of miles that light will travel in a specified
number of days:

8
In 1000 days light will travel
about 16070400000000 miles.
9
Characters

• char in Java is not the same as char in C or C++. In C/C+


+, char is 8 bits wide. This is not the case in Java. Instead,
Java uses Unicode to represent characters.

• Unicode defines a fully international character set that


can represent all of the characters found in all human
languages. It is a unification of dozens of character sets,
such as Latin, Greek, Arabic, Cyrillic, Hebrew, Katakana,
Hangul, and many more.
• For this purpose, it requires 16 bits. Thus, in Java char is
a 16-bit type. The range of a char is 0 to 65,536. There
are no negative chars. 10
Characters

• Here is a program that demonstrates char variables:

ch1 and ch2: X Y


11
Characters

• Notice that ch1 is assigned the value 88, which is the


ASCII (and Unicode) value that corresponds to the letter
X. As mentioned, the ASCII character set occupies the first
127 values in the Unicode character set.

ch1 contains X
ch1 is now Y

12
Booleans

13
Booleans

• Output of previous program:

b is false
b is true
This is executed.
10 > 9 is true

14
Arrays
• Arrays:
• The general form of a one dimensional array declaration is:
dataType[] arr; (or)
dataType []arr; (or)
dataType arr[];

Example: int month_days[];


• new is a special operator that allocates memory.
• The general form of new as it applies to one-dimensional arrays
appears as follows:
array-var = new type [size];
Example: month_days = new int[12]; 15
Arrays
• It is possible to combine the declaration of the array
variable with the allocation of the array itself, as shown
here:
int month_days[] = new int[12];

• This is the way that you will normally see it done in


professionally written Java programs.
• Let’s review: Obtaining an array is a two-step process. First,
you must declare a variable of the desired array type.
Second, you must allocate the memory that will hold the
array, using new, and assign it to the array variable. Thus,
in Java all arrays are dynamically allocated. 16
Arrays
class Testarray{
public static void main(String args[]){

int a[]=new int[5];//declaration and instantiation


a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;

//printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}
}
17
Passing Array to method in java
class Testarray2{
static void min(int arr[]){
int min=arr[0];
for(int i=1;i<arr.length;i++)
if(min>arr[i])
min=arr[i];
System.out.println(min);
}

public static void main(String args[]){


int a[]={33,3,4,5};
min(a);//passing array to method
}
}

Output: 3 18
Multidimensional Arrays
• Multidimensional Arrays:
• In Java, multidimensional arrays are actually arrays of
arrays.
• To declare a multidimensional array variable, specify each
additional index using another set of square brackets.
• For example, the following declares a two-dimensional
array variable called twoD:
dataType[][] arrayRefVar; (or)
dataType [][]arrayRefVar; (or)
dataType arrayRefVar[][]; (or)
dataType []arrayRefVar[];
– Example:
int twoD[][] = new int[4][5];
19
Multidimensional Arrays
class Testarray3{
public static void main(String args[]){

//declaring and initializing 2D array


int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
Output:1 2
3
//printing 2D array
245
for(int i=0;i<3;i++){
445
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}

}}
20
Multidimensional Arrays

• Example:

01234
56789
10 11 12 13 14
15 16 17 18 19

21
Multidimensional Arrays

• When you allocate memory for a multidimensional array,


you need only specify the memory for the first (leftmost)
dimension.
• You can allocate the remaining dimensions separately.
• For example, this following code allocates memory for
the first dimension of twoD when it is declared. It
allocates the second dimension manually.

22
Multidimensional Arrays

23
Multidimensional Arrays

• when you allocate dimensions manually, you do not need


to allocate the same number of elements for each
dimension.

• Since multidimensional arrays are actually arrays of


arrays, the length of each array is under your control. For
example, the following program creates a two
dimensional array in which the sizes of the second
dimension are unequal:

24
25
Multidimensional Arrays
• The array created by this program looks like this:

• The use of uneven (or irregular) multidimensional arrays


may not be appropriate for many applications, because it
runs contrary to what people expect to find when a
multidimensional array is encountered. However,
irregular arrays can be used effectively in some situations.
26
Multidimensional Arrays
• It is possible to initialize multidimensional arrays. To do so,
simply enclose each dimension’s initializer within its own set of
curly braces. The following program creates a matrix where each
element contains the product of the row and column indexes.

27
Alternative Array Declaration Syntax
• There is a second form that may be used to declare an array:
type[ ] var-name;
• Here, the square brackets follow the type speci􀉹er, and not
the name of the array variable.

• For example, the following two declarations are equivalent:


int al[ ] = new int[3];
int[ ] a2 = new int[3];

• The following declarations are also equivalent:


char twod1[ ][ ] = new char[3][4];
char[ ][ ] twod2 = new char[3][4]; 28
Alternative Array Declaration Syntax

• This alternative declaration form offers convenience when


declaring several arrays at the same time. For example,

int[] nums, nums2, nums3; // create three arrays

• creates three array variables of type int. It is the same as


writing
int nums[], nums2[], nums3[]; // create three arrays

29
What is the class name of java array?
• In java, array is an object. For array object, an proxy class
is created whose name can be obtained by
getClass().getName() method on the object.
class Testarray4{
public static void main(String args[]){

int arr[]={4,4,5};
Output:
Class c=arr.getClass();
I
String name=c.getName();

System.out.println(name);

}}
30
Call by value in java
• There is only call by class Operation{
value in java, not call int data=50;
void change(int data){
by reference. data=data+100;//
• If we call a method changes will be in the local variable only
passing a value, it is }
public static void main(String args[]){
known as call by Operation op=new Operation();
value. The changes System.out.println("before change "+op.data);
being done in the op.change(500);
called method, is not System.out.println("after change "+op.data);
}
affected in the calling }
method.
Output:
before change 50 In case of call by value original
after change 50 value is not changed. 31
Call by reference in java
class Operation2{
int data=50;

void change(Operation2 op){ In case of call by reference


op.data=op.data+100;// original value is changed if
changes will be in the instance variabl
we made changes in the
e
called method.
}
public static void main(String args[]){
Operation2 op=new Operation2(); If we pass object in place
of any primitive value,
System.out.println("before change " original value will be
+op.data); changed. In this example
op.change(op);//passing object we are passing object as a
System.out.println("after change "+o value.
p.data);

} 32
A Few Words About Strings

• The String type is used to declare string variables. You


can also declare arrays of strings. A quoted string
constant can be assigned to a String variable.

• Here, str is an object of type String. It is assigned the


string “this is a test”.

33
Thank You

34

You might also like