• 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

Object Conversion Rules!Pls help me!

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Please explain me with an example.
OldType New Type
--------- -----------
Interface Interface(SuperInterface)
Class Class,Old class must be Super Class
Class Interface,old class must implement
or Array, it is given as 'Only Array of object references types may be converted to an array,and then the old element type must be convertible to the new type.
Please explain this array Conversion.
Thanks in advance.
Nirmala
 
Nirmala
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Help Please .............
Nirmala
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nirmala,
Only Array of object references types may be converted to an array,and then the old element type must be convertible to the new type
This means the following

  • You cannot convert array of primitive types. ie., even though an int can be
    assigned to a long, an array of ints cannot be assigned to an array of longs. You can
    only convert arrays which contain objects.
  • [list] When you convert array object references, the object themselves must be convertible to one another.
    Consider the following scenario

    [/list]
    Hope this helps.
    Ajith
    [This message has been edited by Ajith Kallambella (edited July 19, 2000).]
 
Nirmala
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much Ajith. It is clear now.
Thanks again.
Nirmala
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ajith Kallambella:
Nirmala,

  • You cannot convert array of primitive types. ie., even though an int can be
    assigned to a long, an array of ints cannot be assigned to an array of longs. You can
    only convert arrays which contain objects.


  • I don't agree with your statement completely.
    You CAN convert an array of primitive type. Its called identity converstion and though this may seem trivial, it has two practical consequences.
    First, it is always permitted for an expression to have the desired type to begin with, thus allowing the simply stated rule that every expression is subject to conversion, if only a trivial identity conversion. Second, it implies that it is permitted for a program to include redundant cast operators for the sake of clarity.
    The only forbidden conversion for array is :
    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.
    Which means if SC and TC are primitive types, then it has to be the same types.
    i.e
    int [] can be converted only to int [] ( Identity Conversion )
    But if SC and TC are reference types then
    TC[] can be converted to SC[] if TC can be converted to SC !
     
    Nirmala
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Deepak,
    Why this following program gives an error,please look at this:

    public class TestConArray
    {
    public static void main(String args[])
    {
    int [] a = new int[20];
    TestConArray a1=new TestConArray();
    a1.array(a);
    }
    public void array(long b[])
    {
    System.out.println("Test Array Conversion");
    }
    }


    Here is the compiler error:
    TestConArray.java:8 Incompatible type for method. can't convert int[] to long[].
    a1.array(a);
    By the by what is TC/SC?.
    Thanks
    Nirmala
     
    Ajith Kallambella
    Sheriff
    Posts: 5782
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Deepak,
    You have raised a very valid point. Infact, if you read my statement correctly, the analogy I have given


    even though an int can be
    assigned to a long, an array of ints cannot be assigned to an array of longs


    should hint you that I am not talking about identity conversions.
    Thanks for clarifying though...

    Ajith
    [This message has been edited by Ajith Kallambella (edited July 19, 2000).]
     
    Ranch Hand
    Posts: 1467
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Nirmala,
    we all know what primitive arrays are. They are arrays of primitives in java. For example int[], char[], long[], short[]... like that. WE also know that we can assign shorter type var to a wider type var. For example int i=10; long l = i; is valid in Java. But this does not mean , we can do the following. The follwing assignment is NOT valid in java.

    int[] arr1={1,2};
    long[] arr2 = {2,3};
    arr2 = arr1; //This is NOT ok.

    But we can assign an int[] var to ANOTHER int[] var and long[] var to another long[] var, short[] var to another short[] var. This is ok in Java. For example the foll. code is legal.

    int[] arr1={1,2};
    int[] arr2 = {2,3};
    arr2 = arr1; //This is ok.


    At the same time, for object reference arrays like Base[] , SubClass[], if the type 'Subclass' is a subclass of the type 'Base' then we CAN DO the following.
    <pre>

    class Base {}
    class SubClass extends Base{}
    class Test {
    void mtd1() {
    Base[] arr1= null;
    SubClass[] arr2 = null;
    arr1 = arr2; //This is ok as OPPOSED in primitive arrays
    // is Subclass is CONVERTIBLE to Base class.
    }
    }

    </pre>

    I tried to compose a sample program which also explains the concept I just explained.
    PLease go through and come back if you have anything to ask. In Deepak's post the SC and TC just means 'SourceClassName' and 'TargetClassName' as we have here the names as 'Base' and 'Subclass'.
    regds
    maha anna
    <pre>

    class Test {
    static void method1(long[] intArray) {
    }
    public static void main(String[] args) {
    int[] array1 = {1,2,3,4};
    long[] array2 = {10,20,30,40};
    int[] array3;
    long[] array4;
    array2 = array1; //This is NOT ok.
    array3 = array1; //This is ok
    array4 = array2; //This is ok

    int var1 =10;
    long var2=20;
    var2 = var1; //This is ok.

    method1(array1); //This is NOT ok
    method1(array2); //This is ok.
    }
    }

    </pre>

    [This message has been edited by maha anna (edited July 19, 2000).]
     
    Nirmala
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Maha Anna,
    Thanks a lot for explanation and it is really helped me to understand it better.
    Thank you all.
    Nirmala
     
    What? What, what, what? What what 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