• 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

questions

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

1)
int a[]=new int a[];
Object o=a; //compiles
Object o[]=a //does not compile.
2)Could anyone tell me how
String compareTo(Object String) works.
Regards
Neha

 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neha
In your first question there are a couple of things going on here:
The first line:
Object o=a;
compiles because Object is the direct superclass of all Array types, see the JLS section 10.8.
The second line:
Object o[]=a
doesn't compile because in order to do a widening conversion from one array type to another they both have to be reference types, and the int array a isn't. This is in the JLS Section 5.1.4
For your second question the String method compareTo(Object) will only work if the argument passed to it is, in fact, a String. then it works just like the compareTo(String) method. In that case it returns a number or 0, less than 0,or greater than 0 depending on whether or not the string comes before or after the argument string. Basically it checks each character in the two Strings and when it finds a difference it determines if the unicode of the argument Strings character is before or after the character in the same position of the String the method was called on. If its unicode number is higher it returns a value of less than 0, if the value is lower then it returns a number greater than 0.
Try it out with some similiar Strings and see what results you get, that is the best way to see exactly what it is telling you about them.
hope that helped
------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Dave Vick (edited December 04, 2001).]
[This message has been edited by Dave Vick (edited December 04, 2001).]
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This line is still not very clear,could you give me some eg. of this type.
From any array type SC[] to any array type TC[], provided that SC and TC are reference types and there is a narrowing conversion from SC to TC
Regards
neha
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry it is
int compareTo(Object string)
if i put abcd.compareTo("sbcd"); ans -18
i know that when both strings equal ans 0
current string less than the string in parameter then ans <0<br /> " " greater " " " " " " " >0
but how it works i still do not know
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


From any array type SC[] to any array type TC[], provided that SC and TC are reference types and there is a narrowing conversion from SC to TC


Object[] , Integer[]
Object[] is SC[]
Integer[] is TC[]
There is a narrowing conversion from Object to Integer
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Neha Sawant:
Hi,
This line is still not very clear,could you give me some eg. of this type.
From any array type SC[] to any array type TC[], provided that SC and TC are reference types and there is a narrowing conversion from SC to TC
Regards
neha



Neha...
Couple of things you need to observe here.
1. The array declaration is wrong
int a[] = new int[10];//compiles
int []a = new int[] {1,0};//compiles
int a[] = new int a[] //wont compile
2. "provided that SC and TC are reference types and there is a narrowing conversion from SC to TC" This is the key point
No narrowing conversion is taking place between SC and TC
ie will Object o = 4; compile? of course no...
coz integer is atomic and they cant be assigned to object
Therefore the Object o[] = a; wont compile
But Object o = a; will compile becoz... JLS says "From any array type to type Object" ... simple reason being all arrays are objects in java and the supreme reference is Object

To do a sanity check try this:
Create an array of strings and do the same assignment you did for int arrays and tell me what you think?
Have Fun
Ragu


 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ragu,
I am getting the same answers.
String s[]=new String[]{"hello","Hi","How","ARE","YOU"};
String m="Amit";
Object l[]=m; //does not compile
Object o[]=s; // compiles
Object r=m; //compiles
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is ur answer as simple as a Java Cow
public class G93
{
public static void main(String [] args)
{
/*int i[]=new int[2];
Object a[]=i;*/
// it will not compile bcoz it is same as doing
this thing like this.

int i=2;
Object a=i;//which is uterly immmposible

//but this is possible
int []i=new int[3];
Object a[]={i};//now its safe to compile
}
}
EveryBody pray for me as i hava my SCJP2 paper ion 13 of this month..........
thankx in advance...
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx muhammad,
And all the very best for your exams.
I am giving too on 17th of this month.
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Neha Sawant:
Ragu,
I am getting the same answers.
String s[]=new String[]{"hello","Hi","How","ARE","YOU"};
String m="Amit";
Object l[]=m; //does not compile
Object o[]=s; // compiles
Object r=m; //compiles


From JLS:
Each string literal is a reference (�4.3) to an instance (�4.3.1, �12.5) of class String (�4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions (�15.28)-are "interned" so as to share unique instances, using the method String.intern.
So
Object o = m //compiles becoz 'm' is actually a reference to a
string instance
But
Object l[] = m; //it wont compile. Why? Becoz of type
incompatability

HIH
Ragu
Hope it helps
Ragu
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Ragu,
But i did not get this point.
int i=2;
Object o=i;// does not compile
but
int i[]={1,2,3}
Object o=i; //compiles
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"int i=2;
Object o=i;// does not compile"
you can not assign a primitive variable to a reference variable
"but
int i[]={1,2,3}
Object o=i; //compiles"
i is a variable of a reference type. It's pointing an aobject, that is an array of int. Any object can be assigned to a reference of type Object.
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx Jose
Neha
 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know where Khalid talks about this topic. I remember reading that the hierarchy goes as follows:<pre>
Object
|
-------------------------------------........
| | |
Object[] Boolean StringBuffer
|
------------------..........
| |
Boolean[] StringBuffer[]
</pre>
Note that primitive types do not fall anywhere in this hierarchy,
1) Hence it is a nono to assign primitive types to Object or any subclass of Object.
2) Nor is it permissible to assign Object[] and its subclasses to any of its sibling classes or their subclasses.
Unfortunately, I can't find out where I read this to verify this.
Mathew
[This message has been edited by Marilyn deQueiroz (edited December 09, 2001).]
 
Mathew Kuruvilla
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My diagram did not come up properly, so I'll use words. Object[] is a direct subclass of Object. Just like StringBuffer and Boolean. Object[] is also a sibling class of the StringBuffer and Boolean. StringBuffer[] and Boolean[] are SUBCLASSES of Object[].
The primitive types do not fall into this object hierarchy.
Therefore it is not smart to try to assign the arrays of objects to non array objects since they diverge in the object hierarchy immediately after the Object class.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic