• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compiler question

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, guys. Im really confused about what the compiler can and can not do. In the following example



the compiler is unable to see that im adding an Object type object to an array that takes A objects, which shouldnt be allowed. But the compiler does allow it. And the JVM generates a run time error. I know that the compiler has no access to objects, but i did write "new A" in the 3rd line, so why cant the compiler recognize its an array of A type even when it is written in my program?
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is the compiler supposed to keep track of this:



in other words, between where it is initially defined and where it is being used, things could have changed. the compiler can't keep track of all that.
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler 'could' possibly have caught that particular 'case'. However like fred points out, the compiler would not be able guaranty that it 'will' catch all cases of incorrect assignment.
And that's how its decided if a particular thing should be caught by the compiler or not. (It's a bit more complex - language specification, features - but that remains the crux)

There are tools which can work with more assumption and call out such code as error - e.g. findbugs
Such tools can be more liberal in what they report and are allowed to report false positives (mark correct stuff as possible suspect) and false negatives (skip marking some wrong stuff).
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic