Author Topic:   Object Conversion Rules!Pls help me!
Nirmala
ranch hand
posted July 18, 2000 08:22 PM         
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
posted July 19, 2000 05:44 AM         
Help Please .............

Nirmala

Ajith Kallambella
bartender
posted July 19, 2000 06:51 AM             
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.

  • When you convert array object references, the object themselves must be convertible to one another.
    Consider the following scenario
    code:

    class Fruit{}
    class Apple extends Fruit{}
    class RedApple extends Apple{}

    class Grape extends Fruit{}

    Fruit fruits[];
    Apple apples[];
    RedApple redapples[];
    Grape grapes[];

    // code to initialize and fill up arrays

    // Since Apple can be converted to Fruit,
    // the following is valid
    fruits = apples ;

    // Since RedApple can be converted to Apple or Fruit,
    // the following are valid
    fruits = redapples;
    apples = redapples ;

    // Since Grape can be converted to Fruit,
    // the following is valid
    fruits = grapes ;


    // Since Grape CANNOT be converted to Apple or RedApple,
    // the following are not valid
    apples = grapes ;
    redapples = grapes ;



Hope this helps.

Ajith

[This message has been edited by Ajith Kallambella (edited July 19, 2000).]

Nirmala
ranch hand
posted July 19, 2000 11:16 AM         
Thank you so much Ajith. It is clear now.

Thanks again.

Nirmala

Deepak M
ranch hand
posted July 19, 2000 05:58 PM             
quote:
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
    posted July 19, 2000 07:25 PM         
    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
    bartender
    posted July 19, 2000 07:37 PM             
    Deepak,

    You have raised a very valid point. Infact, if you read my statement correctly, the analogy I have given

    quote:

    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).]

    maha anna
    bartender
    posted July 19, 2000 07:45 PM             
    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.



    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.
    }
    }


    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



    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.

    }

    }

    [This message has been edited by maha anna (edited July 19, 2000).]

    Nirmala
    ranch hand
    posted July 19, 2000 08:30 PM         
    Maha Anna,
    Thanks a lot for explanation and it is really helped me to understand it better.

    Thank you all.

    Nirmala

    |