• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Exception?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
14. int[] x=new int[2];
15. try{
16. x[2]=x[1]/0;
17. }catch(NullPointerException e){
18. System.out.print("NPE ");
19. }catch(ArithmeticException e){
20. System.out.print("AE ");
21. }catch(ArrayIndexOutOfBoundsException e){
22. System.out.print("AIOOB ");
23. }



what is output??
The question is from ExamLab.....

 
Rancher
Posts: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you tried it?
 
harsh anajwala
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
according to ExamLab it's Arithmatic Exception bt dnt you thnk that it shud be NullPointer EXc...??
 
Sheriff
Posts: 9689
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Harsh please Use Real Words on javaranch.

Why do you think there will be NullPointerException?? The array is of primitive type int, so the elements in the array will be initialized with the default value i.e. 0. If it was an array of references, then there was a chance of NullPointerException as the elements of an array of references is by default initialized with null...
 
Sheriff
Posts: 6833
1299
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

harsh anajwala wrote:according to ExamLab...


You *must* try the code yourself, instead of relying on other references.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't this a question about operator precedence because in the book K&B they say we are not required to know them.
 
Devaka Cooray
Sheriff
Posts: 6833
1299
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing to do with operator precedence here. But you need to know the procedure of assigning a value to a variable (or an array element).
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this code and it is AE.

I want to ask one thing that the array has been created with two elements and they should be x[0] and x[1].

Why the compiler doesnot complain when I try to assign something to an array element which is out of the bounds of that array and if the compiler doesnot do that then ArrayIndexOutOfBoundsException should be thrown.

I don't know whether I am able to ask my doubt or not but please answer it.
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

indra negi wrote:Why the compiler doesnot complain when I try to assign something to an array element which is out of the bounds of that array and if the compiler doesnot do that then ArrayIndexOutOfBoundsException should be thrown.


What is the output of RHS in the code ?


 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will result in ArithmeticException.

 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe, you are asking when you are writing a code something like this


You must be trying something like that. Actually when we are talking about the statement like new int[2], it is basically making an array object on
the heap. The object creation will happen at run-time. Right now, the compiler is aware that, it is an int type array, it allocates the size at run-time
to my information. That's why right now the compiler won't know that you are accessing something out of the bounds of array. It will only be caught
by the JVM at run-time

Hope this helps,
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok then it should throw exception at runtime as the array element is not within the bounds.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes surely it does/
 
harsh anajwala
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And just to add something....

When an assignment statement is encountered ,first right side is exectued so in this case it will throw Arithmatic Exception so then after there is no chance for ArrayindexoutofBound Exception....

and if the Array is of objects than there should be NullPointerException...what say??!!
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Harsh is correct in saying that the expressions are evaluated from right to left in assignement.
An ArrayIndexOutOfBoundsException will be thrown if the expression is replaced with
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. Thanks to all.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You correctly stated it harsh. Thats right in assignment, it will be evaluated from right to left.

Cheers,
 
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic