Basic concept of java

Page 1

BASIC CONCEPT OF JAVA BY PSK TECHNOLOGIES PVT.LTD IT COMPANY NAGPUR WEBSITE:https://www.pskitservices.com/ ADDRESS:PLOT NO.780 NEAR DURGA TEMPLE, KATOL ROAD CHAONI,NAGPUR-13 CONTACT : 9975288300


Q: Can we override static method? A: No, static method cannot be overridden. It can be proved by runtime polymorphism, so we will learn it later. Q: Why we cannot override static method? A: because static method is bound with class whereas instance method is bound with object. Static belongs to class area and instance belongs to heap area. Q: Can we override java main method? A: No, because main is a static method.

WEBSITE:https://www.pskitservices.com/CONTACT:9975288300


#Difference between method overloading and method overriding in

Method Overloading 1) Method overloading is used to increase the readability of the program. 2) Method overloading is performed within class. 3) In case of method overloading, parameter must be different 4) Method overloading is the example of compile time polymorphism. 5)In java, method overloading can't be performed by changing return type of the

me only. Return type can be same or different in method overloading. But you must have


ď ą Method Overriding 1) Method overriding is used to provide the specific implementation of the method that is already provided by its super class. 2) Method overriding occurs in two classes (inheritance). 3) In case of method overriding, parameter must be same. 4) Method overriding is the example of run time polymorphism. 5) Return type must be same or covariant in method overriding. WEBSITE:https://www.pskitservices.com/CONTCAT:9975288300


/*this keyword in java*/ To refer the current class instance variable To invoke the current class constructor To invoke the current class method To pass as an argument in the method call To pass as an argument in the constructor call To return the current class instance Proving this keyword


/*Usage of java this keyword*/ Here is given the 6 usage of java this keyword. 1. This() keyword can be used to refer current class instance variable. 2. This() can be used to invoke current class constructor. 3 .This() keyword can be used to invoke current class method (implicitly) 4. This() can be passed as an argument in the method call. 5. This() can be passed as argument in the constructor call.


Multithreading in java with examples A thread is a light-weight smallest part of a process that can run concurrently With the other parts(other threads) of the same process. Threads are independent because they all have separate path of execution that’s the reason if an exception occurs in one thread, it doesn’t affect the execution of other threads. All threads of a process share the common memory. The process of executing multiple threads simultaneously is known as multithreading. The main purpose of multithreading is to provide simultaneous execution of two or more parts of a program to maximum utilize the CPU time. A multithreaded program contains two or more parts that can run concurrently. Each such part of a program called thread.


2. Threads are lightweight sub-processes, they share the common memory space. In Multithreaded environment, programs that are benefited from multithreading, utilize the maximum CPU time so that the idle time can be kept to minimum. 3. A thread can be in one of the following states: A thread can be in only one state at a given point in time. Multitasking vs Multithreading vs Multiprocessing vs parallel processing


If you are new to java you may get confused among these terms as they are used quite frequently when we discuss multithreading. Let’s talk about them in brief. Multitasking: Ability to execute more than one task at the same time is known as multitasking. Multithreading: We already discussed about it. It is a process of executing multiple threads simultaneously. Multithreading is also known as Thread-based Multitasking. Multiprocessing: It is same as multitasking, however in multiprocessing more than one CPUs are involved. On the other hand one CPU is involved in multitasking. Parallel Processing: It refers to the utilization of multiple CPUs in a single computer system. Creating a thread in Java


Before we begin with the programs (code) of creating threads, let’s have a look at these methods of Thread class. We have used few of these methods in the example below. •

getName(): It is used for Obtaining a thread’s name

getPriority(): Obtain a thread’s priority

isAlive(): Determine if a thread is still running

join(): Wait for a thread to terminate

run(): Entry point for the thread

sleep(): suspend a thread for a period of time


 Methods: isAlive() and join() •In all the practical situations main thread should finish last else other threads which have spawned from the main thread will also finish. •To know whether the thread has finished we can call is Alive() on the thread which returns true if the thread is not finished. •Another way to achieve this by using join() method, this method when called from the parent thread makes parent thread wait till child thread terminates. •These methods are defined in the Thread class. •We have used is Alive() method in the above examples too. WEBSITE:https://www.pskitservices.com/CONTACT:9975288300


 Synchronization •Multithreading

introduces asynchronous behavior to the programs. If a thread is writing some data another thread may be reading the same data at that time. This may bring inconsistency. •When two or more threads need access to a shared resource there should be some way that the resource will be used only by one resource at a time. The process to achieve this is called synchronization. •To implement the synchronous behavior java has synchronous method. Once a thread is inside a synchronized method, no other thread can call any other synchronized method on the same object. All the other threads then wait until the first thread come out of the synchronized block. WEBSITE:https://www.pskitservices.com/CONTACT:997528300


•When we want to synchronize access to objects of a class which was not designed for the multithreaded access and the code of the method which needs to be accessed synchronously is not available with us, in this case we cannot add the synchronized to the appropriate methods. In java we have the solution for this, put the calls to the methods (which needs to be synchronized) defined by this class inside a synchronized block in following manner. Synchronized (object) { // statement to be synchronized } WEBSITE:https://www.pskitservices.com/CONTACT:9975288300


To make a method synchronized, simply add the synchronized keyword to its declaration. Then no two invocations of synchronized methods on the same object can interleave with each other. 1.Synchronized statements must specify the object that provides the intrinsic synchronized keyword is used for exclusive accessing. 2.Lock. When synchronized (this) is used, you have to avoid to synchronizing invocations of other objects' methods. 3.Wait() tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ). 4.Notify() wakes up the first thread that called wait() on the same object. WEBSITE:https://www.pskitservices.com/CONTACT:9975288300


Synchronized ,Wait, notify and notifyall When a thread running in a synchronized method of an object is calling the wait() method of the same object, that thread releases the lock of the object and is added to that object's waiting queue. As long as it's there, it sits idle. Note also that wait() forces the thread to release its lock. This means that it must own the lock of an object before calling the wait() method of that (same) object. Hence the thread must be in one of the object's synchronized methods or synchronized block before calling wait().

WEBSITE:https://www.pskitservices.com/CONTACT:9975288300


When a thread invokes an object's notify() or notifyAll() method, one (an arbitrary thread) or all of the threads in its waiting queue are removed from the waiting queue to the entry queue. They then actively contend for the object's lock, and the one that gets the lock goes on to execute. If no threads are waiting in the waiting queue, then notify() and notifyAll() have no effect. Before calling the notify() or notifyAll() method of an object, a thread must own the lock of the object. Hence it must be in one of the object's synchronized methods or synchronized block.

WEBSITE:https://www.pskitservices.com/CONTACT:9975288300


OUR SOFTWARE COURSES


OUR HARDWARE COURSES


WEBSITE DESIGNING

WEBSITE:https://www.pskitservices.com/CONTACT:9975288300


DIGITAL MARKETING

WEBSITE:https://www.pskitservices.com/CONTACT:9975288300


IT TRAINING

WEBSITE:https://www.pskitservices.com/CONTACT:9975288300


SALES AND SERVICES

WEBSITE:https://www.pskitservices.com/CONTACT:9975288300


THANK YOU FOLLOW US ON:

WEBSITE:https://www.pskitservices.com/CONTACT:9975288300


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.