aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object Conversion Rules!Pls help me! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object Conversion Rules!Pls help me!" Watch "Object Conversion Rules!Pls help me!" New topic
Author

Object Conversion Rules!Pls help me!

Nirmala
Ranch Hand

Joined: Jun 28, 2000
Posts: 93
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

Joined: Jun 28, 2000
Posts: 93
Help Please .............
Nirmala
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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).]


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Nirmala
Ranch Hand

Joined: Jun 28, 2000
Posts: 93
Thank you so much Ajith. It is clear now.
Thanks again.
Nirmala
Deepak M
Ranch Hand

Joined: Jul 10, 2000
Posts: 124
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

    Joined: Jun 28, 2000
    Posts: 93
    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

    Joined: Mar 17, 2000
    Posts: 5782
    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).]
    maha anna
    Ranch Hand

    Joined: Jan 31, 2000
    Posts: 1467
    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

    Joined: Jun 28, 2000
    Posts: 93
    Maha Anna,
    Thanks a lot for explanation and it is really helped me to understand it better.
    Thank you all.
    Nirmala
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Object Conversion Rules!Pls help me!