File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes general Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "general" Watch "general" New topic
Author

general

vidhya subramaniam
Ranch Hand

Joined: Jul 14, 2001
Posts: 91
can someone please explain me the foll codes:
public class Q1 {
static void processorB() {
System.out.println(" is a boolean Operator.");
}
static Q1 processorA(){
Q1 q=null;
if (("java").startsWith("null"))
return q;
else
return null;
}
static {
System.out.print("Java".startsWith(""));
}
public static void main(String[] args) {
processorA().processorB();
}
}

ans- prints true is a boolean operator

class B{
protected int x;
B(){ x = 10;}
B(int a){ x = a;}
void m1(){x = 20;}
void m1(int x){this.x = x;}
}
public class A {
int x;
A(){x = 20;}
A(int x){this.x = x;}
void m1(B x){this.x = x.x++;}
public static void main(String arf[]){
A x = new A(20);
B y = new B();
System.out.println(x.x);
x.m1(y);
System.out.println(x.x);
//y.m1(x);
System.out.println(y.x);
((A)x).m1(y);
System.out.println(y.x);
}
}
ans-20
10
11
12
Guy Reynolds
Ranch Hand

Joined: Oct 27, 2000
Posts: 61
Question 1...
Here's how it plays out:
The Q1 class is loaded by the java.exe program. Loading the class causes the static initializer block to run. The static initializer block prints out the result of
"Java".startsWith("")
which happens to be the boolean true because the api for the startsWith method of the String object states:
"Note also that true will be returned if the argument is an empty string..."
The arguement you have listed is an empty String, so the result is the boolean true. Therefore, true is the argument for the System.out.print method and the result is "true" output to the standard output device(monitor).
Next, the main method is invoked and the statement
processorA().processorB();
is evaluated.
Java evaluates statements from left to right, so processorA() is evaluated first. The method processorA() will always return a reference that contains null, so it is easy to think that the result should look like:
[null].processorB();
(where [] indicates the result of evaluation)

Which should fail because it is illegal to attempt to invoke a method of null. However, what is actually returned is not null, but a reference of type Q1 that contains a value of null. Since the processorB() method is static, it is not related to any instance of the Q1 class, but to the Q1 class itself. Any reference of the Q1 type, regardless of the actual value it contains, can invoke this static method. So what you really have, in essence, is:
Q1.processorB();
which is perfectly fine and causes the String
is a boolean Operator.
to be output to the standard output device.
So the entire resulting output is:
true is a boolean Operator.

[This message has been edited by Guy Reynolds (edited August 14, 2001).]
Guy Reynolds
Ranch Hand

Joined: Oct 27, 2000
Posts: 61
I don't really understand what your question is concerning the second set of classes...
the play-by-play is as follows:
Note: security is never a problem, because the types of security being used is "default" and protected, which both allow access to classes in the same package, which these are.
A x = new A(20); - the variable of type "A", named "x" now contains a reference to an object of type "A", which has a member variable of type "int", named "x", which has a value of 20.
B y = new B(); - the variable of type "B", named "y" now contains a reference to an object of type "B", which has a member variable of type "int", named "x", which has a value of 10.
System.out.println(x.x); - prints out the value of the variable "x"'s member variable named "x", which is 20. So the result is: 20 is printed to the screen;
x.m1(y); - the "m1" method of the "x" object, which is of type "A", is invoked with an argument of the "y" object, which is of type "B", which matches the paramater listed in the definition of the "A.m1" method.
void m1(B x){this.x = x.x++;} - this.x, the invoking classes member named "x", is set to x.x which equals 10. x.x is the variable named "x" of the object of type "B" that is referenced by the value copied to the parameter variable "x", which was copied from the variable "y" that was passed as an argument. Then the variable named "x" of the object of type "B" that is referenced by the value copied to the parameter variable "x", which was copied from the variable named "y" that was passed as an argument, is then incremented from 10 to 11.
System.out.println(x.x); - does the same thing as it does in the explanation above, except that "x.x" was set to 10 by the preceding method. So the result is: 10 is printed to the screen.
System.out.println(y.x); - does the same thing as above except the variable named "y" contains a reference to an object of type "B" whose member variable named "x", of type int, was incremented from 10 to 11 two steps ago. So the result is: 11 is printed to the screen.
((A)x).m1(y); - does exactly what the call to x.m1(y); did. Casting the variable named "x" to type "A" is meaningless... the variable named "x" is already of type "A". So, like in the previous method invocation, the result is, the variable named "x"'s member variable named "x" is set to the variable named "y"'s member variable named "x"'s value which is currently 11 and then the variable named "y"'s member variable named "x" is incremented from 11 to 12.
System.out.println(y.x); - prints the value of the variable named "y"'s member variable named "x", which is 12. So the result is: 12 is printed to the screen.
And therefore, the output is:
20
10
11
12
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3313
    
    7
You explanation for first one is not quite correct. The reason is that the basic premise ( The Q1 class is loaded by the java.exe program.) on which your explanation is based is irrelevent. And then you've explained what happens at runtime.
But the point is, static methods are always bound at compile time. The compiler figures it out (of course, at compile time!) that the return type of processorA() is Q1 and processorB() is a static method. So, it binds the call to Q1.processorB() right then. There is no question of inspecting the value returned by processorA() at run time. And that's why there is no NPE.
HTH,
Paul.
------------------
SCJP2 Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: general