Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!

Gary Kevin

Ranch Hand
+ Follow
since Jul 24, 2006
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 Gary Kevin

Originally posted by sweety singh:
i still havent understood the same line...






This is the API of poll method.

when you invoke it like this: pq1.poll(),it first "Retrieves and removes the head of this queue",so "poll() will remove the priority element"

second,it " Returns:
the head of this queue,"

so it returns the element which it just removes

then when you code


JVM prints the element it returns
which part of the program you can't understand?inner class or Generics or methods of class PriorityQueue ?
[ May 23, 2008: Message edited by: Ulf Dittmer ]

Originally posted by Viorel Craescu:
Because you can't access the constructor like this.


Constructors are invoked inside the class using this() or super() (for superclass constructor). When you are trying to invoke it as you did, then the compiler is looking for a method named test which takes one argument. Can't find it and throw an error. So you don't actually invoke the constructor, you actually invoke a method which takes one int argument.
If you replace test(2) with this(2) the code will compile.

Viorel





Viorel:

I know that the JVM will insert this() or super() to the first line of constructor.But under which conditions the JVM  insert this()? or super()?

source:http://enigma.vm.bytemark.co.uk/webstart.html
John Meyers's SCJP 5 mock exam:question 60




A prints 2
B prints 0
C Does not compile
D Runtime Exception
E None of above

The correct answer is C.
This invocation will searh for a function named test. It does not invoke the overloaded test() constructor

why it compiles false at line 9?
source:http://enigma.vm.bytemark.co.uk/webstart.html
John Meyers's SCJP 5 mock exam:question 60



A transient
B default
C private
D Hiding cannot be prevented
E protected
F final
The correct answer is C.
final methods cannot be overriden and private methods are never inherited so they are not overriden either. However option F would cause a compile time error since a final method cannot be overridden.

I thought the answer would be D.This is what I read from the K&B's book(Chap2):
"
Finally,remember that static methods can't be overridden!This doesn't mean they can't be redefined in a subclass,but redefining and overriding aren't the same thing.Let's take a look at an example of a redefined(remember,not overridden),static method:

Running this code produces the output
a a a
"

Originally posted by Wim Molenberghs:
I think line 3 DOESN'T compile because it's not defined inside a method.
Line 1 and 2 are just classvariables.

[ May 10, 2008: Message edited by: Wim Molenberghs ]



Wim:thinks,you got the point,my god,I just concentrate on the Generics problem too much and didn't aware of that.
source:http://enigma.vm.bytemark.co.uk/webstart.html
John Meyers's SCJP 5 mock exam:question 31



What't the difference between line 2 and line 4?How to use for loop to get elements from type?

Originally posted by Henry Wong:


Basically, it is expecting the response to be a String. So, it will use the append() method of the StringBuffer/StringBuilder to do the concat. Yes, there is a version of append() that takes an object, and hence, uses the toString() method -- but the compiler is not calling that version.

The combination of using generics and not using generics is fooling the compiler.

Henry



Henry: I still can't understand,it is expecting the response to be a String,Why will it use append() method of the StringBuffer?It seems to be no
relation with StringBuffer here?
source:http://enigma.vm.bytemark.co.uk/webstart.html
John Meyers's SCJP 5 mock exam:question 22


Answer:Runtime error

Runtime Exception. ClassCastException. It looks like 1 and 1 are added to obtain the result 2 but what happens is that, in main() map and sap are expected to return String values based on Integer keys. So when you pass a key, a String is supposed to be returned. At runtime an Integer is returned instead and when the JVM attempts to convert the Integer to String , BAM !


I can't understand that if we put "map.get(1);" there,we got no runtime exception.
The explanation says that " So when you pass a key, a String is supposed to be returned. ",but can't The JVM invoke implicitly the toString() method of Integer and also return a String?

Originally posted by Tarun Kumar:
This is because integer from -128 to 127 are cached by the Intger class.
so when a int within this range is created it is retrived from cache rather than creating a new Inetegr object.

You can find the cache for Integer class...
private static class IntegerCache {
private IntegerCache(){}

static final Integer cache[] = new Integer[-(-128) + 127 + 1];

static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Integer(i - 128);
}
}



Tarun:Thinks very much!

I have seen the source code of Integer class.
[CODE
private static class IntegerCache {
private IntegerCache(){}

static final Integer cache[] = new Integer[-(-128) + 127 + 1];

static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Integer(i - 128);
}
}

/**
* Returns a <tt>Integer</tt> instance representing the specified
* <tt>int</tt> value.
* If a new <tt>Integer</tt> instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Integer(int)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
*
* @param i an <code>int</code> value.
* @return a <tt>Integer</tt> instance representing <tt>i</tt>.
* @since 1.5
*/
public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
return IntegerCache.cache[i + offset];
}
return new Integer(i);
}
[/CODE]

It defines an statci inner class and only uses it in method valueOf,Is that means when we invoke


JVM wrap the int to Integer just like this:

JVM implictly invokes The Integer's method valueOf?
source:http://enigma.vm.bytemark.co.uk/webstart.html
John Meyers's SCJP 5 mock exam:question 7


Answer:false true false

lthree and lfour are two seperate objects. if the lines 1 and 2 were lthree = 2 and lfour = 2 the result would have been true. This is when the objects are created in the pool. When the references i and eye in the pool are compared 2==2 results in true and 2000==2000 is false since it exceeds 127.

I thought the answer would be false flase false
why number smaller than 128 get true?they are wrapped, they should be different objects.

it compiles well,if i write a constructor for class BigOuter like this:



it still compiles well,I can't use the constructor like this:

why?
12 years ago

Originally posted by Ivan Ivanic:
you can put label before any statement or block of code.
but you cannot call break labelX if you are not in its scope.
you are trying to call break label1 in switch case 3 but it is out of scope.
look this code about scopes:



Ivan: is this means that if i put label just before for or while like

this label1 will implicitly add block?