Lakuma Yalamanchili

Greenhorn
+ Follow
since Sep 24, 2008
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Lakuma Yalamanchili

First when we call the no arg constructor:

t -> test Obj with a tester instance pointing to null : Call this Obj_T1


Second when you call the one-arg constructor passing the reference of Obj_T1, we have:

t -> test Obj with a tester instance pointint to Obj_T1, call this Obj_T2


Now, the only live reference that we have is to Obj_T2, which is made null by saying t1=null. Thus, according to:
  • Objects must be considered eligible before they can be garbage collected.
  • An object is eligible when no live thread can reach it.
  • To reach an object, you must have a live, reachable reference to that object


  • There is no reachable reference to either of these objects, thus both the objects are eligible!

    This is my inference or my way of answering this question, please correct me if I happen to be wrong!

    Regards,
    Lakuma
    Congratulations...!!! Thanks for the post..it would boost some of us here!
    13 years ago
    Thanks Denis!

    So I was right when I thought the following is wrong??

    <? super Number> we're not saying super classes of Number(as defined for the class), we're saying any & all subclasses of Number



    Regards,
    Lakuma
    [ October 03, 2008: Message edited by: Lakuma Yalamanchili ]
    Venkata,

    Yes there is a section in Chapter 7 - K&B about using super with Generics.

    Ashish,

    Thanks for the link, it seems to be good to go. So i'll just confirm what I get from this, please correct me if I've misunderstood it!!

    Note that by saying <? super Number> we're not saying super classes of Number(as defined for the class), we're saying any & all subclasses of Number. Think of it as simply List.



    So <? super Number> indicates any class that has Number as it's super class or Number itself!

    <? extends Number> indicates any class that extends Number or Number itself!

    What is the difference between the two statements then?? Any class that has Number as it's super class is the same as any class that extends Number, is it not?

    If they are the same then why is it that we have two different ways of showing the same thing? Is there a subtle difference to this?

    And yes - "Nothing lower in the inheritance tree can come in, but anything higher than Number is OK" is an incorrect statement.



    Maybe there is a different meaning to this statement - not the way we are comprehending it - something more than that?

    If what is said in the first statement is true:

    <? super Number> we're not saying super classes of Number(as defined for the class), we're saying any & all subclasses of Number



    Then in the following code, class Animals should not be accepted, but it happens to work, where Animals is a super class of Dog!

    Code source - K&B : Chapter 7: Generic Methods Section


    Thanks again!!

    Regards,
    Lakuma
    [ October 03, 2008: Message edited by: Lakuma Yalamanchili ]
    Tusshar,

    Here's an example:
  • If there is a package statement, that is the first line of your source code.


  • If there is an import statement, then it should follow your package statement and all the imports should start before a class definition.



  • If there is an import statement and no package statement, then the import statement would be your first statement of the source code.



  • If there is no package statement and there are no import statements, then obviously the class declaration would be the first line of your source code.




  • Hope this makes it clear!

    Regards,
    Lakuma
    [ October 03, 2008: Message edited by: Lakuma Yalamanchili ]
    Deepak,

    If you ask at what point object reference by b1 would be eligible for GC?
    -- It won't be eligible



    The above means that, when you say b1 = b2, now b1 refers to the same object that b2 refers and this object is not eligible for GC as there are references to this object.

    But if you ask at line b1 = b2 what object would be eligible for GC?
    -- new Base("One")



    The above means that, before the statement b1 = b2, b1 refers to a object whose string was "One" and is later set to "". This object becomes eligible for GC - since b1 no longer refers to it after the statement b1=b2, so you cannot say that the object with the string "" is referred by b1. Thus, the object referred by b1 is never eligible for GC, but the object with the string "" is eligible for GC!

    Hope this makes it clear!

    Regards,
    Lakuma
    [ October 03, 2008: Message edited by: Lakuma Yalamanchili ]
    Hi,

    Let's say we have a return type to a method -> List<E super Number>

    Now, where all can this be stored, like when returned by the method:
    A. <Any subtype of List><Any super type of Number>
    B. <Any subtype of List><Number>

    And with this as the return type to a method -> List<E extends Number>
    A. <Any subtype of List><Any sub type of Number>
    B. <Any subtype of List><Number>

    I wonder if either of those options are right, because Generics says it is rigid with what is in between <> as it has to be exactly the same. So either of the options is wrong?

    Please let me know which of those two in each of the case is right and also if there are any other possibilities of storing the returned value.

    Also, are there real good articles about Generics and polymorphism other than the chapter in K&B. The chapter in K&B on Generics seems real good, but I somehow feel reading more examples would help...any suggestions?

    Thanks in advance,
    Lakuma
    Congratulation Vidhya....!!!
    13 years ago
    I have also found this thread on Sun's java forums. Look into it:

    http://forums.sun.com/thread.jspa?messageID=1734569#1734569
    Hope this gives a bit of clarification!
    Trey,

    'a' here is a primitive and not a Object.

    int a = 5; ---> a is given a value 5
    a = a--; ---> a is assigned a value 5 and then the RHS does nothing...!

    Can you try and guess what the oputput of this would be:



    It would be 11..!! Evaluate RHS and give it to LHS. RHS would be 5+6 and 11 would be assigned to a!
    ? can only be used in reference declarations like as in variables, return types, method arguments and so on...! No where else..that's the simplest way to remember it I guess!!

    I have a doubt with Generics....especially with the return type arguments:

    Let's say we have a return type to a method -> List<E super Number>

    Now, where all can this be stored, like when returned by the method:
    A. <Any subtype of List><Any super type of Number>
    B. <Any subtype of List><Number>

    And with this as the return type to a method -> List<E extends Number>
    A. <Any subtype of List><Any sub type of Number>
    B. <Any subtype of List><Number>

    I wonder if either of those options are right, because Generics says it is rigid with what is in between <> as it has to be exactly the same. So either of the options is wrong?

    Please let me know which of those two in each of the case is right and also if there are any other possibilities of storing the returned value.

    Thanks in advance,
    Lakuma
    [ October 02, 2008: Message edited by: Lakuma Yalamanchili ]
    A is not right as the returned value is a List, and if you notice we are trying to take the returned List into an ArrayList in this option, which is not allowed by the compiler. ArrayList output = <returned List> - This is not allowed by the compiler!!

    B is right as the argument of the method takes a List, this means any subtype value is allowed as well, thus ArrayList can be give to the input and the returned value is a List, which is the type of output!

    Hope this clarifies your answer.

    In the same question that you quoted, I don't understand why C is not right. If we take the types, ArrayList is the input and List is the ouput which is valid and if we look at the type of the generic, the input takes Integer, so E = Integer and the return type has <? super E> which means anything that is a super of Integer, so Number is valid.
    So why is ArrayList<Integer> as the input and List<Number> as the output not the right answer?

    The explanation says the output evaluates to List<Integer>, but I would say the output would evaluate to List<any Super class of Integer and Integer itself> so List<Number> would also be right, wouldn't it?


    [ September 30, 2008: Message edited by: Lakuma Yalamanchili ]
    [ September 30, 2008: Message edited by: Lakuma Yalamanchili ]
    Vidhya,

    You are right about option 2 not being right, you can ask the JVM, but that never guarantees running of the GC! Option 1 is anyways ruled out!

    Option 3 - As finally runs on the object before it is GCed, if that is what option 3 refers to, it has to be true!
    Option 4 - True. As the topic in K&B states that the JVM already runs the GC before it gives you the OutOfMemory exception, which implies that GC is run in low memory conditions!

    So looks like Option 3 and 4 are right, but option 3 kind of puts me into thinking regarding the underlying meaning that it has..!! Someone add to this please and correct me if I'm wrong!

    Thanks,
    Lakuma
    [ September 30, 2008: Message edited by: Lakuma Yalamanchili ]
    Good to know I was able to help!!

    Good luck to you!!
    Vidhya,

    Thanks for your update, I feel better..!!

    Thanks again!

    Saritha,

    The local variable ob1 does refer to obj_ob1, but once the execution call is out of the method "m1", the local variable does not exist as the scope of it lies within the method. But the reference that it created is passed onto c2. So after the method execution:
  • Obj_ob1 exists
  • The reference is given to c2
  • and ob1 is destroyed!


  • Did I make it clear? Do let me know!

    Regards,
    Lakuma
    [ September 30, 2008: Message edited by: Lakuma Yalamanchili ]