41
JAVA Java---------released----25-5-1995 vendor---------- sun micro system Father--------------gosling. 1st name-----------OAK but change to java because a language OAK was already present at that time initial target---------small device (mobile) hello.c----compile---hello.exe(native code/object code) No intermediate file generated Hello.java---java compiler(JVM)--Hello.class (byte code)---java interpreter-----(native code) Intermediate compile file(.class) is generated .exe means native code which means that all the program are compile already on particular platform & further can you run on same platform(OS). Importance of java: ------- Platform Independent Object oriented Portable Support networking Robustness Security Simple and secure Java is OOPL: ------- java initially developed with the syntax of c language and object oriented Principle based on c++. Java provides the facility to developed some kind of programs and also supporting object oriented principle because of this we can say java is object oriented programming language. Java is platform independent:-independent of OS

01_JLC

  • Upload
    phani

  • View
    3

  • Download
    1

Embed Size (px)

DESCRIPTION

mnbkj

Citation preview

JAVAJava---------released----25-5-1995vendor---------- sun micro systemFather--------------gosling.1st name-----------OAK but change to java because a language OAK as already !resent at that timeinitial target---------small device "mobile#hello.c----com!ile---hello.e$e"native code%object code#&o intermediate 'ile generated(ello.java---java com!iler"J)*#--(ello.class "byte code#---java inter!reter-----"native code#+ntermediate com!ile 'ile".class# is generated.e$e means native code hich means that all the !rogram are com!ile already on !articular !lat'orm ,'urther can you run on same !lat'orm"O-#. Importance of java: ------- .lat'orm +nde!endent Object oriented .ortable -u!!ort netor/ing 0obustness -ecurity -im!le and secureJava is OOPL: ------- java initially develo!ed ith the synta$ o' c language and object oriented .rinci!le based on c11.Java !rovides the 'acility to develo!ed some /ind o' !rograms and also su!!orting object oriented !rinci!le because o' this e can say java is object oriented !rogramming language.Java is platform independent:-independent of OSjava is !lat'orm inde!endent beteen O- 2but de!endent on J)*"java virtual machine#because o' J)* and byte code e can say java is!lat'orm inde!endent. hen you com!ile yourjava !rogram you get the byte code hich can be under stableby J)*2 so i' you install J)* in your system then you can run your byte code in any Os ith the hel! o' J)*.JVM3 --------- java virtual machine3 -----it is logical re!resentation o' J04 "java run time environment#.hysically e ill get a so'tare called J04 into the machine hich ill hel! to understand the byte code . J04 is a !hysical one hich is available as so'tare here as J)* is a logical one hichis available as document.Java compiler: -----java com!iler is a !rogram hich is im!lemented in c language. java com!iler veri'y the java !rogram 'urther grammatical errors2 hen no errors 'ound then source code ill be converted into byte code ith the hel! o' J)* 2it available as single 'ile called javac.e$e.

