• 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

Java Language Specification

 
Ranch Hand
Posts: 53
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi every one,

As a developer , is this important to read and understand Java Language Specification ?
I was going through this document and i do found that it has a lot of things and also it is complex to understand.
For example  i was reading Chapter 5. Conversions and Contexts  (http://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html).

It is easy to say that we can not cost integer to string , compiler will give error , however the class which is not related to interface still you can type cast at compile type , but run time it will throw exception .



Is there best way to understand casting in detail ,  ?

 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prateek.

Any question that starts with, "Is it important to..." is likely to provoke a wide variety of answers. Others can tell you what they think, but here's mine: It is more important to know about the JLS than it is to read the JLS. That's because, as you have observed, is can be complex. I think it's complexity arises from the fact that it is the definitive document for Java. Whatever the JLS says is, in a way, the law. Anything that differs from the JLS is not Java. That which matches the JLS, and does so without omission, is Java. That's what it's for, to define what is (and, by symmetry, what is not) Java. Around here, that means it gets used to settle raging debates friendly disagreements, more than anything else. As a way to learn Java, however, I would not recommend using it much.

Regarding casting, I am a bit surprised the compiler doesn't catch that error, since You neither implements Me nor is it subclassed from anything (which might either implement Me or be further subclassed from something that does). Maybe one of the more experienced people here can tell you why it doesn't.
 
Marshal
Posts: 79239
377
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stevens Miller wrote:. . . You neither implements Me nor is it subclassed from anything  . . .

...but it is possible to create a subclass of You which implements MeNow you can make that cast (if you can get my class name to compile ‍). I think the compiler is programmed to give casts the benefit of the doubt even when the programmer can see the cast will never work.
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Stevens Miller wrote:. . . You neither implements Me nor is it subclassed from anything  . . .

...but it is possible to create a subclass of You which implements MeNow you can make that cast (if you can get my class name to compile ‍). I think the compiler is programmed to give casts the benefit of the doubt even when the programmer can see the cast will never work.



Oh dear God...

So downcasting a You to a Me becomes possible when the actual object is a subclass of You that implements Me. You'd be limited to the interface, but you'd get the implementation of the subclass, wouldn't you?

Now, I think I see how that would be more work for the compiler than is reasonable. Sure, if we knew the entire universe were in one file, and that file contained no subclasses of You, then the compiler ought to be able to see that (Me)you is an impossible downcast (assuming you is a You). But, if your subclass were defined elsewhere, the compiler would virtually have to run your code to know if you were really a Y'all.

Except...

In Prateek's example, he's not asking about casting a variable that contains a reference. He's asking about casting a reference returned by new. Seems like the compiler ought to be able to look up the hierarchy from You's declaration and see if it descends from any class that implements Me and, if not, call that a compile time error. Am I missing why it can't?
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stevens Miller wrote:It is more important to know about the JLS than it is to read the JLS.


Agreed. Books are useful as the authors have digested this information and turned into a more readable form.
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stevens Miller wrote:In Prateek's example, he's not asking about casting a variable that contains a reference. He's asking about casting a reference returned by new. Seems like the compiler ought to be able to look up the hierarchy from You's declaration and see if it descends from any class that implements Me and, if not, call that a compile time error. Am I missing why it can't?



Maybe what I was missing was something called "bytecode instrumentation," a process so arcane it has me looking for garlic to hang in the window.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn't my explanation make you want garlic too? Let's see if we can't find a better explanation.

The cast in question is a narrowing reference conversion according to the JLS. This is what that section says:-

5.1.6. Narrowing Reference Conversion

Six kinds of conversions are called the narrowing reference conversions:

  •    From any reference type S to any reference type T, provided that S is a proper supertype of T (§4.10).
          An important special case is that there is a narrowing reference conversion from the class type Object to any other reference type (§4.12.4).
  •    From any class type C to any non-parameterized interface type K, provided that C is not final and does not implement K.
  •    From any interface type J to any non-parameterized class type C that is not final.
  •    From any interface type J to any non-parameterized interface type K, provided that J is not a subinterface of K.
  •    From the interface types Cloneable and java.io.Serializable to any array type T[].
  •    From any array type SC[] to any array type TC[], provided that SC and TC are reference types and there is a narrowing reference conversion from SC to TC.
  • Note that the 2nd allows you to cast any class to an interface which it does not implement. That seems to be the case which the current cast falls into. Obviously the compiler must permit those casts, even if they are going to throw class cast exceptions at runtime. It appears you can declare casts between all sorts of interface types which do not appear to be subtypes of one another. It is probably to allow things likeA Queue might be a List as well. It might and it might not, so the compiler is programmed secure in the knowledge that programmers know what they are doing and will only declare casts which will actually work. In the case of casting to an interface, it always gives the programmer the benefit of the doubt, because class cast exceptions are so much more fun. That is what the rest of that JLS section which I didn't quote says.
     
    Stevens Miller
    Bartender
    Posts: 1464
    32
    Netbeans IDE C++ Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Doesn't my explanation make you want garlic too?


    Actually, everything makes me want garlic. I love garlic. (Praise Ghu, so does my wife.)

    ...the compiler is programmed secure in the knowledge that programmers know what they are doing and will only declare casts which will actually work. In the case of casting to an interface, it always gives the programmer the benefit of the doubt, because class cast exceptions are so much more fun.


    For those lacking in the necessary self-confidence, Horstmann suggests this idiom in "Core Java:"
     
    Campbell Ritchie
    Marshal
    Posts: 79239
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Of course, you sh‍ould apply garlic to any casts. Once you start casting, particularly reference types, you shou‍ld consider whether there is something wrong with your design. You shou‍ld start looking for silver bullets and wooden stakes if you are tempted to use the instanceof operator
     
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jeanne Boyarsky wrote:

    Stevens Miller wrote:It is more important to know about the JLS than it is to read the JLS.


    Agreed. Books are useful as the authors have digested this information and turned into a more readable form.

    Yes JLS is like constitution of Java, to understand it you need to know it else will spend hours perusing it resulting confusion. As Jeanne mentioned better read books first as author present them in simple readable langauge with examples.
     
    prateek shaw
    Ranch Hand
    Posts: 53
    1
    Oracle Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank everyone, Yes i know it is tough enough to digest all JLS .


    So for i understood below point.

    1-if class A extends B ,


    Yes above will not throw compile time error however run time.

    2-if class A and class b are not related at all then

    Even above code does not compile at all. compiler is smart enough to identify that there is no relation between these classes so let me throw error while compiling.

    3-if class A does not implements interface C

    This will compile. but throw error at run time.

    4- but one things if class A is final and does not implements interface C


    this will not compile.


    These are the some point , but there are still a lot to learn and remember.
    Thank everyone for their help.


     
    Campbell Ritchie
    Marshal
    Posts: 79239
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    prateek shaw wrote:. . . if class A extends B,Yes above will not throw compile time error however run time. . . .

    That doesn't make sense; as far as I can see that code will run without exceptions. Have you tried those bits of code. Try them all and see what happens. If you get an exception and only part of your code executes, you shou‍ld: comment out the code by starting the line with // or wrap it in if (false) {...}, and recompile, so you can see the rest of the code running.

    3-if class A does not implements interface C


    This will compile. but throw error at run time.

    Correct. As we have seen from the previous examples, the JLS allows a cast to an interface type, but you will suffer an exception (not an error) at runtime, more specifically this sort of exception.

    4- but one things if class A is final and does not implements interface Cthis will not compile.

    Can't remember. Does it say anything about non‑final in the JLS quote I posted earlier? Did you try that sort of code?These are the some point , but there are still a lot to learn and remember.You might need to know about which casts will compile and which won't for a cert exam, but I am not sure; I have never sat any cert exams. The most important thing to remember about casts is that they need garlic. Look on them as vampires in your code. Whenever you see yourself casting anything, think you have done something wrong and you shou‍ld redesign your code so the cast is not necessary. If you see instanceof too, become even more suspicious.

    Thank everyone for their help.

    That's a pleasure
    reply
      Bookmark Topic Watch Topic
    • New Topic