+ All Categories
Home > Documents > Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh...

Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh...

Date post: 22-Jun-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
26
1 What do you mean by wrapper class, why we need it? IMPORTANT QUESTIONS need it? 2 Arrays in JAVA 3 Deadlock 4 Constructor Overloading 5 What is garbage collection? How it works?
Transcript
Page 1: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

1 What do you mean by wrapper class, why we need it?

IMPORTANT QUESTIONS

need it?2 Arrays in JAVA3 Deadlock4 Constructor Overloading5 What is garbage collection? How it works?

Page 2: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

1. What do you mean by wrapper class, why we need it?

A Wrapper class is a class whoseobject wraps or contains a primitivedata types. When we create an objectto a wrapper class, it contains a fieldto a wrapper class, it contains a fieldand in this field, we can store aprimitive data types. In other words,we can wrap a primitive value into awrapper class object.

Page 3: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

Need of Wrapper Classes

They convert primitive data types into objects.Objects are needed if we wish to modify thearguments passed into a method (becauseprimitive types are passed by value).

The classes in java.util package handles only The classes in java.util package handles onlyobjects and hence wrapper classes help in thiscase also.

Data structures in the Collection framework,such as ArrayList and Vector, store only objects(reference types) and not primitive types.

An object is needed to support synchronizationin multithreading.

Page 4: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

Primitive Data types and their Corresponding Wrapper class

Page 5: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

Example

public class WrapperExample1

{

public static void main(String args[])

{

//Converting int into Integer//Converting int into Integer

int a=20;

Integer i=Integer.valueOf(a);//converting int into Integer

Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally

System.out.println(a+" "+i+" "+j);

}

}

Page 6: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

2. Arrays in JAVA

An array is a group of like-typedvariables that are referred to by acommon name.Arrays in Java workdifferently than they do in C/C++.differently than they do in C/C++.Following are some important pointabout Java arrays.

Page 7: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

In Java all arrays are dynamically allocated.

Since arrays are objects in Java, we can find their length usingmember length. This is different from C/C++ where we findlength using sizeof.

A Java array variable can also be declared like other variableswith [] after the data type.with [] after the data type.

The variables in the array are ordered and each have an indexbeginning from 0.

Java array can be also be used as a static field, a local variableor a method parameter.

The size of an array must be specified by an int value and notlong or short.

The direct superclass of an array type is Object.

Page 8: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh
Page 9: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

Advantages

Code Optimization: It makes the codeoptimized, we can retrieve or sort thedata efficiently.

Random access: We can get any data Random access: We can get any datalocated at an index position.

Page 10: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

Disadvantages

Size Limit: We can store only the fixedsize of elements in the array. It doesn'tgrow its size at runtime. To solve thisproblem, collection framework is usedproblem, collection framework is usedin Java which grows automatically.

Page 11: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

Types of Array

Single Dimensional Array

Multidimensional Array

Page 12: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

Single Dimensional Array

Syntax: dataType[] arr; (or)

dataType []arr; (or)

dataType arr[]; (or) dataType arr[]; (or)

arrayRefVar=new datatype[size];

Page 13: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

class Testarray

{

public static void main(String args[])

{

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

a[0]=10;//initialization

a[1]=20;

a[2]=70;

a[3]=40;

a[4]=50;

for(int i=0;i<a.length;i++)//length is the property of array

System.out.println(a[i]);

}

}

Page 14: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

Multidimensional Array

Syntax dataType[][] arrayRefVar; (or)

dataType [][]arrayRefVar; (or)

dataType arrayRefVar[][]; (or) dataType arrayRefVar[][]; (or)

int[][] arr=new int[3][3];//3 row and 3 column

Page 15: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

Example:class Testarray3

{

public static void main(String args[])

{

//declaring and initializing 2D array

int arr[][]={{1,2,3},{2,4,5},{4,4,5}};

//printing 2D array

for(int i=0;i<3;i++)

{

for(int j=0;j<3;j++)

System.out.print(arr[i][j]+" ");

System.out.println();

}

}

}

Page 16: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

3 Deadlock

Deadlock in java is a part ofmultithreading. Deadlock can occur ina situation when a thread is waiting foran object lock, that is acquired byan object lock, that is acquired byanother thread and second thread iswaiting for an object lock that isacquired by first thread. Since, boththreads are waiting for each other torelease the lock, the condition is calleddeadlock.

Page 17: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh
Page 18: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

Example

Give Example of THREAD WITHOUT SYNCHRONIZATION

Page 19: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

4 Constructor Overloading

In addition to overloading methods, wecan also overload constructors in java.

Overloaded constructor is called basedupon the parameters specified whenupon the parameters specified whennew is executed. Sometimes there is aneed of initializing an object indifferent ways. This can be done usingconstructor overloading.

Page 20: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

When do we need Constructor Overloading?

Sometimes there is a need ofinitializing an object in different ways.This can be done using constructoroverloading.overloading.

Page 21: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh
Page 22: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

Example:class conOLoad

{

conOLoad()

{

System.out.println("No parameters");

}

] conOLoad(int a)

{

System.out.println("a: " + a);System.out.println("a: " + a);

}

conOLoad(int a, int b)

{

System.out.println("a and b: " + a + " " + b);

}

}

class Overload

{

public static void main(String args[])

{

conOLoad o1 = new conOLoad();

conOLoad o2 = new conOLoad(10);

conOLoad o3 = new conOLoad(10,10);

}

}

Page 23: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

5 What is garbage collection? How it works?

In Java, the programmers don’t need to take careof destroying the objects that are out of use. TheGarbage Collector takes care of it.

Garbage Collector is a Daemon thread that keepsrunning in the background. Basically, it frees uprunning in the background. Basically, it frees upthe heap memory by destroying the unreachableobjects.

Unreachable objects are the ones that are nolonger referenced by any part of the program.

We can choose the garbage collector for our javaprogram through JVM options, we will look intothese in later section of this tutorial.

Page 24: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

Finalization

Just before destroying an object, GarbageCollector calls finalize() method on theobject to perform cleanup activities.Once finalize() method completes,Once finalize() method completes,Garbage Collector destroys that object.

Page 25: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

finalize() method is present in Objectclass with following prototype.

Syntax: protected void finalize() throws Throwable protected void finalize() throws Throwable

Page 26: Important.ppt · 1hhg ri :udsshu &odvvhv 7kh\ frqyhuw sulplwlyh gdwd w\shv lqwr remhfwv 2emhfwv duh qhhghg li zh zlvk wr prgli\ wkh dujxphqwv sdvvhg lqwr d phwkrg ehfdxvh

Advantage of Garbage Collection

It makes java memoryefficient because garbage collectorremoves the unreferenced objects fromheap memory.heap memory.

It is automatically done by thegarbage collector(a part of JVM) so wedon't need to make extra efforts.


Recommended