java interpreter3-----javainter!reter isa !rogram hich is im!lemented in c language.java inter!reter res!onsibility is converting the byte code to native code and e$ecuting the native code line by line. -o'tare re5uired is java.e$e 'ile.earlier inter!reter as im!lemented at that time java as im!lemented 'or small a!!lication having less memory re5uiredso inter!reter uses2 hich chec/ and load one line at a time and then unloadthat and load ne$t line to 0A*2 but no due to global a!!lication re5uired , heavy a!!lication re5uired inter!reter is im!lemented as com!iler hich run the hole code at a time and result in e''icient and 'ast a!!lication.JIT: ----J+6 is just-in-time7 it is a com!iler here res!onsibility is converting the byte code tonative code. Old java inter!reter as re!lace ith J+6 com!iler to increase the !er'ormance o' java a!!lication .Chec T!":----J)* is !lat'orm inde!endent---------------FJ8K is !lat'orm inde!endent---------------6Java com!iler is !lat'orm inde!endent-----6Java inter!reter "J+6# is 9 9 -----F:%c11 is !lat'orm inde!endent3 ------------FJava is9 93-----------6J#$: -------- java develo!ment /it2 hich is a collection o' various tools tore5uire to develo!ed and run your java !rograms. -ome o' the develo!ed /its are J042 java com!iler2 java inter!reter and manymore.you can donload the jd/ 'rom sun site 2hen you donloading J8K you need to select the J8Khich is com!atible ith your O- .i.e. various so'tare !rovided 'or various O- .you need to donload onehich is suitable 'or your O- because J8K is !lat'orm inde!endent. Install J#$-----------install J#$ properl% in %o&r s%stemA'ter installation set the !ath and class !ath in command !rom!t as 'ollos3-- set !ath;.2=bin7set class !ath;=bin7Ahen you set a class!ath 'or library then these classes ill be available in all the 'olders o' your O-"!ersonal com!uter#.:lass!ath3 ---- =lib7+' you ant to access .e$e 'ile available in one 'older in any other 'olders o' O-2 you need to set the !ath .i' you ant to access .class 'ile available in one 'older in any other 'olders o' O-2 then you need to set class!ath.2hat are the 4a%s availa(le to set the path and classpath36here are to ays3 ---- 5sin' the command prompt 5sin' environment varia(le*y com!uter ------!ro!erties-----advance tab-------environment variable ---clic/ on ne button under user variable )ariable name----------!ath)ariable value --------- .2BC>=bin7)ariable name----------class!ath)ariable value --------- =lib7 6he !ath , class!ath hat are you setting in dos !rom!t is tem!orary here as it is !ermanent in environment variable % system variable.Java lan'&a'e Character set Literals $e%4ord Operators #ata t%peControl statement Varia(leArra%s Constants Character set: -------it allos---- digits "C-9#Al!habets "a-D 2A-E# -!ecial -ymbol"12-2@2 FG. H. I...J FK----L#$e%4ord : --------some have given some setu! ords ith some !rede'ined name. these ords are called as /eyords or reserved ords.#ata t%pe: ------- Moolean2 char2 byte2 sort2 long2 int2 'loat2 double ----------------N data ty!e-------Control statements: --- +'2 else2 'or2 do2 hile2 sitch2 case2 brea/2 de'ault2 continue2 goto "&O66Class!o(ject: -----class2 inter'ace2 ne2 return2 e$tends2 im!lement Access specifies: ---- !ublic2 !rivate2 !rotected-Modifies: ------ static2 'inal2 abstract2 volatile2 assert2 native2 strict2 synchroniDation2 transientPaca'e: ----!ac/age2 im!ort78ception handlin':--catch2 thros2 'inally2 try2 throMISC: -- instant2 null2 true2 'alse2 void2 const "&O6#5serdefined4ords:------sometimesyoumayget re5uirements7 youriteyour oncodeshich are called as user de'ined ords or identi'iers. Oou can use 'or variables2 constants2 class2classes etc. Identifiers are names 4hich %o& can 'ive to varia(le9 constant9 class9 interfaces etc Identifiers are names defined (% the pro'rammer (ased on the re:&irement2hen %o& 4ritin' the identifiers %o& sho&ld follo4 the follo4in' r&les: ------------- +' the identi'iers contains then al!habets2 digits2B2H. 1stcharacter must be in al!habets or H2B. Keyords shouldn?t use as identi'iers.,+;a(c "A(c ,+; "Ifoolean --- false 2h% java character is tain' t4o (%tes36o!rovide the internaliDation 'acility. Ahich allo you to su!!ort multi!le languages.Varia(le: ---------it is the name o' the many bloc/ hich contains the value. value in the variable can be changed .Varia(le #eclaration: --------- 8ata ty!e varBname7 int a7char c7 boolean b7Constant: ----- it is also called as 'inal variables. Ae can?t change the value o' 'inal variable.'inal int a;997 S%nta8-------------- 'inal data ty!e varBname;value7'inal int sum;9997Literal: ----- +t is a value2 it is one o' the 'olloing 'our ty!es3 ------ Inte'er literal Character literal "loatin' point literal Strin' literalInte'er literal: -- integer value is a collection o' digits ithout decimal !oint. 6here are three ty!es o' +nteger literal 8ecimal +P---------"C-9#------------12Q------------6otal--------1C Octal +P-------------"C-R#------------C12Q--------------9----------N (e$adecimal +P----"C-92A-F#------CS12Q-------------9--------1TDecimal IL: --- decimal integer contains the digits 'rom C to 9.e$!----------12QOctal IL3 --- octal integer value contains collection o' digits 'rom C to R and must starts ith C.Hexadecimal IL3 --- (e$adecimal +nteger value contains collection o' digits 'rom C to 9 and A to F and must start ith CS. 4$!3 --- CS12Q here S can be small or ca!italCharacter literal: --- hen a single character enclosed beteen single 5uotation"U U# is called as character literal. 4$!3 ------Us?2 U!?2 ?6?2 ?A?.4very character hich is enclosed beteen single 5uotation "U U#have some integer value.UA?; T52 Ua?;9R Ub?;9N"loatin' point literal: --- 'loating !oint value is collection o' digits ith decimal !oints.6here are to ay to re!resent the 'loating !oint number- #ecimal point notation-------------,+;-.C 78ponential point notation --------,+;D,0B-E F ,+; 7-EStrin' literals: ---- set o' character hich are enclosed beteen double 5uotation "9 9# modes is called as string literals. 4$!3 --------- 9abc 112QV2VrajivV2Vhello + am J8KV 2V 9----em!ty stringOP7GATOGSArithmetic operator ---------AH9-9D9!9)6Assi'nment operator-------- AF9HF9-F9DF9!F9)F6Gelational operator----------AI9IF9J9JF9FF9KF6Lo'ical operator--------------ALL9MM9K6Increment!#ecrement operator------ AHH 9 -- 6Ternar% operator----------------------- A3:6Ne4 operator>it4ise operator ------------------------A II9JJ9L9M9O6Instance operator Arithmetic operator:-- hen you combine arithmetic o!erator ith to o!erant then it called as arithmetic e$!ression. 4$!3--->1>2T@92Q-> 6he result o' arithmetic e$!ression may be int%'loat value. All arithmetic o!erators are binary o!erators 12- are also unary o!erator.Assi'nment operator: ----- All assignment o!erators are binary o!erators. Assignment o!erators areused to assign values o' right side o!erant to le't side o!erant.4$!3--b;a here value o' ais assign into the o!erator b.Ahen you use the assignment o!erators you should use same ty!e o' o!erant.4$!3--hen you rite a;b then a and b must have same data ty!e.-ometimes you may get a re5uirement to assign one ty!e o' value to another ty!e then you need to do t%pe castin'.Castin' is a process of convertin' val&e in one data t%pe to another data t%pe -S%nta8:------------ var,Adest-6 FAdata t%pe6 var+Aso&rce6 6here are to ty!e o'ty!e casting3------------- Implicit castin' 78plicit castin' Ahen you assign one variable to another and hen source is smaller and destination is bigger thenJ)* ill do the ty!e casting automatically2 this is called as im!licit casting.it is alsocalled as4idenin' e8p:-- converting integer to double 2this ill ha!!en automatically.Ahen you assign one value or variable to another variable and hen source is bigger and destinationis smaller then you have to do the casting. 6his is called as e$!licit casting and this is also called asnarro4in'- 78p: -- converting double to integer. Gelational operator: ---- all relational o!erators are binary o!erators relational o!erators 'orm the relational e$!ression hose result is Moolean values.Lo'ical operator: ---- in this ,,2WW2X"binary &O6 o!erator# is unary o!erator.logical o!erators 'ounda logical e$!ression hose result is a Moolean value.the o!erands o' logical o!erators must be aMoolean ty!e. A > A LL>AMM> KA T TT" " T " "" " " T "" T " " "TTIncrement!#ecrement operator :--- Ternar% operator: --- ternary o!erator is used to !er'orm sim!le condition chec/.S%nta83 -----var;"condition#Le$!ression13e$!ression274$!3 -------- ma$;"aYb#La3b7+st condition ill be e$ecuted .hen the condition is true then true bloc/ value is assign into the variable. Ahen the condition ill be 'alse then 'alse value is assign to the variable.Instance operator: ------ this is used too chec/ hether given object is object o' given class or not.Ne4 operator: ------ ne o!erator is used to create the object o' a class or used to allocate the memory 'or instance variable o' the class.>it4ise operator: ----- it is invo/ed on individual bits o' given no"ZZ2YY2,2W2[2\# JJ left shift II ri'ht shift L (it4ise AN# M(it4ise OG O(it4ise NOT B(it4ise POGControl statement :------------- If statement "or statement #o/4hile Contin&e >rea S4itch statement 2hile statementIf statement: -----------it is used to !er'orm conditional chec/. +t is used to !er'orm one tas/beteen to tas/ based on the condition. +' "condition# J %%statementK-tat $7 +' "condition# J %%statement1 K 4lseJ %%statement2 K-tat $7 +' "condition#J %%statement1 K+' else "condition#J%%-tatement2 K 4lseJ %%statementQ K-tat $7Ahen i' statement is 4ncrusted 'irst condition is evaluated2 i' the condition is true then true bloc/ is e$ecuted. Ahen the condition is 'alse then statement inside the 'alse bloc/ ill be e$ecuted.NOT7:--- inside the i' condition alays you should !ass Moolean ty!e variable.+' you !assing a non-Moolean ty!e the 'olloing com!ilation error ill be accrued +ncom!arable ty!es Found3int 0e5uire 3 Moolean"or statement :------ it is used to e$ecute set o' statement multi!le times.'or "initialiDation 7condition 7increment%decrement#J-tatement 1 ------ statement n7K-tatement $2hen for statement is enco&ntered then the follo4in' steps 4ill occ&rs: ------------------------ Fist initialiDation statement ill be e$ecuted "only once# :ondition ill be veri'ied +' the condition is true then statement inside the bloc/ ill be e$ecuted. 6hen increment%decrement statement ill be e$ecuted. A'ter increment%decrement again condition is veri'ied i' condition is true then same !rocess ill be re!eated. Ahen condition is 'alse then control ill come out the loo! and e$ecute statement $. 2hile statement : ------- this statement also do the same tas/ similar to 'or statement.S%nta8 :----+nitialiDation7 Ahile "condition# J+ncrement%decrement7K-tatement $7 Ahen hile statement is encountered2 condition ill be veri'ied 'irst2 hen the condition is true then statement inside hile bloc/ ill be e$ecuted and again condition ill be veri'ied. -tatement inside the hile bloc/ ill be e$ecuted as long as condition is true. Once the condition is 'alse then control ill come out the loo! and then statement $ ill be e$ecuted.do-4hile statement: ----------6his is used to e$ecute a set o' statement re!eatedly2 until the logical test results in 'alse. 6his is called the !ost-test loo!2 because the test 'or re!etition is made at the end o' each !ass.S%nta8: ------------do J -tatement7 Khile"condition#7 J+ncrement%decrement7K-tatement $7+n do-hile 'irst all the statement in the bloc/ ill be e$ecuted and then condition ill be veri'ied.Ahen the condition ill be true then loo! ill be re!eated and hen condition is 'alse then controlcome out the loo!.+n hile statement 'irst condition ill be veri'ied i' the condition is true then loo! ill be re!eated and condition ill 'alse then control come out the loo!.+n hile hen condition is 'alse 'irst time then statement inside the bloc/ ill e$ecute Eero time hereas in do-hile statement ill e$ecute at one time.NOT7: -------At least once the body o' do-hile is e$ecuted. Mecause the logical test2 'or re!etition o' the loo!2 is carried out at the end o' each !ass. S4itch statement: ---------------------+t is used to !er'orm single tas/ among multi!le tas/. -itch statement !rovides a multi!le ay branching.-ynta$3 ------- -itch "4$!ression#J:ase val13 statement 17 brea/7:ase val23 statement 27 brea/7 :ase valQ3 statement Q7 brea/7 8e'ault3 statement $7KAhen sitch statement is encountered2 'irst e$!ression ill be evaluated and resultant value o'e$!ression ill be com!ared ith values given in the cases. +' any matching case is 'ound then thestatement related to the case ill be e$ecuted and the control ill be trans'erred to end o' the sitchstatement. +' matching case is not 'ound then the de'ault case ill be e$ecuted.NOT7 :------- 6y!e o' e$!ression and case value is alays integer. Myte2 char2 short ty!es are also alloed because these three ty!es ill be converted to integer automatically. Moolean2 long2 'loat and double ty!es are not alloed. +' + am not riting brea/ cases2 all the cases ill be e$ecuted starting 'rom the matching case. Ae can rite de'ault case anyhere2 Mut at the end hen e are riting de'ault e no need to rite brea/ statement a'ter it. >rea and contin&e statement: ------------Mrea/ is used to trans'er the control to the end o' the bloc/ unconditionally:ontinue is used to trans'er the control to the beginning o' the bloc/ unconditionally as above.Finally brea/ is used to move the control to end o' the loo! and continue is used to move the control to the beginning o' the loo!.Mrea/ statement3 ------------------------ Ahile "condition#J %% statement Mrea/7%% end o' the loo! KContin&e statement: ------------------------ Ahile "condition#J %% statement :ontinue7%% beginning o' the loo! KArra%: --------Array is collection o' similar elements. +n java array are objects8ataBty!e]^.arrayBname ;ne dataBty!e ]siDe^74$!3 - int]^.$;ne int]Q^7Ahen you are declare the array e should not s!eci'y the siDe. Ahen s!eci'y the siDe .here you aretrying to use the inde$ hich is not available then array inde$ out o' bounds e$ce!tion is thro atrun time.6o dimensional arrays3 -----to dimensional arrays is nothing but array o' arrays. S%nta8: ------- data ty!e ]^ ]^ arrayBname;ne data ty!e ]siDe^]siDe^ +nt ]^]^$;ne int ]Q^]Q^7OOPS in JAVAObject oriented !rinci!les3 ------------------------ A(straction 7ncaps&lation Pol%morphism InheritanceA(straction: --- !roviding essential !ro!erty and o!eration o' an object by hiding internal things iscalled as abstraction.4$!3- -- abstracting the monitor is nothing but !roviding some !ro!erties li/e color2 height2 idth2com!any2 manu'acturing date2 cost etc2 and some o!eration li/e sitch O& 2sitch OFF2 increasebrightness2 decrease brightness etc. by hiding internal 'unctionality o' the monitor. 7ncaps&lation: ----- !lacing all the !ro!erties and o!eration o' an object at one !lace is called asenca!sulation. 6he !lace is called as a class and !ro!erties are called as variable and o!eration arecalled as method%behavior.In programming language3 -------- riting a java class by !lacing variables and method inside thatis called enca!sulation Pol%morphism: ---- one 'orm behaving di''erently in di''erent situation is called as !olymor!hism.4$!3 -----one button in :._ ill do to tas/ called as sitch O& and sitch OFF.Inheritance: ------ getting e$isting !ro!erty and o!eration o' an e$isting class to a ne class is called as inheritance. 6he e$isting class is called as !arent class% base class and the ne class is called as sub class% child class%derived class.In java: ------- the !rocess o' using the su!er class "!arent class# 'unctionality in subclass "child class# is called as inheritance.Class and o(jectsS%nta8 to declare class: ----------- ]*odi'ier^ class classBnameJ]*odi'ier^ data ty!e var12 var22 varQFF7 ---------------------------- ]*odi'ier^ return ty!e methodBname "data ty!e var12 dataty!e var2# J -tatement17 -taemnent27--------------- K K78p3-- class A J+nt a7+nt b7 )oid sho "# J -ystem.out.!rintln "a#7 -ystem.out.!rintln "b#7KKCreatin' o(ject:--------- :lassBname varBname;ne classBname"#7-tudent s;ne student"#7A obj ;ne A"#72hen the a(ove line is e8ec&ted then the follo4in' thin's 4ill happen:----------A o(j: ---------*emory ill be allocated 'or the re'erence variable obj o' ty!e class A and de'aultnull have assigned . obj &_PPN byte Ne4 operator: ---- this is used to allocate the memory 'or instance variable o' the class.+n the above class A there are to instance variables called a and b 2 so in thisne o!erator allocatethe memory 'or to variables aand b . AA63----i'constructorisavailablethenit ill becalledtoinitialiDetheobject orinitialiDetheinstance variable.F:---------- object address ill assign to re'erence variable object +n this e$am!le objis re'erence variable o' ty!e A and it can store address o' object o' ty!e A only. )ariable and methods o' the class are called as member o' the class.0e'erence variable.memberBname778p:---s-sho4A6* methods-sid* varia(leQo4 man% different t%pes of mem(ers %o& can 4rite insides the class 3Ae can rite 'our ty!es o' members in a class Varia(le >loc Method ClassT%pesof varia(le :---------------Class level variable3 ---------------------------- instance variable%non-static variable local variable 'inal variable static variable re'erence variableMethod level variable: ------------------- local variable 'inal variable !rimitive variable re'erence variableInstance varia(le: ---variable inside the class is called as instance variable.Ahen you creatingobject 'or class then memory ill be allocated 'or all instance variable. A'ter allocating memory 'orinstance variables J)* initialiDe the variable ith de'ault value.+nstance variable sco!e is ithin the class here they declare. -ometimes it ill access outside but itill need to re'er ith re'erence variable o' the class.Local varia(le:---6hevariablehich isdeclaredinsidethemethod iscalledas localvariable.*emory ill be allocated 'or local variable hen you call the method.Oou should initialiDe the local variable be'ore using otherise com!ilation error ill came.")ariable might not have been initialiDe#-co!e o' local variable is ithin the method here it is declare."inal varia(le: ---- variable hich are declare ith 'inal /eyord are called as 'inal variables. Ahen you declare the 'inal variable then you should assign them values. Only one value is assigned 'or 'inal variables2 it is 'i$ed and can?t be changed. +nstance variable can be a 'inal variable. Pocal variable can be a 'inal variable. Final variable can be ritten ithin the class or ithin the method.Static varia(le: ------- hen you create an object -tudent s;ne s"#7 -tudent class ill be loaded into main memory. +nt a7 !rimitive variable -tudent s re'erence variable )ariable hich are declare inside the class ith the static /eyord are called as static variables. *emory ill be allocated 'or static variables hen class is loaded into memory. A'ter allocating memory J)* initialiDe the static variable ith de'ault values. -co!e o' static variable is ithin the class here you de'ined2 sometimes you can use static variable outside the class ith object or ith class. Only one co!y o' memory ill be allocated 'or the static variables 'or all the object creation. *emory ill be allocated 'or local variables after callin' the method onl% *emory ill be allocated 'or instance variable hile %o& are creatin' the o(ject-#ifference (et4een instance and static varia(le: ----------- Instance varia(le )ariable declare inside the class ithout astatic /eyord is called instance variable *emory ill be allocated 'or the instancevariable hile you are creating the object. Ahen you create the multi!le objects 'or the class then the multi!le co!ies is created 'or instance variable. i.e. 1 co!y !er an object. +nstance variable can be re'erred only ith an object. Static varia(le )ariable declare inside the class ith a static /eyord is called staticvariable *emory ill be allocated 'or the static variable hile class is loading into memory Ahen you create the multi!le object 'or the class then only oneco!y o' static variable ill be allocated 'or all the object . i.e. 1 co!y 'or all objects. -tatic variable can be re'erred only ith an object and class name.#onRt load t4o class &ntil the memor% loaded for the class, is empt%-*emory ill be allocated 'or static variable "only one co!y# hen you create multi!le object 'or aclass. Ahere as in case o' instance variables multi!le co!y ill be created 'or multi!le objects.-tatic /eyord is not alloed 'or local variable.Varia(les are divided into t4o cate'or%: ---------- Primitive varia(le Geference varia(le Primitive varia(le )ariable declare ith any o' the !rimitivedata ty!e are called as !rimitive variables. *emory allocation is done 'or !rimitive variable based on !rimitive data ty!e used. 4$!3--int ---> bytedouble------N byte +nitial values o' !rimitive variables de!end on data ty!es used. Geference varia(le )ariables hich are declared ith any class ty!e are called as re'erence variables. Alays N bytes o' memory ill be allocated 'or re'erence variable. Alays null ill be assign as de'ault 'or the re'erence variable Similarit% :-----------Primitive varia(le.rimitive variables can be instance variable2 static variable and local variable.Geference varia(le0e'erence variables can be instance variable2 static variable and local variable-Instance var- Local varia(le"inal var- Static varia(lePrimitivevar-SS SSGeference var-SS S SAe can rite one more ty!e o' member in the class called as (loc >locs ill be e$ecuted automatically henever you create object.>locs: -------- bloc/ is set o' statements hich are enclosed beteen JK.Ae can rite the bloc/ inside the method also. 6here bloc/s are called as local bloc/. Pocal bloc/sill be e$ecuted henever you call the methods inside a class. :reating object;creating instanceThere are t4o t%pe of (loc: --------- Instance (loc Static (loc Instance (loc Mloc/s de'ined inside a class ithout static /eyord is called as instance bloc/. +nstance bloc/ ill be e$ecuted hen youcreate the object"instance#Static (loc Mloc/s de'ined inside a class ith static /eyord is called as static bloc/. -taticbloc/ ill be e$ecuted henever class ill be loaded into memory-2hen a class 4ill (e loaded into memor% then follo4in' t4o thin's 4ill (e happens : ------ Memor% 4ill (e allocated for static varia(le Static (locs 4ill (e e8ec&ted- Ahen you are not riting any constructor in the class then J)* insert one de'aultconstructor hich is also called as im!licit constructor. Ahen you rite any constructor in the class then J)* ouldn?t insert any de'aultconstructor. Ahen you s!eci'y a return ty!e as constructor that ill be treated as method. it can call 2it ill bean object. :onstructor can?t be call ith an object.Methods: -------------*ethod re!resents some o!eration and it is sim!ly a bloc/ o' statementsith the 'olloing synta$.-ynta$ --------]modi'ier^ returnBty!emethodBname"!arameters# J -tatements 0eturn ty!e ----------- %%o!tional K*ethod ta/es some data as a !arameter and does some !rocess and return some value. -ome times. -ometimes ithout ta/ing any data "no !arameter# method can do some !rocess and return some data. *ethod can return only one value hen you are s!eci'ying the return ty!e that must be the data ty!e o' the value hich you are returning. +' you not returning any data ty!e then you should rite return ty!e void. Ahen return ty!e is void then no need to !rovide the return statement in method. Ahen return ty!es any other than void then e should !rovide the return statement in method.Can I 4rite m&ltiple ret&rn statements inside a method3Oes 2 e can rite multi!le returnstatement in the method conditionally i.e. all the return statementsyou have ritten must be reachable . 6he above synta$ is used to de'ine a method. once method is de'ined2 then e can call method ith the 'olloing synta$. ]variable ^;re'erence Bvariable. methodBname"!arameter#h.sho"#7h1.sho"1C22C#7sum; h2.sho"1C22C# 7 A method can ta/e multi!le !arameters. *ethod can return only one value. .arameter ty!e and return value ty!e may be !rimitive or re'erence.Static method: ----- *ethods hich are declared ith static /eyord is called as static method.+t can be invo/ed by the class name directly. Ae can also invo/e the static method ith the object.NOT7: --- +n the case o' instance method or non-static methods e have to use objects to invo/ethe methods. &on static variable cannot be re'erenced 'rom the static method or static bloc/.Static (locNon static (locStatic method Non- Static methodStatic varia(leT TT TNon-static varia(leP TP TStatic methodT TT TNon-static method P TP T :an + declare to! level class as 'inalLOes:an + declare to! level class as staticL&o:an + declare constructor as 'inalL&o:an + declare constructor as staticL&o:an + declare class level variable as staticLOes:an + declare method level variable as staticL &o:an + declare class level variable as 'inalLOes:an + declare method level variable as 'inalL&o:an + declare method as staticLOes:an + declare method as 'inalLOes:an + declare bloc/s as staticLOes:an + declare bloc/s as 'inalL&o:an you rite bloc/ inside the methodLOes2 thesebloc/sarecalledaslocal bloc/. Pocal bloc/ill bee$ecutedheneveramethodiscalled.Ahen you are riting the methods you need to rite some !arameter and some return ty!e hichre5uire. 6his !arameter and return ty!e may be o' !rimitive ty!e or re'erence ty!e also. Int m,Aint a9int (6 ------------- 4ith primitive Qello m+Ahello h6 --------------4ith reference t%pe Instance methods3 ------------- methods de'ined ithout static /eyord is called as instancemethods. Oou can call instance methods ith objects. Oou can re'er all /inds o' variables and methods inside the instance methods.Static methods: ------ methods hich are declared ith the static /eyord is called as static methods. Oou can call static methods ith the class name , also ith the object. Oou can re'er only static variables and static methods inside the static methods. +nstance variables are not alloed inside the static bloc/ and static method directly. +nstance method calling is not alloed inside static bloc/ and static method directly. +nstance variable2 static variable2 instance methods and static methods are alloed inside the instance bloc/ and instance method directly. Ae can call the instance variables and instance methods ith objects because instance member belongs to object. -tatic variable and static methods can be called ith class name because static members belongs to class static members can also be re'erred ith object.Qo4 to load the class3-ame ty!e o' more than one re'erence variable you cannot use as in the main class (ello h;ne (ello"#7 (ello h ; ne (ello"#7+t gives error as h is already de'ined in main "java.lang.string ]^#Mut h.m"#7h.m"#7&o !roblem java can use here m is a method.Ae can not declare any variable as a static inside any bloc/ including static bloc/ too and any method li/e.J-tatic int a;Q7 KOr m"# J-tatic int a7KMecause that variable is nothing but a local variable hich can not be declare static.(ence only instance variable can be declared as static.Only static bloc/ is e$ecuted as the class loading time as ell as memory is allocated 'or static variable andits value is assigned to it.+n static bloc/ e can also rite -tatic J int m;Q7-ystem.out.!rintln"m#7 K6his is e$ecuted only one time at class loading time. Mut local variable can never be declared as static. Call (% val&e V!S call (% referenceAhen you call a method by !assing !rimitive value as !arameters then it is called as call by value. Ahen you call a method by !assing object as !arameters then it is called as call by re'erence.+n the case o' call by value 2 hen the method modi'y the !arameter value then those modi'icationill not a''ected caller o' methods here as in case o' call by re'erence modi'ication ha!!ens in themethod ill be a''ected to caller o' the methods.Constr&ctor:onstructor is a s!ecial method hose name is same as class name. :onstructors don?t return any value even void also.:onstructor ill be invo/ed by J)* hen object is created .constructor ill be used to initialiDe instance variables o' the class. :onstructor shouldn?t be called by the develo!er2 they should be call by J)* only hen you are creating object 'or class. _se o' constructor is to initialiDe the object "instance variable#. :onstructor can be !arameteriDed i.e. e can rite multi!le constructors in a same class in the same name by changing the !arameter. Ahen you creating an object then the corres!onding constructor ill be invo/ed based on the !arameters only. Ahen you don?t have any constructors in the class 2J)* inserts one de'ault constructor as 'ollos Qello A6 UV Ahen you have any other constructors in the class J)* don?t insert any de'ault constructor. Ae should not s!eci'y any return ty!e 'or the constructor including void also . Ahen you s!eci'y any return ty!e that ill be considered as normal method. 8e'ault constructor means the constructor hich has Dero argument. Method overloadin'+n a class you can rite more than one method ith the same name by changing the !arameters as'ollos3 --------- i.e. No of parameters T%pes of parameters Order of parametersMethod invoin' depends on parameter-Ahen you overriding methods2 return ty!e is not a matter i.e. you can use any return ty!e hich ill not be used in method invocation.Constr&ctor overloadin': -----riting multi!le constructors in a class by changing the !arameter is called constructor overloading.Get&rn t%pe: ------------no need-This e%4ord: -------------- Uthis? is re'erence variables hich contain the object o' current class.-ometimes you can ta/e the same name 'or local and instance variable. 6hen inside the method localvariables hides the instance variables. 6o di''erentiate local , instance variable2 e use this to re'erthe instance variables.Uthis? is the instance re'erence variable hich is not alloed 'rom staticbloc/ and static methods. 2hen this reference varia(le is created (% JVM3 ---------------Class loadin' time-2hat is class 3:lass is a user de'ined data ty!e hich contains logical re!resentation o' o!eration and !ro!erties. 2hat is an o(ject 3Object is a !hysical e$istence o' !ro!erties "data#.Class loadin' (% class loaders: ---------:lass loaders are !art o' J)* hose res!onsibility is loading the classes into main memory hen you are trying to use a class then class ill be loaded into memory.The follo4in' fo&r scenarios 4here 4e can &se the class: --------------- Ahen you are running the java class 'rom class !rom!ts. 4$!3- java test97 %% thentest9 ill be loaded. Ahen you are trying to create object 'or the class then that class ill be loaded. 4$!3---------(ai raj; ne (ai"#7 Ahen you are invo/ing a static method ith the re'erence variable hich contains null then the class o' that re'erences variable ill be loaded.4$!3-------- (ello.h;null7h.m"#7A.m1"#7 Ahen you are invo/ing a static method in the class name then the class ill be loading 'irst. 4$!3---------A.m1"#7In java 4e can onl% assi'n n&ll address to other reference varia(le 9 other are not allo4ed-NOT7: -------- hen you are invo/ing a static method ith a re'erence variable hich is !ointingsome !roject then class ill not be loaded into the memory at that !oint because class is alreadyloaded be'ore to this hen you are creating the object.(ello h;ne (ello"#7 %% loads the classh.m1"#7%%ouldn?t load the class Ahen you are invo/ing a non-static method ith a re'erence variable hich contains null then null !ointer e$ce!tion ill ha!!en at run time. Ahen you are invo/ing a static method ith the re'erence variable hich contains null then method ill be invo/ed success'ully ithout error. O(ject creation time: ------------ :lass ill be loaded. *emory ill be allocated 'or the re'erence variable. *emory ill be allocated 'or instance variable. +nstance bloc/ ill be e$ecuted. :onstructor ill be invo/ed. Assign address to object into re'erence variable-Class loadin' time: ------------- :reate the de'ault object :reating the stac/. -tatic variable memory allocation -tatic bloc/ e$ecution Access modifiers6here are 'our access modi'iers in java3 ------------------ Private A#efa&lt6 Protected P&(lic Access modi'ier ill be used to s!eci'y the sco!e o' class2 methods and variables. .rivate member are allos in the class here they are de'ined. .rivate member is not alloed in other classes hich are available in same !ac/age. 8e'ault members2 !rotected members and !ublic members are alloed ithin the class and also in other classes hich are available in the same !ac/age. Ahen you are not s!eci'ying any !ac/age then classes ill be stored in de'ault !ac/age. 6o! level classes can?t be !rivate and !rotected and can be de'ault and !ublic. All the class level variables can be !rivate2 !rotected2 !ublic2 de'ault"static variable and instance variable both# Pocal variable cannot be any thing such as !ublic2 !rivate or de'ault. *ethod ithin the class can be !rivate2 !ublic2 !rotected or de'ault. :onstructors can be !rivate2 !ublic2 !rotected and de'ault. Mloc/s cannot be !rivate2 !ublic2 !rotected2 'inal2 de'ault. Ahen you declare a to! level class as !ublic2 you must save that in a se!arate source 'ile iththe same 'ile name.78p: ---- 4hen %o& declare Qello class as p&(lic %o& m&st save that so&rce file 4ith Qello- java Qo4 man% p&(lic classes are allo4ed in one so&rce file3---------------------Onl% one Ahy to! level classes can?t be !rivate and !ublicL (o many class 'ile can + rite inside a source 'ileL*ulti!le classes e can rite inside a source 'ile. .ublic class only once alloed and de'ault classes are alloed many times.

INQ7GITANC7+t is a !rocess that e$tending the e$isting class 'unctionality ith the ne class. Ahen ne classe$tending ith e$isting class then all the members o' e$isting class ill be used in the ne classdirectly.Ahen e e$tends one class 'or other class then all su!er class members becomes member o' subclass. Aith the su!er class object e can access only su!er class members. Aith sub class object e can access both su!er and subclass members.T%pes of inheritance: -------------------- SimpleInheritance M&ltilevelInheritance Qierarchical Inheritance M&ltiple Inheritance Q%(rid InheritanceSimple Inheritance: --- in sim!le inheritance there ill be e$actly one su!er class and one subclassi.e. subclass can get the 'unctionality 'rom e$actly one su!er class-M&ltilevel Inheritance: ------- +n *ultilevel +nheritance one su!er class can have many subclasses2in that one subclass is directly and all remaining are indirect.Pi/e that one subclass can have many su!er classes , one is direct su!er class and all remaining areindirect su!er class.Qierarchical Inheritance: -- +n this one su!er class have many direct subclasses.M&ltiple Inheritances: ----+n this one subclass can have multi!le direct su!er classes.*ulti!le +nheritances is not alloed in java2 using classes "due to various region6 Q%(rid Inheritance: ----- (ybrid +nheritance is a combination o' multi!le inheritance2 multilevelinheritance and hierarchical inheritance. 2hen %o& 4rite constr&ctor inside the s&(class then JVM inserts one defa&lt s&per as a first statement- This defa&lt s&per invoes the immediate s&per class defa&lt constr&ctor- If %o& 4ant to invoe s&per class ar'&ment constr&ctors then %o& need to 4rite s&per 4ith parameters- S&perA6---------invoes s&per class defa&lt constr&ctor -S&perAa9(6----- invoes s&per class + ar'&ment constr&ctor S&per class (e the ,st statement inside the constr&ctor- Onl% one s&per class statement is allo4ed inside the constr&ctor- 2hen %o& 4rite the s&per class statement inside the constr&ctor then JVM 4o&ldnRt insert the defa&lt s&per-S&per is also a reference varia(le lie WthisX- YThisR reference varia(le points o(ject of c&rrent class 4hereas Ys&perR reference varia(le points o(ject of immediate s&per class- 2ith YthisR reference varia(le 4e can refer c&rrent class varia(le and methods 4here as 4ith Ys&perR reference varia(le9 4e can refer immediate s&per class varia(le and methods 5sin' this A6 %o& can invoe c&rrent class constr&ctor- >&t &sin' s&per A6 %o& can invoeimmediate constr&ctor- Can I have same name for s&per class varia(le9 s&(class varia(le and local varia(le3Ses 9%o& can have 9 (&t s&(class mem(er hides the s&per class mem(ers f&rther %o& have to differentiatethe s&(class and s&per class mem(ers refer the s&(class mem(er 4ith YthisR and s&per class mem(er 4ith Ys&perR-M7TQO# OV7GGI#INZMethod overridin': -------+m!lementing the su!er class method in subclass ith the same nameand ith the same signature. 6his is called as method overriding.G&les for method overridin': -------- -ubclass method name must be same as su!er class method name. -ubclass !arameters must be same as su!er class !arameter. -ubclass method return ty!e must be same as su!er class method return ty!e. -ubclassmethod accesss!eci'iermust be same assu!erclassmethod accesss!eci'ier orhigher than su!er class method s!eci'ierS&per class Private#efa&ltProtectedP&(lic S&(class Private9 defa&lt 9protected 9p&(lic#efa&lt 9protected 9p&(licProtected 9p&(licp&(lic2hen s&per class method is thro4in' an e8ception in s&(class9 then %o& do one of follo4in': -- Oou can omit the method level e$ce!tion in subclass Oou can use same method level e$ce!tion in subclass -ub class methods can thro subclasses to su!er class e$ce!tion. -ubclass method can?t thro su!er class to su!er class e$ce!tion class. Ass&me that s&per class has a method of follo4s: ---------)oid m1"int $#thros MSo& can override this method 4ith one of the follo4in' form: ----)oid m1"int $#]!rotected void m1"int a#thros M^)oid m1"int $#]!ublic void m1"int a#thros M^#%namic dispatch Assigning subclass object to su!er class re'erence variable dynamically at run time is called as dynamic dis!atch. Ae can not assign su!er class object to sub class re'erence variable.2hen A is s&pper class and > is s&(class 4hich of the follo4in' statementare --------- T!"A o(j F ne4 AA6* ----------------T> o(j F ne4 >A6*----------------TA o(j F ne4 >A6*----------------T> o(j F ne4 AA6*----------------"Can I override private method3-------------------------- No If I 4ant to achieve r&n time pol%morphism 4hat are re:&ired3 #%namic dispatch and method overridin' Pol%morphismOne 'orm behaving di''erently in di''erent situation is called !olymor!hism.+n java e have to ty!es o' !olymor!hism3 --- Static!compile time #%namic!r&n time Oou can im!lement com!ile time by using method overloading Oou can im!lement run time by using method overriding and dynamic dis!atch Ahen you have static and !rivate method in su!er class2 e can?t achieve the run time !olymor!hism. Ahen su!er class method is static e have to override that method as static only in the subclass.2hat is the &se of final e%4ord3 Ae can use 'inal /eyord 'or variables2 methods and classes. Final variable can?t be modi'ied. Final variable can?t be overridden. Final variable can?t be inherited.A(stract classAhen you riting the classes2 sometime you are unable to im!lement some o' the methods2 i.e. you are unable to !rovide the 5uery 'or that because im!lementation details are unclear. Ahen you not !roviding 5uery 'or the methods2 you must declare method as abstract.. Ahen a class has one and more abstract method then you should declare the class has abstract. Ahen a class is abstract then it can?t be initiated 2 but you can declare the re'erence variable. Ahen one class is e$tending another class2 then subclass has to override all the abstract method o' su!er class2 otherise subclass must be declared as abstract. Abstract class can contain both abstract method and concrete method. Abstract class can also contain only abstract method. Abstract class can also contain only concrete methods Abstract class can?t be initiated i.e. e can?t ma/e the object o' abstract class. Ahen you are not !roviding body 'or the method then e should declare that method as abstract. Ahen a class is abstract2 you can?t create object 'or that class. Abstract class must have subclass. Can I declare a class as a(stract 4itho&t a(stract method3Ses9 4e can have a(stract class 4itho&t a(stract method-Can I declare instance varia(le and static varia(les inside the a(stract class3Ses9 %o& can declare-Can I 4rite constr&ctor inside a(stract class3SesCan I have static and non-static (loc inside the a(stract class3SesCan I override static methods3NoCan I override private methods3 No9 private method canRt override-IfI 4ant to stop method overridin' 4hat I have to do3#eclare the method as final-Qo4 can I stop the inheritance3>% declare the class as final- Alays you have to call instance method ith object only. Mut you can call static method ith class name or ith re'erence variables hich contains null or ith an object hy it isL +nstance method may use instance variable and concrete object only. -o ithout creating object you cannot access instance method. -tatic methods ouldn?t use any instance variables so2 object is not re5uire to invo/e static methods.2h% m&ltiple inheritances are allo4s 4ith java interfaces and 4h% it is not allo4ed 4ith java classes3 #ifference (et4een a(stract class and interface: ---------A(stract class Abstract class can have instance variable2static variable2 constructor2 abstract methods2 concrete methods2 instance bloc/s and static bloc/s. -ubclass has to e$tend abstract class using e$tend /eyord. Ae can declare the variable o' abstract class as !rivate2 de'ault2 !ublic2 !rotected. Ae can declare the abstract class variableas 'inal and non-'inal variable Ae can declare the abstract class variableas static and non-static Ae can declare the abstract class methodsas !rotected2 de'ault2 !ublic. Abstract class doesn?t allo multi!le inheritances. Interface +nter'ace can have only static variable and abstract methods. -ubclass has to im!lement inter'ace using im!lement /eyord. Alays inter'ace variable are !ublic Alays inter'ace variable are 'inal. Alays inter'ace variable are static. Alays inter'ace methods are !ublic +nter'aceallo multi!le inheritance-Similarit% (et4een a(stract class and interface: ---------- Moth abstract class and inter'ace cannot be initiated. Ae can declare the re'erence variable 'or both abstract class inter'ace class. Ahen you e$tend abstract class or hen you im!lement inter'ace then you have to override all the abstract method in subclass2 otherise you have to declare subclass as abstract.

2hen I 4ant to &se a(stract class and interface3 -election o' abstract class and inter'ace de!ends on your re5uirement. 4$!3 -- you can rite abstractmethod and concrete method then you have to select abstract class. Ahen you ant to rite only abstract methods then you can select abstract class and inter'ace. +' you ant to achieve multi!le inheritances2 then you have to select inter'ace only.2hich of the follo4in' e%4ord are allo4ed for interface3 .rivate------------------- S .rotected ----------------- S 8e'ault ------------------- O .ublic ---------------------- O -tatic ----------------------- S Final ------------------------ S Abstract -------------------- SFor concrete class3 ----------- de'ault2 !ublic2 'inal2 abstract.For abstract class3 ----------- de'ault2 !ublic Class level var-Methodlevel var-constr&ctor Class level (locMethodlevel (locmethods class InterfacePrivateTP T PPTPPA#efa&lt6TP T PPTTTProtectedTP T PPTPPP&(lic TP T PPTTTStatic TP P TPTPP"inal T T P PPTTPA(stractPP P PPTTPPrivate9 p&(lic9 protected9 static9 final9 a(stract -------------Ahich o' the above /eyord is alloed 'or instance method3 ---------!rivate2 !rotected2 !ublic2 'inal2 abstract Ahich o' the above /eyord is alloed 'or a static method3 ------ e$ce!t abstract all are alloed Ahich o' the above /eyord is alloed 'or abstract methods3 --------- abstract2 static Interface+nter'ace is a s!ecial JA)A class hich is 'ully abstracted2 hich contains only to ty!e o' members3--- Final static variable .ublic abstract variable S%nta8: -- interface interface/name U #ata t%pe var,9var+9-----* "inal static data t%pe var;* P&(lic a(stract ret&rn t%pe method /nameAparameter6* Get&rn t%pe method/nameAparameter6*V78p:--- Interface i, Uint aF,0*int (F,+*Void m,A6*V+nter'ace is a com!lete abstract class hich contains !ublic2 'inal2 static variable and !ublic abstract methods.+nter'ace cannot be instantiated but e can declare the re'erence variable hen a class is im!lementing inter'ace then that class must override all the abstract methods o' inter'ace otherise class must be abstract.Ahen a class is im!lementing inter'ace then all 'inal static variable o' inter'ace ill be inherited to subclass.All the variables declared inside the inter'ace are 'inal and static."e can also access the static variable ith class name#All the methods declared inside the inter'ace are !ublic and abstract.+nter'ace can?t be initiated i.e.:annot create an object. Mut e can create re'erence variable. +nter'ace must be a subclass.Can I declare interface as static3&oCan I declare interface as final3NoCan I declare top level interface as private3NoCan I declare a(stract method as static3NoCan I declare a(stract method as final3NoCan I declare a(stract class as final3NoCan I 4rite constr&ctor inside the interface3NoCan I 4rite static and non-static (loc inside the interface3NoCan I 4rite concrete method inside the interface3 NoQo4 can I achieve m&ltiple inheritances in java3My inter'ace only 2h% m&ltiple inheritances are allo4ed thro&'h interface onl%3 2h% not thro&'h class3 Gefer dia'ram- Can I 4rite constr&ctor inside the interface3:onstructor is not alloed inside inter'aceCan I 4rite static (loc inside the interface3NoCan I 4rite instance (loc inside the interface3NoCan I 4rite instance varia(le9 concrete methods inside the interface3Ses Can I 4rite static method inside the interface3NoPaca'e.ac/age is a collection o' .class 'ile..ac/age is a 'older or set o' 'olders in the three 'ormats3 --------.ac/age ill have vie to organiDe the 'iles o' your !roject in a better ay..ac/age ill have vie to solve the naming com!le$.Ahen you are riting java classes and com!ile2 by de'ault all the classes ill be !laced in de'ault !ac/age "!ac/age ithout name#.+' you ant to !lace your classes in some !ac/age ith some name2 e have to use the !ac/age statement in the source 'ile.A86 A86-------I A%6 A86----------IA%6------ -IA=6Paca'e 8*paca'e 8-%*paca'e 8-%-=*#eclaration of paca'e:-------S%nta8: ----------!ac/age !ac/ageBname7 .ac/age $7 .ac/age $.y7 .ac/age $.y.D7+n the above e$am!le $ is a 'older inside that one more 'older y. inside y another 'older called D .herese!arate the 'older name ith dot A-6-.ac/age declaration statement must be the 1st statement o' the java source 'ile. 6o !ac/age declaration statements is not alloed inside one source 'ile. .rivate member are alloed ithin the class and not alloed outside the class. .ublic member are alloed any here. 8e'ault member are alloed ithin the class2 subclass , nonBsubclass hich are available insame !ac/age. 8e'ault member are not alloed out side the !ac/age. .rotected member are alloed ithin the class2 subclass , non subclass available in same !ac/age and subclass available in di''erent !ac/age. .rotected members are not alloed in non subclass hich is available in di''erent !ac/age. Class78tendsClass ClassImplementInterface Interface78tendsInterface InterfaceNo operationClass Chec T!": ---------------Java is !ure OO.P3 ------------------------------------- FJava su!!orts !ointer3 ---------------------------------- FJava su!!ort o!erator overloading3 ------------------ F:onst and goto /eyord is alloed in java3 --------- F*ethod overloading is not alloed in java3 ---------- F*ulti!le inheritances is alloed in java3 -------------- 6*ethod inside method is not alloed3 --------------- 6:onstructor can not be inherited3 ----------------------6.rivate method can?t be inherited3 -------------------- 6.rivate method can not be overridden3 ----------------6-tatic method can not be overridden3 -------------------6-tatic method can not be inherited 3 --------------------F.rotected methods can?t be inherited3 --------------------F.rotected methods can?t be overridden3 ----------------- FMloc/ can?t be inherited3 ------------------------------------ 6