Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why am i getting strange results?

 
ashok khetan
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code:
class kon {
public static void main(String args[]) {
String a[] = new String[5];
for(int i=0; i<5; a[i++]="hi");

System.out.println(a[6]); //statement 1
System.out.println(a[5]); //statement 2
}
}
i am getting strange results when i execute the above code. If we initialize the array elements without using for or while loop, it works fine and throws ArrayIndexOutOfBounds Exception as it should be. But if we use the for loop as per above then it shows as following:
on executing only statement 1 - null
on executing only statement 2 - throws NoSuchFieldException
on executing both the statements- throws NullPointerException
can u please help me why is it so?
(replace the for loop with while loop and then execute both the statements changing the order, it's different from the for loop too).
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
I got results as follows...
On executing Only 1st Stmt - ArrayIndexOutOfBoundsException
On executing 2nd stmt - ArrayIndexOutOfBoundsException
On executing BOth - ArrayIndexOutOfBoundsException
How come u r getting null for the first one? Compile it once again...and even after using while loop the result is the same.
so i dont understand what do u mean by...

(replace the for loop with while loop and then execute both the statements changing the order, it's different from the for loop too).

Rashmi
 
Guoqiao Sun
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in my computer, the output is always like:
<code>
java.lang.ArrayIndexOutOfBoundsException
</code>
What is the matter?
guoqiao
Originally posted by ashok khetan:
code:

 
Nils W Widmer
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what I find cool u can actually initialise an array this way,
if someone has some other cool things
BTW:
I get ArrayIndexOutOfBoundsException using JDK1.3

------------------
[-_-]
 
ashok khetan
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all!
I compiled it again and i am getting the same results as i have written in my query. I am using JDK1.2.2. Don't know why it is displaying the output in this way.. anyway i would like to go with what majority says..
quote:
-------------------------------------------------------------------------------
(replace the for loop with while loop and then execute both the statements changing the order, it's different from the for loop too).
--------------------------------------------------------------------------------
In the above quote, i actually mean that if we use while loop and execute both the statements by changing their order(like stmt2 first and then stmt1), then the result will be different than what we get in case of 'for' loop using the order what we use with while.

thanx.
ashok.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic