aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "questions" Watch "questions" New topic
Author

questions

Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
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


nss
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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).]


Dave
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
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

Joined: Oct 11, 2001
Posts: 204
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
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

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


SCJP2. Please Indent your code using UBB Code
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
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

Joined: Oct 11, 2001
Posts: 204
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
Hussain
Ranch Hand

Joined: Nov 22, 2001
Posts: 110
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...


Muhammad Hussain<br />Sun Certified Java Programer (SCJP2)<br />Sun Certified Web Component Devloper SCWCD
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
thanx muhammad,
And all the very best for your exams.
I am giving too on 17th of this month.
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
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

Joined: Oct 11, 2001
Posts: 204
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

Joined: Jul 03, 2001
Posts: 2120
"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

Joined: Oct 11, 2001
Posts: 204
thanx Jose
Neha
Mathew Kuruvilla
Ranch Hand

Joined: Nov 27, 2001
Posts: 135
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

Joined: Nov 27, 2001
Posts: 135
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: questions
 
Similar Threads
Casting of Arrays and Object
storage
How to pass Arraylist to the JSP from the Action Class
question from kathy/bates book
Array of Objects