• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

expression evaluation

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Ram Gopal
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey is there any command in Java to trace the flow of control within the program..like in C language?
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha, no nested quotes.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Burkhard for your replies.

I got it.

Thanks,
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Faisal!!

Happy SCJP studying to you
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic