java intrvw cncpts details extra.docx

Upload: ashish-gulshan

Post on 26-Feb-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 java intrvw cncpts details extra.docx

    1/23

    next prev

    Variable and Datatype in Java

    1. Variable

    2. Types of Variable

    3. Data Types in Java

    In this page, we will learn about the variable and java data types. Variable is a name of

    memory location. There are three types of variables local, instance and static. There are

    two types of datatypes in java, primitive and non!primitive.

    Variable

    Variable is name of reserved area allocated in memory.

    1. intdata"#$%&&'ere data is variable

    Types of Variable

    There are three types of variables in java

    local variable

    http://www.javatpoint.com/unicode-system-in-javahttp://www.javatpoint.com/internal-details-of-jvmhttp://www.javatpoint.com/variable-datatype#variablehttp://www.javatpoint.com/variable-datatype#typesofvariablehttp://www.javatpoint.com/variable-datatype#datatypehttp://www.javatpoint.com/internal-details-of-jvmhttp://www.javatpoint.com/variable-datatype#variablehttp://www.javatpoint.com/variable-datatype#typesofvariablehttp://www.javatpoint.com/variable-datatype#datatypehttp://www.javatpoint.com/unicode-system-in-java
  • 7/25/2019 java intrvw cncpts details extra.docx

    2/23

    instance variable

    static variable

    Local Variable

    ( variable that is declared inside the method is called local variable.

    Instance Variable

    ( variable that is declared inside the class but outside the method is called instance

    variable . It is not declared as static.

    Static variable

    ( variable that is declared as static is called static variable. It cannot be local.

    )e will have detailed learning of these variables in ne*t chapters.

    Example to understand the types of variables

    1. class(+

    2. intdata"#$%&&instance variable

    3. staticintm"1$$%&&static variable. voidmethod-+

    #. intn"/$%&&local variable

    0. . &&end of class

    Data Types in Java

  • 7/25/2019 java intrvw cncpts details extra.docx

    3/23

    In java, there are two types of data types

    primitive data types

    non!primitive data types

    Data Type Default Value Default s

    boolean false 1 bit

    char 4u$$$$ 2 byte

    byte $ 1 byte

    short $ 2 byte

    int $ byte

    long $5 6 byte

    float $.$f byte

  • 7/25/2019 java intrvw cncpts details extra.docx

    4/23

    double $.$d 6 byte

    Why char uses 2 byte in java and what is \u0000 ?

    because java uses unicode system rather than (78II code system. 4u$$$$ is the lowest

    range of unicode system.To get detail about 9nicode see below.

    JVM (Java Virtual Machine)

    1.Java Virtual Machine

    2.Internal Architecture of JVM

    :V; -:ava Virtual ;achine is an abstract machine. It is a specification that provides runtime

    environment in which java bytecode can be e*ecuted.

    :V;s are available for many hardware and software platforms -i.e.:V; is plateform

    dependent.

    What is JVM?

    It is

    1. A specificationwhere woruntime

    ?nvironment.

    3. Runtime Instance)henever you write java command on the command prompt to

    run the java class, and instance of :V; is created.

    What it does?

    The :V; performs following operation

    5oads code

    http://www.javatpoint.com/internal-details-of-jvmhttp://www.javatpoint.com/internal-details-of-jvm#jvminternalarchhttp://www.javatpoint.com/internal-details-of-jvmhttp://www.javatpoint.com/internal-details-of-jvm#jvminternalarch
  • 7/25/2019 java intrvw cncpts details extra.docx

    5/23

    Verifies code

    ?*ecutes code

    @rovides runtime environment

    :V; provides definitions for the

    ;emory area

    8lass file format

    >egister set

    Aarbage!collected heap

    Batal error reporting etc.

    Internal Architecture of JVM

    5ets understand the internal architecture of :V;. It contains classloader, memory area,

    e*ecution engine etc.

  • 7/25/2019 java intrvw cncpts details extra.docx

    6/23

    1) Classloader:

    8lassloader is a subsystem of :V; that is used to load class files.

    2) Class(Method) Area:

    8lass-;ethod (rea stores per!class structures such as the runtime constant pool, field and

    method data, the code for methods.

    3) Heap:

    It is the runtime data area in which objects are allocated.

  • 7/25/2019 java intrvw cncpts details extra.docx

    7/23

    4) Stack:

    :ava 7tac< stores frames.It holds local variables and partial results, and plays a part in

    method invocation and return.

    ?ach thread has a private :V; stac

  • 7/25/2019 java intrvw cncpts details extra.docx

    8/23

    =efore 9nicode, there were many language standards

    ASCII-(merican 7tandard 8ode for Information Interchange for the 9nited

    7tates.

    IS !!"#-1for )estern ?uropean 5anguage.

    $I-!for >ussian.

    %&1!'3' and &I%-"for chinese, and so on.

    Tis caused to pro*lems:

    1. ( particular code value corresponds to different letters in the various languagestandards.

    2. The encodings for languages with large character sets have variable length.7ome

    common characters are encoded as single bytes, other reDuire two or more byte.

    To solve these problems, a new language standard was developed i.e. 9nicode 7ystem.

    In unicode, character holds 2 byte, so java also uses 2 byte for characters.

    loest value:4u$$$$

    i+est value:4uBBBB

    Constructor in ,avais a special type of methodthat is used to initialiEe the object.

    :ava constructor is invoked at the time of object creation. It constructs the values i.e.

    provides data for the object that is why it is

  • 7/25/2019 java intrvw cncpts details extra.docx

    9/23

    There are two types of constructors

    1. Fefault constructor -no!arg constructor

    2. @arameteriEed constructor

    Java Default Constructor( constructor that have no parameter is

  • 7/25/2019 java intrvw cncpts details extra.docx

    10/23

    Q) What is the purpose of default constructor?

    Fefault constructor provides the default values to the object li

  • 7/25/2019 java intrvw cncpts details extra.docx

    11/23

    Java parameterized constructor

    ( constructor that have parameters is

  • 7/25/2019 java intrvw cncpts details extra.docx

    12/23

    Example of Constructor Overloading

    1. class7tudent#+2. intid%

    3. 7tring name%

    . intage%#. 7tudent#-inti,7tring n+0. id " i%

    . name " n%6.

    /. 7tudent#-inti,7tring n,inta+1$. id " i%

    11. name " n%12. age"a%

    13. 1. voiddisplay-+7ystem.out.println-idNJ JNnameNJ JNage%

    1#.

    10. pu*licstaticvoidmain-7tring argsKL+

    1. 7tudent# s1 " ne7tudent#-111,JOaranJ%16. 7tudent# s2 " ne7tudent#-222,J(ryanJ,2#%

    1/. s1.display-%2$. s2.display-%

    21. 22.

    Test it o

    Mutput

    111 Karan 0

    222 Aryan 25

    Difference between constructor and method in javaThere are many differences between constructors and methods. They are given below.

    Java Constructor Java Method

    8onstructor is used to initialiEe the state of an object. ;ethod is used to

    object.

    8onstructor must not have return type. ;ethod must hav

    8onstructor is invo

  • 7/25/2019 java intrvw cncpts details extra.docx

    13/23

    The java compiler provides a default constructor if you dont have any

    constructor.

    ;ethod is not pro

    case.

    8onstructor name must be same as the class name. ;ethod name ma

    class name.

    Java Copy ConstructorThere is no copy constructor in java. =ut, we can copy the values of one object to

    another li

  • 7/25/2019 java intrvw cncpts details extra.docx

    14/23

    Mutput

    111 Karan

    111 Karan

    Copying values without constructor)e can copy the values of one object into another by assigning the objects values to

    another object. In this case, there is no need to create the constructor.

    1. class7tudent+2. intid%

    3. 7tring name%. 7tudent-inti,7tring n+

    #. id " i%0. name " n%

    . 6. 7tudent-+

    /. voiddisplay-+7ystem.out.println-idNJ JNname%1$.

    11. pu*licstaticvoidmain-7tring argsKL+12. 7tudent s1 " ne7tudent-111,JOaranJ%

    13. 7tudent s2 " ne7tudent-%1. s2.id"s1.id%

    1#. s2.name"s1.name%10. s1.display-%

    1. s2.display-%16.

    1/. Test it o

    Mutput

    111 Karan

    111 Karan

    Q) Does constructor return any value?

    Ans:yes, that is current class instance -Pou cannot use return type yet it returns a value.

    Can constructor perform other tasks instead of initialization?

    http://www.javatpoint.com/opr/test.jsp?filename=Student7http://www.javatpoint.com/opr/test.jsp?filename=Student7
  • 7/25/2019 java intrvw cncpts details extra.docx

    15/23

    Pes, li

  • 7/25/2019 java intrvw cncpts details extra.docx

    16/23

    3. 7tring name%. 7tring college"JIT7J%

    #.

    7uppose there are #$$ students in my college, now all instance data members will get

    memory each time when object is created.(ll student have its uniDue rollno and name so

    instance data member is good.'ere, college refers to the common property of all objects.If

    we ma

  • 7/25/2019 java intrvw cncpts details extra.docx

    17/23

    Program of counter without static variableIn this e*ample, we have created an instance variable named count which is incremented in

    the constructor. 7ince instance variable gets the memory at the time of object creation,

    each object will have the copy of the instance variable, if it is incremented, it wont reflect to

    other objects. 7o each objects will have the value 1 in the count variable.

    1. class8ounter+

    2. intcount"$%&&will get memory when instance is created3.

    . 8ounter-+

    #. countNN%0. 7ystem.out.println-count%

    . 6.

    /. pu*licstaticvoidmain-7tring argsKL+

    1$.11. 8ounter c1"ne8ounter-%

    12. 8ounter c2"ne8ounter-%13. 8ounter c3"ne8ounter-%

  • 7/25/2019 java intrvw cncpts details extra.docx

    18/23

    1.1#.

    10. Test it o

    Output:1

    1

    1

    Program of counter by static variable

    (s we have mentioned above, static variable will get the memory only once, if any object

    changes the value of the static variable, it will retain its value.

    1. class8ounter2+

    2. staticintcount"$%&&will get memory only once and retain its value3.. 8ounter2-+

    #. countNN%0. 7ystem.out.println-count%

    . 6.

    /. pu*licstaticvoidmain-7tring argsKL+1$.

    11. 8ounter2 c1"ne8ounter2-%12. 8ounter2 c2"ne8ounter2-%

    13. 8ounter2 c3"ne8ounter2-%1.

    1#.

    10. Test it o

    Output:1

    2

    3

    2) Java static methodIf you apply static

  • 7/25/2019 java intrvw cncpts details extra.docx

    19/23

    Example of static method

    1. &&@rogram of changing the common property of all objects-static field.

    2.3. class7tudent/+

    . introllno%#. 7tring name%

    0. static7tring college " JIT7J%.

    6. staticvoidchange-+/. college " J==FITJ%

    1$. 11.

    12. 7tudent/-intr, 7tring n+13. rollno " r%

    1. name " n%

    1#. 10.

    1. voiddisplay -+7ystem.out.println-rollnoNJ JNnameNJ JNcollege%16.

    1/. pu*licstaticvoidmain-7tring argsKL+

    2$. 7tudent/.change-%21.

    22. 7tudent/ s1 " ne7tudent/ -111,JOaranJ%23. 7tudent/ s2 " ne7tudent/ -222,J(ryanJ%

    2. 7tudent/ s3 " ne7tudent/ -333,J7onooJ%2#.

    20. s1.display-%2. s2.display-%

    26. s3.display-%2/.

    3$. Test it o

    Output:111 Karan BBDIT

    222 Aryan BBDIT

    333 Sonoo BBDIT

    Another example of static method that performs normal

    calculation

    1. &&@rogram to get cube of a given number by static method

    2.3. class8alculate+

    . staticintcube-int*+#. return*Q*Q*%

    0. .

    http://www.javatpoint.com/opr/test.jsp?filename=Student9http://www.javatpoint.com/opr/test.jsp?filename=Student9
  • 7/25/2019 java intrvw cncpts details extra.docx

    20/23

    6. pu*licstaticvoidmain-7tring argsKL+/. intresult"8alculate.cube-#%

    1$. 7ystem.out.println-result%11.

    12. Test it o

    Output:125

    Restrictions for static method

    There are two main restrictions for the static method. They are

    1. The static method can not use non static data member or call non!static method

    directly.

    2. this and super cannot be used in static conte*t.

    1. class(+2. inta"$%&&non static3.

    . pu*licstaticvoidmain-7tring argsKL+

    #. 7ystem.out.println-a%0.

    . Test it o

    Output:Copile Tie !rror

    Q) why java main method is static?

    (ns because object is not reDuired to call static method if it were non!static method,

    jvm create object first then call main- method that will lead the problem of e*tra

    memory allocation.

    3) Java static block

    o Is used to initialiEe the static data member.

    o It is e*ecuted before main method at the time of classloading.

    Example of static block

    1. class(2+2. static+7ystem.out.println-Jstatic bloc< is invo

  • 7/25/2019 java intrvw cncpts details extra.docx

    21/23

    3. pu*licstaticvoidmain-7tring argsKL+. 7ystem.out.println-J'ello mainJ%

    #. 0.

    Test it o

    Output:static "lock is in#oked

    $ello ain

    Q) Can we execute a program without main() method?

    (ns Pes, one of the way is static bloc< but in previous version of :FO not in :FO 1..

    1. class(3+

    2. static+3. 7ystem.out.println-Jstatic bloc< is invo

  • 7/25/2019 java intrvw cncpts details extra.docx

    22/23

    2 (rray5ist increments "'of current

    array siEe if number of element e*ceeds

    from its capacity.

    Vector increments 1''means doubles the

    of element e*ceeds than its capacity.

    3 (rray5ist is not a le+acclass, it is

    introduced in :FO 1.2.

    Vector is a le+acclass.

    (rray5ist is fastbecause it is non!

    synchroniEed.

    Vector is slobecause it is synchroniEed i.e. in

    environment, it will hold the other threads in ru

    state until current thread releases the loc< of o

    # (rray5ist uses Iteratorinterface to

    traverse the elements.

    Vector uses .numerationinterface to traverse

    use Iterator also.

    Example of Java ArrayList

    5ets see a simple e*ample where we are using (rray5ist to store and traverse the elements.

    1. importjava.util.Q%2. classTest(rray5ist21+

    3. pu*licstaticvoidmain-7tring argsKL+

    .#. 5istG7tring al"ne(rray5istG7tring-%&&creating arraylist

    0. al.add-J7onooJ%&&adding object in arraylist. al.add-J;ichaelJ%6. al.add-J:amesJ%

    /. al.add-J(ndyJ%1$. &&traversing elements using Iterator

    11. Iterator itr"al.iterator-%12. ile-itr.hasRe*t-+

    13. 7ystem.out.println-itr.ne*t-%1.

    1#. 10.

    Test it o

    Mutput

    Sonoo

    *ic+ael

    'aes

    Andy

    http://www.javatpoint.com/opr/test.jsp?filename=TestArrayList21http://www.javatpoint.com/opr/test.jsp?filename=TestArrayList21
  • 7/25/2019 java intrvw cncpts details extra.docx

    23/23

    Example of Java Vector

    5ets see a simple e*ample of java Vector class that uses ?numeration interface.

    1. importjava.util.Q%

    2. classTestVector1+3. pu*licstaticvoidmain-7tring argsKL+

    . VectorG7tring v"neVectorG7tring-%&&creating vector#. v.add-JumeshJ%&&method of 8ollection

    0. v.add?lement-JirfanJ%&&method of Vector. v.add?lement-J