• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

marcus green mock exam

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could someone explain the program to me (especially static Q1 processor A())
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();
}
 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry this is from jtips mocks and not marcus
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Sona,
You must have checked output. This program will give output :
true is a boolean Operator
This can be explained as below.
First static initialiser block is executed at time of loading the class, so it prints true.
(b'cos "Java".startsWith("") is true)
Then in main() method, processorA() method of class Q1 is called, which is static method.
The statement ("java").startsWith("null") will be evaluated to false, b'cos here "null" is string
and which is not same as ""
So, else part will be executed and null will be returned.
Point is even if null is returned, how processorB() is executed and NullPointerException is not thrown.
This is because processorB() is static method. For executing static method, the object reference is not requied.
It can be exaplained with simple example,
Q1 q1 = null;
q1.processorB(); // This is correct statement
I think this will make things clear.
Regards,
Sujit
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good explanation Sujit...
You have correctly addressed the querry.

Ravindra Mohan.
 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
processorA().processorB()
can u explain this i don't understand what this means
and what is it doing
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
processorA().processorB()
If you take the first part of the above code processorA(), it returns an object of type Q. So, when you say
processorA().processorB(), it means that you are calling processorB() of an object returned by processorA().
I am not very good at explaining..hope this helps!
Madhu
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic