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 expression evaluation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "expression evaluation" Watch "expression evaluation" New topic
Author

expression evaluation

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi,


class Test
{
public static int[ ] getArray() { return null; }
public static void main(String[] args)
{
int index = 1;
try
{
getArray()[index=2]++;
}
catch (Exception e){ } //empty catch
System.out.println("index = " + index);
}
}



My question: how is this expression evaluated? Please tell the rule of this kind of expression evaluation, so that I could apply that in similar questions.


Source : Enthuware exam

Thanks,


cmbhatt
Ram Gopal
Greenhorn

Joined: Apr 06, 2007
Posts: 14
Hey is there any command in Java to trace the flow of control within the program..like in C language?
Matt Russell
Ranch Hand

Joined: Aug 15, 2006
Posts: 165
Yikes! It'd be a bit daft if you got anything like this on the actual exam.

As I understand it, the effect of "getArray()[index=2]++" is to call getArray(), assign 2 to index, look up the third element in the result returned by getArray(), and then increment the third element. In the context of your example, however, the getArray() call returns null, so a NullPointerException is thrown (and then caught and swallowed) after 2 has been assigned to index.

The JLS covers the exact rules for evaluating expressions, but that's probably overkill for this case.


Matt
Inquisition: open-source mock exam simulator for SCJP and SCWCD
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

about the line in the try block:
getArray()[index=2]++;

It consists of three parts:
getArray() one
[index=2] two
++ three


Chandra:
Please tell the rule of this kind of expression evaluation...

The Java Language Specification, Third Edition (JLS) says:
15.7.1 Evaluate Left-Hand Operand First



So the first thing happening is the method call (returning null). Now we have
null [index=2]++;

15.7.2 Evaluate Operands before Operation

So index (the int variable) gets its new value of two and the line says
null [2]++;

The left part - null[2] is an array access, JLS says:
15.13.1 Runtime Evaluation of Array Access
An array access expression is evaluated using the following procedure:
(... snip ...)
Otherwise, if the value of the array reference expression is null, then a NullPointerException is thrown.


The NPE leads us to
14.20.1 Execution of try-catch, no quote.

This moves us to the catch block. The Exception is caught, then the value of value is printed out (2).
That's it.

The ++ operation is never executed, as we left the try block due to the npe.




Ram asked:
Hey is there any command in Java to trace the flow of control

C is procedural, java is object oriented, so no, not really.
The only trick you could use is to have a look in the StackTrace, eg

Insert this at the point of code, you want to check. In the catch - block it is recommended to print the stack trace of the caught exception. The method (of class Exception ) is
printStackTrace()

But both of them wouldn't help you that much, because they'd only tell us, that we are in the main method (surprise, surprise) and indicate the line number.


Matt Russell posted Today:
The JLS covers the exact rules for evaluating expressions, but that's probably overkill for this case.


Agreed, but now, I've written it already...


By the way:

Chandra Bhatt posted:
No answer yet!


Don't hurry us.


Yours,
Bu.


all events occur in real time
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Fantastic explanation by Burkhard!


The ++ operation is never executed, as we left the try block due to the npe.



Why to not executing ++ operation. If it would pre ++.

15.7.2 Evaluate Operands before Operation

Doesn't this rule apply to this?


Thanks,
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Chandra Bhatt:
No answer yet!



Please stop posting these remarks of impatience. Or have you paid for some service you are are not getting?

EaseUp!
[ May 05, 2007: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Chandra asked:

Why to not executing ++ operation. If it would pre ++.
.

15.7.2 Evaluate Operands before Operation

Doesn't this rule apply to this?


Yes, sure, in either case
expression++ and ++expression,
the operand(expression) has to be evaluated before the increment.
We would land in the catch block in either case in our example.

Bu.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Aha, no nested quotes.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Thanks Burkhard for your replies.

I got it.

Thanks,
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Yup, itz really disturbing to see people using "nested quotes", passing on stress to others and some are so impatient to answer the queries in a good way! I saw few using bad emoticons and misguiding replies, just confusing the questioner(maybe, negative answering!). Hope these guys remember the help they get from others! Its after all "give and take" affair, remember!
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206

Its after all "give and take" affair, remember!


Hi Faisal,

YOu are right...in a give and take affair this doesnt happen..but..if someone takes the trouble of giving replies...it seems as if you are blind to the effort...

When it becomes a give and drop...give and drop...give and drop affair..thats the time you see impatient replies and bad emoticons...

Otherwise everyone is pretty friendly and focussed towards the goal of
certification thats all. No personal hard feelings here.

Thanks,
Megha
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Originally posted by megha joshi:

it seems as if you are blind to the effort...
When it becomes a give and drop...give and drop...give and drop affair..thats the time you see impatient replies and bad emoticons...

Personal attacks!!?? Noooooo!!
If the guys think they were correct in posting such replies, why did they remove their replies? Afraid? And now remember this one too.. "Politeness is to human nature what warmth is to wax." Try to "burn it" in your mind!
By the way, I wonder whats "give and drop affair"..its first time that Im hearing about it. Anyways, no time to think about it too..
Well..well..itz better to focus on the exam Megha, right? Good luck!
PS: No personal but human feelings.
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Thanks Faisal!!

Happy SCJP studying to you
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
I guess if someone took the name of this thread in another way. It was all about java expression evaluation not HUMAN.


Anyways thank you all for your replies.



------------------------
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Originally posted by Ram Gopal:
Hey is there any command in Java to trace the flow of control within the program..like in C language?


What in the C language allows you to "trace the flow of control within a program"?

Also, does Java have commands?

- Anand


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Originally posted by Chandra Bhatt:
I guess if someone took the name of this thread in another way. It was all about java expression evaluation not HUMAN.


Anyways thank you all for your replies.



------------------------

Chandra, maybe you want to check this KeepItDown
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Faisal Pasha posted Today 00:12
Yup, itz really disturbing to see people using "nested quotes"


Giggle, with nested quotes I meant something like this


And this is not possible.

Yours,
Bu.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Back to the original question - precedence details are a very, very small part of the current exams. Back in the old exams (1.2 and earlier), there were a lot of tricky questions concerning precedence, but that was reduced a lot in the 1.4 exam and reduced even more in the 5.0 exam.

My advice would be to make sure you've really nailed the important topics and then, if you just can't resist :roll: , you could spend some of your valuable time on this kind of puzzler...but really, don't bother.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: expression evaluation