//The following class will print '2' when compiled and run. class Confuse { public static int[ ] getArray() { return null; } public static void main(String[] args) { int index = 1; try { getArray()[index =2]++; // 1
} catch (Exception e) {}
System.out.println(" index=" + index ); } } Hey Friends, I am confuse with line 1 getArray()[index =2]++; Please explain how does this line compile?
Ivan Ivanoff
Ranch Hand
Joined: Jan 04, 2002
Posts: 56
posted
0
hi - i just commented out try/catch block and i got NullPointerException. So you handeled this exception with catch(Exception e){} line . One of the rules of exceptions says that if you catched an exception than programm flow will execute code below try/cath block. I am not an expert yet, but may be it will help. Ivan
anand raman
Ranch Hand
Joined: Jun 06, 2001
Posts: 66
posted
0
getArray()[index =2]++;
will be translated to null[index=2]++ null[2]++ which basically tries to increment the 3 rd element of a null array. This will throw a null pointer exception which is caught by the exception block Hope this helps -Anand
Ivan Ivanoff
Ranch Hand
Joined: Jan 04, 2002
Posts: 56
posted
0
now i am confused to - where did you get this question ? Ivan
Samith Nambiar
Ranch Hand
Joined: Mar 14, 2001
Posts: 147
posted
0
getArray()[index = 2]++; // 1 The first thing that happens here is that index is set to 2 then you can look at it this way ... getArray() returns an integer array and the value of the third element of that array is incremented by 1. /SAmith
Seany Iris
Ranch Hand
Joined: Jan 08, 2002
Posts: 54
posted
0
I just try it, and it give output---"index=2" I agree with Ivan Ivanoff. Because try caught the exception,the following code of try won't run. So the index doesn't increase 1. Finnally the code will run and give output---"index=2"
help you means help me
Seany Iris
Ranch Hand
Joined: Jan 08, 2002
Posts: 54
posted
0
I got a new confuse,Please help me. I rewrite the code: class Confuse { public static int[ ] getArray() { int [ ] arys = {1,1,1,1,1};; return arys; } public static void main(String[] args) { int index = 1; int test = 0; test=getArray()[index =2]++; try { getArray()[index =2]++; // 1 } catch (Exception e) {} System.out.println(" index=" + index ); System.out.println( getArray()+"[" + index + "]=" + test ); } } give me the output: index=2 [I@310d42[2]=1 I really donn't understand it. Who can explain it to me,thanks.
Samith Nambiar
Ranch Hand
Joined: Mar 14, 2001
Posts: 147
posted
0
what is the output u expected ... can u pls explain what exactly u dont understand here /SAmith
shiren shah
Greenhorn
Joined: Nov 16, 2001
Posts: 14
posted
0
Thanks guys but still i am not clear how can any method assigned as array. anybody could explain with some simple example. Thanks _______________________________________________ Time never stop
Rob Ross
Bartender
Joined: Jan 07, 2002
Posts: 2205
posted
0
Hi Shiren. Are you confused about the array element access expression, or the fact that you are using a method expression that resolves to an array? here's a simple program
I hope this helps make it clearer for you! [ January 08, 2002: Message edited by: Rob Ross ]
Rob
SCJP 1.4
mark stone
Ranch Hand
Joined: Dec 18, 2001
Posts: 417
posted
0
just to make sure again. are you sure that your explaination about getArray()[index=2]++ is correct ? it is confusing but good to learn this. just let me know if you are sure about this. (i think you are correct but just in case...)
Originally posted by Samith Nambiar: getArray()[index = 2]++; // 1 The first thing that happens here is that index is set to 2 then you can look at it this way ... getArray() returns an integer array and the value of the third element of that array is incremented by 1. /SAmith
shiren shah
Greenhorn
Joined: Nov 16, 2001
Posts: 14
posted
0
Thanks Rob, It's a nice example to clear the concept. well, i was confused, the method which was returing null and was trying to access the array element.
Chris Graham
Greenhorn
Joined: Jan 08, 2002
Posts: 22
posted
0
I think in the first example everyone is finding this statement confusing (I know I did): getArray()[index =2]++; getArray() evaluates to an array, therefore it's the same as: x[2]++ Where x is an array. The fact that it didn't blow up at run time as stated earlier is due to the try/catch block. When code in the try block throws an exception it is handled in the catch block. Although you don't actually have to do anything in the catch block to handle it, execution will none the less continue. Hope this helps, --Chris
Seany Iris
Ranch Hand
Joined: Jan 08, 2002
Posts: 54
posted
0
Originally posted by Samith Nambiar: what is the output u expected ... can u pls explain what exactly u dont understand here /SAmith
I expected that the result should be: index=2 arys[2]=2 Could you tell me how I can get the result? [ January 10, 2002: Message edited by: Seany Iris ]
Tony reedy
Greenhorn
Joined: Jan 11, 2002
Posts: 21
posted
0
Hi Shiren I am not able to explain your question instead I am adding to your question test=getArray()[index =2];//compiles well without ++ getArray()[index =2];//compiler error ----invalid expression. If any one explains this,that would be great. -Tony
Jose Botella
Ranch Hand
Joined: Jul 03, 2001
Posts: 2120
posted
0
to Tony
test=getArray()[index =2];//compiles well without ++ getArray()[index =2];//compiler error ----invalid expression.
the second result in something like 1; this is not valid to Seany getArray returns a diferent array each time it is invoked. Even if the code were changed to increment the same array, "test" is printed, not an element of the array.