Zhenyi Luo

Ranch Hand
+ Follow
since Sep 03, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
1
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Zhenyi Luo

Simon Edwards wrote:In the following code, does method row() in interface Derived overrides method row() in interface Base, or hides it?



Thanks.



I believe it is override.

Ashish Dutt wrote:Dear friends,
I have a slight confusion here.
My question is, "In order to take the OCJP7 certification exam, is it imperative to take the OCJA 7 exam first?"
Please do clarify the doubt.



Take OCJP 6 first is another option.

Joe Allen wrote:Thanks for replying.
If T is Integer, wouldn't the following code blow up?

class Pie<Integer> {
public static void main(String... args) {
Pie<Fruit> p=new Pie<Fruit>(); //Pie is Integer, not Fruit???
}
}

Zhenyi, thanks for the generic link. In fact, I postponded my OCPJP6 exam from Friday to Monday, just to study generics in that link over the weekend. But after the exam, I am still confused about it. Luckily I did get the generic question in the exam correct.



Congratulations!

Jesper de Jong wrote:

Zhenyi Luo wrote:The thread class's start method is overloaded, and can not start the run() method.


It is not overloaded, but overridden.

Overloading = multiple methods with the same name, but different parameters
Overriding = method in a subclass which has the same name and the same parameters as a method in a superclass

If you override a method, then the method in the superclass that you are overriding is not called automatically. If you want the superclass version to be executed as well, you'll need to call it explicitly with super:


Thank you for noticing this out to me. I think about overridden in my head, but type overloaded.
What a stupid mistake, lol
Yes, you are right. If the overloading start() does not start the run() method, it will not run by itself.

vishal mishra wrote:

Dan Drillich wrote:The + is counted into the 5.

Please try -



which prints +000009758.

Regards,
Dan





Thanks for replying..

I understand that but i want to know why it didn't print 09758 ? Why +9758?Why it has given preference to '+' not to ' 0' ?

And when i tried this statement

then also it prints the same +9758

I am not getting....so please explain....
Thanks



"+" and '0' are both considered. Since the width is already 5 after adding "+", 0 is not added.

Joe Allen wrote:class MyThread extends Thread {
public void run() { System.out.println("MyThread: run()"); }
public void start() { System.out.println("MyThread: start()"); }
}

public class ThreadTest {
public static void main(String args[]) {
MyThread myThread = new MyThread();
myThread.start();
}
}

It prints "MyThread: start()".
My question is why run() in this class is not called to print out "MyThread: run()"?
Would anyone care to explain?



The thread class's start method is overloaded, and can not start the run() method.
The original start() method is as follows:
public synchronized void start()
{
if ((this.threadStatus != 0) || (this != this.me))
throw new IllegalThreadStateException();
this.group.add(this);
start0();
if (!(this.stopBeforeStart))
return;
stop0(this.throwableFromStop);
}

Joe Allen wrote:From K&B's mock exam:

class Food {}
class Fruit extends Food {}
class Apple extends Fruit {}
//insert code here
public static void main(String... args) {
Pie<Fruit> p=new Pie<Fruit>();
}
}

Correct answers:
A:class Pie<T extends Food> {
B:class Pie<T extends Fruit> {
F:class Pie<T> {

Would someone please explain why answer F?
Thanks.


T does not necessary need to extends anything, look at this: http://docs.oracle.com/javase/tutorial/java/generics/types.html

Joe Allen wrote:import java.io.*;

abstract interface Veloz { void metodo(String x); }
public class A {
private abstract interface Veloz{ Number metodo(int x); }
abstract static class B{
private abstract static interface Veloz{void metodo(long x) throws Exception;}
public void metodo(){
class Opa implements Veloz{
public void metodo(long x) throws IOException { }
}
}
}
public void test(){
class Opa2 implements Veloz{
public Integer metodo(int x) throws RuntimeException {
return (Integer)(int) .5f;
}
}
}
}
===========================
private abstract static interface?
abstract static class?
I don't understand. Hopefully I don't get all confused and messed up before taking the exam.



Interfaces can be members of class definitions and can be declared private or protected there. Inner class is similar.

vishal mishra wrote:Namaste Friends !!

Is it good for me to complete this exam in 3 hours or I must try to Complete this exam in 2 hours 30 minutes which is the time limit for real exam ?

Please guide me....


Thanks


It's always better to finish the exam in advance then you have plenty of time to review.

Adam Crawford wrote:Hi all,

I've got the SCJP 6 exam coming up in just over a week and was going over some CertPal questions for practice. This evening I came across this one:


With one of the following answers:
1) Hi
2) No Ouput
3) Compile error
4) Runtime error
5) None of the above

I chose 4 (runtime error) as the class doesn't implement Comparable, but it was marked wrong and the correct answer given was 1 (Hi) because:

"The code to compare the elements in the set will kick in only when the next element is added. Adding more than one element to this set will result in a runtime exception".

Yet, when I try this out, I get a runtime exception until I add an "implements Comparable" and a compareTo method - at which point I get the answer "Hi".

Am I missing something?

Thanks.
Adam.



When I try it out, it prints "Hi", no need to modify anything

Mahtab Alam wrote:public class Hose<E extends Hose>
{
E innerE;

public E get(E e,Hose<E>e2)
{
return e.getE(); //compiler says found Hose
}

E getE()
{
return innerE;
}
}

I know for compiler get() methods return type can only be E nothing else

Please explain clearly ,



I guess it's because when get(E e,Hose<E>e2) is called, e must be instantialized as a specific object corresponding to specific class.
If we want to return E, we have to add a cast. (change return e.getE() to return (E) e.getE() )
On the other hand, if e is not involved, it always works. (Change return e.getE() to return getE() )

Mahtab Alam wrote:
public class MyStuff {
MyStuff(String n) { name = n; }
String name;
public static void main(String[] args) {
MyStuff m1 = new MyStuff("guitar");
MyStuff m2 = new MyStuff("tv");
System.out.println(m2.equals(m1));
}
public boolean equals(Object o) {
MyStuff m = (MyStuff) o;
if(m.name != null)
return true;
return false;
}
}


Answer is :prints false and MyStuff does not fulfills Object.equals() contract

so it means Object.hashCode() contract is included in Object.equals() contract



The answer is true, did you miss some info?

Mahtab Alam wrote:Now compiler complaint a Local variable x and a block variable x


class R
{
public static void main(String args[])
{
show(10);
}
static void show(int x)
{
for(int x=1;x<2;x++)
System.out.println(x);
}
}


compiler say x is already defined in show(int)

When scope conflict occurs then there is problem.
So why there is no scope conflict in the first code


Shadowing happens when local variable "shadow" the instance variable temporarily using the same name, but local variable could not "shadow" another local variable. Refer to this discussion: http://stackoverflow.com/questions/4623334/question-about-variable-scope-and-shadowing-in-java

Mahtab Alam wrote:class Cast
{
public static void main(String args[])
{
Short s=new Short((short)10);
show(s);
}

static void show(int i)
{
System.out.println("Great");
}
}

This works so it means that Short is first unboxed to short which is then widened to int



That's correct.