Sei sulla pagina 1di 11

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.

com 9831709283 7Nov2013 pg 1

Difference between Daemon and non-Daemon(U er !"read # $

Fig: Internal of Java Virtual machine %n one line main difference between daemon t"read and & er t"read i t"at a oon a all & er t"read fini " e'ec&tion (ava program or )*+ terminate it elf, )*+ doe n-t wait for daemon t"read to fini " t"ere e'ec&tion. / oon a la t non daemon t"read fini "ed )*+ terminate no matter how many Daemon t"read e'i t or r&nning in ide )*+. 1. /ny t"read created by main t"read, w"ic" r&n main met"od in )ava i by defa&lt non daemon beca& e !"read in"erit it daemon nat&re from t"e !"read w"ic" create it i.e. parent !"read and ince main t"read i a non daemon t"read, any ot"er t"read created from it will remain non-daemon &ntil e'plicitly made daemon by calling setDaemon(true). 2. Thread.setDaemon(true) ma0e a !"read daemon b&t it can only be called before tarting !"read in )ava. %t will t"row IllegalThreadStateException if corre ponding !"read i already tarted and r&nning. 3. Daemon !"read are &itable for doing bac0gro&nd (ob li0e "o& e0eeping, !"o&g" % "ave yet to & e it for any practical p&rpo e in application code. let & 0now if yo& "ave & ed daemon t"read in yo&r (ava application for any practical p&rpo e. Difference between Daemon and Non Daemon t"read in )ava "ere are co&ple of differences between daemon and user thread in Java$ 1# )*+ doe n-t wait for any daemon t"read to fini " before e'i ting.
Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg 1

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg 2

2# Daemon !"read are treated differently t"an U er !"read w"en )*+ terminate , finally bloc0 are not called, 1tac0 are not &nwo&nded and )*+ (& t e'it . Daemon !"read 2'ample in )ava

You can make any java thread as daemon thread. Daemon threads acts like service providers for other threads running in the same process. Daemon threads will be terminated by the JVM when there are none of the other threads running, it includes main thread of e ecution as well. !o specify that a thread is a daemon thread, call the setDaemon method with the argument true. !o determine if a thread is a daemon thread, use the accessor method isDaemon.

Daemon Thread Sample Code


"

1 2 3 4 5 6 7 8 1! 11 12 13 14 15 16 17

