Peter Ricke

Greenhorn
+ Follow
since Dec 07, 2007
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Peter Ricke

Thanks a lot.
I was to blind to see this
I Don`t understand the following behaviour:
i habe an object referenced by variable a, im synchronizing 2 methods on it
first method calls wait on it.
second method creates a new object and assigns it to varable a,
than synchronizes on a (the new Object!!!)
But when calling notifyAll() a waiting thread (who called meth1)is waked up.
But is`nt it true, that we are dealing here with 2 different objects due to the new object instantiation?




By the way:
the following code throws an IllegalMonitorStateException:


Thanks
Thank you again,

at least one half of the confusion is gone.
It seems that it is possibly necessary just to memorise the other half;-)

Regards,
The answers they provided are true, i checked it (didnt know also).

As far as i learned by now, you are only allowed to instantiate an Array of generic types if the generic type symbol is ?
e.g. new List<String>[3]; is not allowed,
but new List<?>[3]; is.
Therefor you can only declair List<?> []l ; as a variable to this construct.



The other point is that in a generic Method we can not
instantate the generic type, eg:

<T> T method(T t){
return new T(); //not ok !!! What would T be?
}
but we can return null or also return ((T) new XObject()), but for the latter we should write <T extends XObject>

Hope this is right, thats it as far as i understand
Hallo Kelvin,
thank you for the anthwer, but...

if

It [java] does allow automatic narrowing following by boxing


why can`t i compile
Float f = 5.0; //not ok
from my point of view, i`m trying here to narrow and than box.
OK, maybe double cannot be narrowed to float but
Integer i = 3L; also does not work. (Narrowing also not possible here?)

and if

Java does not allow for automatic widening followed by boxing, even for compile-time constants


why does my 2nd example
Short s2 = (byte)5;
compile ? I am widening (from byte to short) or am I not?

Hopefully yours...
Uppps

one of the last lines went wrong:

i*i ==2 as now i==2


should be 1*i ==2 as now i==2
3 Things to know:

1) result =5+i++%4*i ;
is equal to
result =5+(((i++)%4)*i) ;
because:
++ and -- goes first
than */ and %
and at last + -

if two operations are equal (like * and %) move from left to right

2)i++ returns the i-value before the increment (here 1)
but after this (left side of i++) i = 2

Summary :
i++%4 == 1 (as i++==1)
i*i ==2 as now i==2
5+2==....
Hope this helps...
Try to think about the two thinks completly separated one from another:
One is defining a class Tester, the other defining a variable of type Tester

When defining a class like

you declair that every instance of this class will have to have 2 parameters (of cause you can leave the parameters at all, but if paramters are used...).
The first has to be whatever the implementer wants.
The second has to extend Number , so could be Integer or Long or...

This guarantees that Methods of tester are acting only on Number-Instances, not e.g. on Strings..


If than someone instantiates your class

she is doing 3 things:
a) declair a variable named teste.
This can hold every Teste-object which has parameters String ans Something superior to number.
b) create a new Teste-Object with Parameters String and Number
c) assign the object to the variable

Due to your class-definition the object could also be a
new Teste<Integer,Integer>()
but you would not be able to assign to the variable.

On the other hand, later inb the program, you could instantiate another tester-object eg (new Tester<String,Integer>()) and assign to your variabe tester.
This is ok cause Integer is "super" to Integer.

Other Instance-types are not possibel to assign to tester,because
var 1 has to be a String and
var 2 has to be super to Integer and extending Number and this is true only for Number and Integer

Hope this helps
Maybe there is a anthwer to this post allready somewhere, but i did�nt find...

Short s = 5;
this is ok, cause narrowing a compileTimeConstant and than boxing is ok

Short s2 = (byte)5;
this is ok, because again its a compileTimeConstant, so in this case even widening and than boxing is ok (what it is not in other cases, e.g.

Byte b = (byte)5;
Short s3 = b; //Not OK Widening than Boxing

But why is the following NOT ok?
Like in the 2nd example i`m widening and than boxing a compileTimeConstant

Long l = 5;

The more i try to understand this stuff , the more i get confused maybe after all i dont see the tree in the woods?

Thanks
old thread, but i hope it comes up to be seen by someone...

i stumbled about the same question (its from a John Meyers mock exam)
i decided to say "none of the above" as two doors (both of type door) seem not to corrspond to two different types (long & int)...
is there a general rule for this for the real exam?

Thanks
i was asked by private message to explain my example. maybe its interesting for someone else ...

2 things to know here
1)
every number tipe in java is signed,
so the first bit corresponds to the sign of that number:
0 is positive, 1 is negativ
2)
Explicit casting bigger types (eg an int to a byte) removes the leading bits.
e.g. you have a int (32 bit) 00000000 00000000 00000000 00000011 (3)
after casting to byte (8 bit) : 00000011 (still 3)

But if you habe a longer number (eg 130), you loose information:
00000000 00000000 00000000 10000010 (130)
becomes 10000010
here the leading sign is positive, but (ups)a positiv sign means a negative number!!! (see above)
To find out, how a negative number is calculated, you can look here
(but its not relevant for exam, i hope)
in short : substitute every bit with its complement
(here we get 01111101) and add 1
(we get 01111110) which corresponds to 126. As we know it is a negative number (see above) it has to be -126
but again: dont worry about for exam.
to play around, you can use e.g. Integer's parse()-Method with 2 parameters, first a binary String ("01111110") that a 2 (for binary)
You can print out the decimal number than...
regards
I found some additional information here, maybe someone`s interested in:


NEW SUN CERTIFIED PROGRAMMER FOR JAVA PLATFORM 6 EXAM
What are the differences between Sun Certified Programmer for Java Platform 5 (CX-310-055) and 6 (CX-310-065) exams?

Questions concerning System.gc() have been removed.
Coverage of the java.io.Console class has been added.
Coverage of navigable collections has been added.
Several of the previous objectives have been strengthened (so you can expect more questions and more detailed questions on them). These strengthened objectives include: exception handling, collection classes and collection interfaces, assertions, threads, and flow control.

Erratum:

i wrote


Object o = new Juicy_Fruit();

than String s = (String) JF1 ; would be no problem



but wanted to write :

In generall, you are allowed to explicit cast only, if a correct type cast is "possible". If the compiler "thinks", it is not, there will be a runtime exception.

e.g.: you declaired:

Juicy_Fruit JF1 = new Juicy_Fruit();



Because it is obvious, that a Juicy_Fruit object never could be e.g. a String object it is not allowed to cast
String s = (String) JF1;

But if you would have declaired

Object o = new Juicy_Fruit();

than String s = (String) JF1 ; would be no problem for the compiler (not talking about runtime right now) because an Object of type Object could be everything, so why not a String?

So for the compiler everything you codes is just fine
#1 is even an implicit cast (or would be, if you didnt tipe it explicite) as JF1 IS A sweet
#2 is ok for compiler (as JF1 could be a Round_Fruit because Round_Fruit IS A Juicy_Fruit
#3 is ok also because Juicy_Fruit IS A Fruit, so a Fruit-object could be a Juicy_Fruit


At runtime you have to ask yourself : is the object really what i�m trying to make out of it?

#2 is problematic because JF1 could be a Round_Fruit but IT IS NOT. So here a ClassCastException will be thrown.
I�m also mixed up by the availability of 1.6 exam as i�m going to
take on of both during this month (hopefully).

Despite also having never heard about this 3 new topics in 1.6 exam,
there seems to be one real remarkable benefit:
the testee has 210 instead of 175 minutes time but no additional question.
As i�m afraid that my speed of thinking could realy be limiting factor (for nice percentage at least) this seems to be very attractive.

But of cause i�m full of doubts : what is the cost for this additional time?
Can i expect the same question pool as in 1.5 exam (so the questions we all are or were prepairing for) and just a handfull of them replaced by new 1.6 things or will there be a complete new (and maybe more complicated) question pool which would explain the additional exam time?

How was it as 1.5 came up with respect to 1.4 ?