Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Forbidden Conversions

 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This is an abstract from JLS:

5.1.7 Forbidden Conversions
  • There is no permitted conversion from any reference type to any primitive type.
  • Except for the string conversions, there is no permitted conversion from any primitive type to any reference type.
  • There is no permitted conversion from the null type to any primitive type.
  • There is no permitted conversion to the null type other than the identity conversion.
  • There is no permitted conversion to the type boolean other than the identity conversion.
  • There is no permitted conversion from the type boolean other than the identity conversion and string conversion.
  • There is no permitted conversion other than string conversion from class type S to a different class type T if S is not a subclass of T and T is not a subclass of S.
  • There is no permitted conversion from class type S to interface type K if S is final and does not implement K.
  • There is no permitted conversion from class type S to any array type if S is not Object.
  • There is no permitted conversion other than string conversion from interface type J to class type T if T is final and does not implement J.
  • There is no permitted conversion from interface type J to interface type K if J and K declare methods with the same signature but different return types.
  • There is no permitted conversion from any array type to any class type other than Object or String.
  • There is no permitted conversion from any array type to any interface type, except to the interface type Cloneable, which is implemented by all arrays.
  • There is no permitted conversion from array type SC[] to array type TC[] if there is no permitted conversion other than a string conversion from SC to TC.



  • I dont understand the points in bold.
    also i am not able to practically implement:
    "conversion from any array type to Object or String. "

    Please Help
    Thanx

    Sandy
     
    Barry Gaunt
    Ranch Hand
    Posts: 7729
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Which JLS is this? The second or third edition?
     
    Barry Gaunt
    Ranch Hand
    Posts: 7729
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The third edition says in section 5.1.12 Forbidden Conversions:
    Any conversion not explicity allowed is forbidden.


    That's it !

    So it looks as if you have JLS 2nd edition. In that "boxing" did not exist in Java, so you could not convert between primitives and their wrapper classes. In Java 5.0 you can. For instance an int can be converted to an Integer, and vice versa. Or a boolean can be converted to a Boolean or vice versa.
    [ September 16, 2005: Message edited by: Barry Gaunt ]
     
    Barry Gaunt
    Ranch Hand
    Posts: 7729
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This code:


    Shows an array being converted to an Object (in method m1) and to a String (in the concatenation operation.)

    Compiled with Java 5.0.
     
    Sandeep Chhabra
    Ranch Hand
    Posts: 340
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanx Barry.

    Got your point.

    Sandy
     
    Mark Spritzler
    ranger
    Sheriff
    Posts: 17278
    6
    IntelliJ IDE Mac Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It is forbidden, like the passion between two people from different sides of the train tracks.

    It sounds so tropical, Forbibben Conversions. I think my wife has that steamy romance novel with the same title.

    Mark
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic