Abahmane mustafa

Greenhorn
+ Follow
since Aug 22, 2007
Merit badge: grant badges
For More
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 Abahmane mustafa

Hello all of you,

In chapter 6 on the book:

Given :

import java.util.regex.*;
class Regex2 {
public static void main(String[] args) {
Pattern p = Pattern.compile(args[0]);
Matcher m = p.matcher(args[1]);
boolean b = false;
while(b = m.find()) {
System.out.print(m.start() + m.group());
}
}
}

And the command line:

java Regex2 "\d*" ab34ef

What is the result?
A. 234
B. 334
C. 2334
D. 0123456
E. 01234456
F. 12334567
G. Compilation fails.

......
the answer given in the book :

E is correct, they then gave an expalanation that gives answer D.

I thougt it was D :0123456, but it gives me E when i tested it.
But i don't understand how the output comes to be E and not D.

Can anyone here explain ?

thnx

[ October 28, 2007: Message edited by: Abahmane mustafa ]
[ October 28, 2007: Message edited by: Abahmane mustafa ]
Hello all,

In page 147 of the book we can see :
****************
class Animal {
static void doStuff() {
System.out.print("a ");
}
}
class Dog extends Animal {
static void doStuff() {
// it's a redefinition,
// not an override
System.out.print("d ");
}
public static void main(String [] args) {
Animal [] a = {new Animal(), new Dog(), new Animal()};
for(int x = 0; x < a.length; x++)
a[x].doStuff(); // invoke the static method
}
}
*******************
Running this code produces the output:
a a a

***********

I can't understand the difference between an ovverdie an redefinition in this case. For me it's clearly an override..but that is not legal for a static methode..
So is that piece of code legal or not?
How did you understood that ?

Originally posted by Prasanna Rajaperumal:
Static blocks are executed first when you class is loaded into the JVM.
(not a necessary, but is ensured to be executed before any other code in the class).

Non-static class level blocks are executed before the constructor of the class is called, when an instance of the object is created.

The super class constructor is called (default no-arg constructor) before subclass constructor.

Now apply these facts, you will arrive at the output.




ok Thanks a lot..

which class is first loaded ( Bird or Rapace)?

As Eagle extends Rapace , Rapace class is loaded before the Bird one.

Classes are loaded one after one following the inheritance tree. is that

true?

That is why we have r1 r2 before b1..

But how it come the Bird constructor is executed first?

the Bird class is loaded last so its constructor should excute after the

Rapace one , no ?

[ September 11, 2007: Message edited by: Abahmane mustafa ]
[ September 11, 2007: Message edited by: Abahmane mustafa ]
Hello evry body ,

i have find this code on a mock exam :

*********************

class Bird {
{ System.out.print("b1 "); }
public Bird() { System.out.print("b2 "); }
}
class Rapace extends Bird {
static { System.out.print("r1 "); }
public Rapace() { System.out.print("r2 "); }
{ System.out.print("r3 "); }
static { System.out.print("r4 "); }
}
class Eagle extends Rapace {
public static void main(String[] args) {
System.out.print("pre ");
new Eagle ();
System.out.println("eagle ");
}
}
**********************

The code compiles fine and gives the output :

r1 r4 pre b1 b2 r3 r2 eagle

I can't understand how !

Can someone explain what happens at execution time, what is first loaded ?.. to have this order in the output.

Many thanks by advance

Originally posted by Henry Wong:


When you compile a program, compilation errors are listed with error messages? What is the message that you got when you compiled it? And what about it do you not understand?

And .... Please Quote Your Sources.

Thanks,
Henry




I'm also trying to understand ..wyh we get the compile time error :

C:\Program Files\Java\jdk1.5.0_11\bin>javac AQuestion.java
AQuestion.java:15: reference to method is ambiguous, both method method(java.lang.StringBuffer) in AQuestion and method
method(java.lang.String) in AQuestion match
question.method(null);
^
1 error


thanks by advance

Originally posted by Jesper Young:
'iamprotected' is a method in class Alpha. You can't assign a value to 'iamprotected' as if it is a member variable.

The statement 'this.iamprotected = 10;' also will not compile.



Sure?

void accessMethod(Alpha a) {
a.iamprotected = 10;// illegal

It is a method of the class Alpha .

class Alpha {
protected void iamprotected() {
System.out.println("iamprotected");
}
}

I'm not considering it as a member variable, i'm calling the method from a reference to an a ojbect ..ok?
[ August 22, 2007: Message edited by: Abahmane mustafa ]
Hello all of you;

I found this example on the net ..but i can't understand the real reason why the line in bold is illegal, since it a reference of an a object that is calling the protected method

***
package Greek;

class Alpha {
protected void iamprotected() {
System.out.println("iamprotected");
}
}

package Greek;

class Gamma {
void accessMethod() {
Alpha a = new Alpha();
a.iamprotected();// legal
}
}

class Gamma extends Alpha {
void accessMethod(Alpha a) {
a.iamprotected = 10;// illegal
this.iamprotected = 10;// legal
}
}

Can anyone explain ?

thank you a lot
[ August 22, 2007: Message edited by: Abahmane mustafa ]