package com.myjava.threads; public class DaemonThread extends Thread! public DaemonThread()! setDaemon(true); " public void run()! System.out.println(#Is this thread Daemon$ % #&isDaemon()); " public static void main(String a'()! DaemonThread dt ) ne* DaemonThread(); ++ even you can set daemon constrain here also ++ it is like dt.setDeamon(true) dt.start(); "

"

Example Output
#s this thread Daemon" $ true

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg 2

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg 3

Java Inner Classes

Why Use Nested Classes?

"om#elling rea$on$ for u$ing ne$te% cla$$e$ inclu%e the following:

Logically grouping classes, readability & maintainability that are only used in one place. Like complex UI design and implementing complex algorithms.
It increases encapsulation: "on$i%er two to#&level cla$$e$' ( an% )' where ) nee%$ acce$$ to mem*er$ of ( that woul% otherwi$e *e %eclare% #rivate+ )y hi%ing cla$$ ) within cla$$ (' (,$ mem*er$ can *e %eclare% #rivate an% ) can acce$$ them+ In a%%ition' ) it$elf can *e hi%%en from the out$i%e worl%+

Nested Classes

-he Java #rogramming language allow$ you to %efine a cla$$ within another cla$$+ .e$te% cla$$e$ are %ivi%e% into two categorie$: $tatic an% non&$tatic+ .e$te% cla$$e$ that are %eclare% static are calle% static nested classes+ .on&$tatic ne$te% cla$$e$ are calle% inner classes+
class ,uter-lass ! ... static class Static.ested-lass ! ... " class Inner-lass ! ... " "

( ne$te% cla$$ i$ a mem*er of it$ enclo$ing cla$$+ .on&$tatic ne$te% cla$$e$ /inner cla$$e$0 have acce$$ to other mem*er$ of the enclo$ing cla$$' even if they are %eclare% #rivate+
Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg 3

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg 3

1tatic ne$te% cla$$e$ %o not have acce$$ to other mem*er$ of the enclo$ing cla$$+ ($ a mem*er of the ,uter-lass' a ne$te% cla$$ can *e %eclare% private' public' protected' or package private+ /2ecall that outer cla$$e$ can only *e %eclare% public or package private+0

Fig: /n %n tance of %nner4la

2'i t 5it"in an %n tance of 6&ter4la

Static inner class / wit" cla met"od and variable , a tatic ne ted cla i a ociated wit" it o&ter cla . /nd li0e tatic cla met"od , a tatic ne ted cla cannot refer directly to in tance variable or met"od defined in it enclo ing cla 7 it can & e t"em only t"ro&g" an ob(ect reference. Instance inner class / non- tatic inner cla "a t"e o&ter cla a an in tance variable, w"ic" mean it can only be in tantiated from &c" an in tance of t"e o&ter cla $ Anonymous inner class /n anonymo& inner cla can come & ef&l w"en ma0ing an in tance of an ob(ect w"ic" certain 8e'tra 8 &c" a overloading met"od , wit"o&t "aving to act&ally &bcla a cla . %n below e'ample we are & ing !"read & ing anonymo& inner cla . 1. /nonymo& cla i declared and initiali9ed im&ltaneo& ly. 2. /nonymo& cla m& t e'tend or implement to one and only one cla 3. / anonymo& cla "a no name, it can be & ed only once. or interface re p.

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg 3

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg :

class ,uter! private static String message ) #/ello0orld#; // Static nested class private static class 1essage2rinter! //Only static member of Outer class is directly accessible in nested static class public void print1essage()! // Compile time error if message field is not static System.out.println(#1essage 3rom nested static class 4 # & message); " " //non static nested class - also called Inner class private class Inner! // Both static and non static member of Outer class is accessible in this Inner class public void display()! System.out.println(# 1essage 3rom non static nested or Inner class 4 # & message); " " // How to create instance of static and non static nested class public static void main(String... args)! // creating instance of nested Static class ,uter.1essage2rinter printer ) new ,uter.1essage2rinter(); //calling non static method of nested static class printer.print1essage(); // creating instance of non static nested class or Inner class // In order to create instance of Inner class you need an Outer class instance ,uter outer ) new ,uter(); //outer class instance for creating non static nested class ,uter.Inner inner ) outer.new Inner();

//calling non static method of Inner class inner.display(); // we can also combine above steps in one step to create instance of Inner class ,uter.Inner nonStaticIner ) new ,uter().new Inner(); // similarly you can now call Inner class method nonStaticIner.display(); " " ,utput4 1essage 3rom nested static class 4 /ello0orld 1essage 3rom non static nested or Inner class 4 /ello0orld 1essage 3rom non static nested or Inner class 4 /ello0orld

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg :

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg ;

<. 5"y wo&ld yo& & e a ync"roni9ed bloc0 v . ync"roni9ed met"od= 1ync"roni9ed bloc0 place loc0 for "orter period t"an ync"roni9ed met"od . <. 5"at i volatile in )ava= %f a variable i declared a volatile t"en i g&aranteed t"at any t"read w"ic" read t"e field will ee t"e mo t recently written val&e. !"e 0eyword volatile will not perform any m&t&al e'cl& ive loc0 on t"e variable.

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg ;

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg 7

<. 5"at are wrapped cla e = Wrapped classes are classes that allow primitive types to be accessed as objects. : primitive type wrapper type
boolean byte char double 3loat int long short

Boolean Byte Character Double Float Integer Long

Short Java automatically converts from primitive types to wrapper types (autoboxing) and back (autounboxing) when warranted. Immutability. An immutable data type has the property that the value of an object never changes once constructed. By contrast, a mutable data type manipulates object values that are intended to change. Java's language support for helping to enforce immutability is the final modifier. When you declare a variable to be final, you are promising to assign it a value only once, either in an initializer or in the constructor. Code that could modify the value of a final variable leads to a compile-time error

Q : How do you express an is a relationship and a has a relationship or explain inheritance and composition? What is the difference etween composition and aggregation? !"e is a relation "ip i e'pre ed wit" inheritance and has a relation "ip i e'pre ed wit" composition. >ot" in"eritance and compo ition allow yo& to place &b-ob(ect in ide yo&r new cla . !wo of t"e main tec"ni?&e for
Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg 7

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg 8

code reuse are class inheritance and ob ect composition!

Inheritance i &ni-directional. @or e'ample House is a Building. >&t Building i not a House. %n"eritance & e e"tends 0ey word. Composition: i & ed w"en House has a Bathroom. %t i incorrect to ay House i a Bathroom. 4ompo ition imply mean & ing in tance variable t"at refer to ot"er ob(ect . !"e cla House will "ave an in tance variable, w"ic" refer to a Bathroom ob(ect. Which one to use? !"e g&ide i t"at in"eritance "o&ld be only & ed w"en subclass Ais aB superclass. DonBt & e in"eritance (& t to get code re& e. %f t"ere i no Ais aB relation "ip t"en & e compo ition for code re& e. 6ver& e of implementation inheritance (& e t"e Ce'tend D 0ey word# can brea0 all t"e &bcla e , if t"e &percla i modified. Do not & e in"eritance (& t to get polymorp"i m. %f t"ere i no Ais aB relation "ip and all yo& want i polymorphism t"en & e inter#ace inheritance wit" composition, w"ic" give yo& code reuse

Q. What is polymorphism? Eolymorp"i m may be defined a t"e ability of related ob(ect to re pond to t"e ame me age wit" different, b&t appropriate action . %n ot"er word , polymorp"i m mean ta0ing more t"an one form. Eolymorp"i m lead to two important a pect in 6b(ect 6riented terminology - @&nction 6verloading and @&nction 6verriding. 6verloading i t"e practice of &pplying more t"an one definition for a given f&nction name in t"e ame cope. !"e compiler i left to pic0 t"e appropriate ver ion of t"e f&nction or operator ba ed on t"e arg&ment wit" w"ic" it i called. 6verriding refer to t"e modification made in t"e &b cla to t"e in"erited met"od from t"e ba e cla to c"ange t"eir be"avior. Overloading vs Overriding

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg 8

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg 9

this would be considered to be method overloading if at least one of these 2 things is true: 5.) The number o3 parameters is di33erent 3or the methods 6.) The parameter types are di33erent. Fow to N6! overload met"od $ 5. 6. -hanging the return type o3 the method -hanging the name o3 the method parameters7 but not changing the parameter types.

Confused? Well, here are some very helpful examples of where overloading would be both valid and invalid: 2'ample of +et"od 6verloading in )ava G bot" valid and invalid$

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg 9

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg 10

++compiler error % can8t overload based on the ++type returned % ++(one method returns int7 the other returns a 3loat)4 int changeDate(int 9ear) ; 3loat changeDate (int 9ear); ++compiler error % can8t overload by changing just ++the name o3 the parameter (3rom 1onth to 9ear)4 int changeDate(int 1onth) ; 3loat changeDate(int 9ear); ++valid case o3 overloading7 since there is an ++extra parameter in the 3irst method4 int changeDate(int 9ear7 int 1onth) ; int changeDate(int 9ear); ++also a valid case o3 overloading7 since the ++parameters are o3 di33erent types4 int changeDate(3loat 9ear) ; int changeDate(int 9ear); What a out method o!erriding?

Overriding methods is completely different from overloading methods. f a derived class re!uires a different definition for an inherited method, then that method can be redefined in the derived class. "his would be considered overriding. #n overridden method would have the exact same method name, return type, number of parameters, and types of parameters as the method in the parent class, and the only difference would be the definition of the method. $et%s summari&e the differences between overloading and overriding. When overloading, one must change either the type or the number of parameters for a method that belongs to the same class. 'ut, overriding a method means that a method inherited from a base class is what%s being changed. # java private method cannot be overridden because in first place it is not accessible to an inheriting object. "n example for o!erriding from #a!a pac$age: mplicitly every object in (ava is extended from Object class. Object has a method named
Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg 10

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg 11

e!uals. "his implementation compares the passed object with the current object and returns true if the reference are same. n )tring class you don%t want this behaviour. "herefore, e!uals method of the Object class is overridden in )tring class with its own implementation. *ere the behaviour is modified to chec+ if the character se!uence is same as the compared one and returns true. "his is a classic example of overriding .%xample #a!a source code for o!erloading: public class ,verloadExample ! public static void main(String args'() ! System.out.println(play1usic(#- sharp#7#D sharp#)); System.out.println(play1usic(#-#7#D 3lat#7#E 3lat#)); " public static String play1usic(String c7 String d) ! return c&d; " public static String play1usic(String c7 String d7 String e)! return c&d&e; " " &!erriding: t is very simple and easy to understand. When you inherit an object, you don%t li+e certain behaviour and so you replace it with your own. ,ote the word replace. 'ecause after overriding the old behaviour is completely obsolete. ,ow loo+ at the image of a monster truc+. *ere the old small four wheels are replaced with huge wheels to suit the current need. "his is overriding

Java daemon thread, inner class, JVM etc compiled by sroychoudhury7@gmail.com 9831709283 7Nov2013 pg 11