instanceOf operator new bie question  
Author Message
dbaplusplus





PostPosted: 2006-4-28 1:52:00 Top

java-programmer, instanceOf operator new bie question I am using Java 1.4.2.x

I wrote following code:

NumberFormat df;
df = NumberFormat.getInstance();
if (df instanceof DecimalFormat) {
((DecimalFormat) df).setMinimumIntegerDigits(1);
((DecimalFormat) df).setDecimalSeparatorAlwaysShown(true);
}

What does "df instanceof DecimalFormat" really mean? Does it mean that
NumberFormat can be converted to DecimalFormat? Because my
NumberFormat.getInstance() is returning me a NumberFormat and not
DecimalFormat what else could it mean.

I did read about instanceOf opeartor on google, but it is still
confusing to me.


Thanks a lot.
Prem

 
Danno





PostPosted: 2006-4-28 2:19:00 Top

java-programmer >> instanceOf operator new bie question Well, in class inheritance if one class is on the same family tree or
connected in someway to another Object or Interface, then that one
class is an instance of that relative.

In other words, if I have this class structure.....

Object
^
Fruit
________|___________
^ ^
Apple Citrus implements Squeezable
^
Orange

and lets say that I create an orange object. That means that instance
of will return true for Orange, Citrus, Squeezable, Fruit, and Object

Orange o = new Orange;
System.out.println (o instanceof Orange); //true
System.out.println (o instanceof Citrus); //true
System.out.println (o instanceof Squeezable); //true
System.out.println (o instanceof Fruit); //true
System.out.println (o instanceof Object); //true

This would be false:
System.out.println(o instanceof Apple); //false

Because that's like comparing apples to oranges...pun intended.

 
dbaplusplus





PostPosted: 2006-4-28 3:07:00 Top

java-programmer >> instanceOf operator new bie question Thanks. But, it still have question:
If I wrote code like:
Fruit f = new Fruit;
Syetem.out.println(f instanceof Orange); // will that be true.

In case of DecimalFormat, it extends NumberFormat and not the other way
around.

java.text
Class DecimalFormat
java.lang.Object
java.text.Format
java.text.NumberFormat
java.text.DecimalFormat

 
 
Arvind





PostPosted: 2006-4-28 3:19:00 Top

java-programmer >> instanceOf operator new bie question
email***@***.com wrote:
> Thanks. But, it still have question:
> If I wrote code like:
> Fruit f = new Fruit;
> Syetem.out.println(f instanceof Orange); // will that be true.

Does not take long to write the snippet of code would it ? - but with
your next question the answer to this one is significant....the answer
will be false.

>
> In case of DecimalFormat, it extends NumberFormat and not the other way
> around.
>
> java.text
> Class DecimalFormat
> java.lang.Object
> java.text.Format
> java.text.NumberFormat
> java.text.DecimalFormat

Yes, DecimalForm is a concrete subclass of NumberFormat
(http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html)

If you read the description carefull, the getInstance() factory method
can return *any* subclass of NumberFormat - which means, when u call
the getInstance method, you can be returned a DecimalFormat, or any
other subclass.....hence the need to check whether you are really
dealing with a DecimalFormat class...

to explain it with analogy given above...

let us say, i told you, you can eat a fruit, smell a fruit, squeeze a
fruit...for all these operations on the fruit, you would not need to
know, what fruit it is - unless ofcourse, you end up trying to squeeze
a jack fruit hurting your hands ;)

But, operation like peel the fruit - would not be applicable for
strawberry, apples - but applicable for oranges - in which case, you
would need to know, whether the fruit at hand, is really an apple or an
orange before you can 'peel' it.....

hence, the use of instanceOf....

 
 
Danno





PostPosted: 2006-4-28 3:49:00 Top

java-programmer >> instanceOf operator new bie question Just to add, you can use

DecimalFormat df = new DecimalFormat();
or
DecimalFormat df = new DecimalFormat(pattern);

If you know you want DecimalFormat straight up.

 
 
dbaplusplus





PostPosted: 2006-4-28 4:05:00 Top

java-programmer >> instanceOf operator new bie question I know I can use that, but some Java Documentation says. you should
not create an instance of DecimalFormat directly.

 
 
Danno





PostPosted: 2006-4-28 4:22:00 Top

java-programmer >> instanceOf operator new bie question Yeah, it is a weird sentence too. I never quite understood the meaning
of it....

"In general, do not call the DecimalFormat constructors directly, since
the NumberFormat factory methods may return subclasses other than
DecimalFormat. "

I think they need some better documentation on this because calling
NumberFormat factory methods has nothing to do with calling
DecimalFormat constructors directly.

 
 
Oliver Wong





PostPosted: 2006-4-29 1:04:00 Top

java-programmer >> instanceOf operator new bie question
"Danno" <email***@***.com> wrote in message
news:email***@***.com...
> Yeah, it is a weird sentence too. I never quite understood the meaning
> of it....
>
> "In general, do not call the DecimalFormat constructors directly, since
> the NumberFormat factory methods may return subclasses other than
> DecimalFormat. "
>
> I think they need some better documentation on this because calling
> NumberFormat factory methods has nothing to do with calling
> DecimalFormat constructors directly.
>

What this is saying is that the "DecimalFormat" class may itself have
secret subclasses that you don't know about. E.g:
DecimalFormatSecretSubclassThatSpecializesInValuesBetweenZeroAndOne, or
DecimalFormatSecretSubclassThatSpecializesInValuesBeyond4294967296. [*]

So don't use the DecimalFormat constructor directly. Instead, use the
factory methods of NumberFormat, which will decide the best subclass to use
for the given situation and use that one.

[*] As of Java 1.5.0_06, DecimalFormat does not have any secret subclasses,
but Sun may change that in the future.

- Oliver

 
 
Mike Schilling





PostPosted: 2006-4-29 5:02:00 Top

java-programmer >> instanceOf operator new bie question
"Oliver Wong" <email***@***.com> wrote in message
news:GVr4g.196$zn1.164@edtnps90...
>
> [*] As of Java 1.5.0_06, DecimalFormat does not have any secret
> subclasses,

or if it does, they're really, really secret.


 
 
Oliver Wong





PostPosted: 2006-5-1 23:06:00 Top

java-programmer >> instanceOf operator new bie question
"Mike Schilling" <email***@***.com> wrote in message
news:5pv4g.63594$email***@***.com...
>
> "Oliver Wong" <email***@***.com> wrote in message
> news:GVr4g.196$zn1.164@edtnps90...
>>
>> [*] As of Java 1.5.0_06, DecimalFormat does not have any secret
>> subclasses,
>
> or if it does, they're really, really secret.

Right ;) I was basing my claim above by opening the JARs that Sun
bundles with the JRE, and using Eclipse to build the class hierarchy
diagram, and Eclipse says DecimalFormat has no subclasses. I also took a
peek at the getInstance() source code, and it seems to always return
DecimalFormat for now.

- Oliver

 
 
Timo Stamm





PostPosted: 2006-5-1 23:33:00 Top

java-programmer >> instanceOf operator new bie question Oliver Wong schrieb:
>
> "Mike Schilling" <email***@***.com> wrote in message
> news:5pv4g.63594$email***@***.com...
>>
>> "Oliver Wong" <email***@***.com> wrote in message
>> news:GVr4g.196$zn1.164@edtnps90...
>>>
>>> [*] As of Java 1.5.0_06, DecimalFormat does not have any secret
>>> subclasses,
>>
>> or if it does, they're really, really secret.
>
> Right ;) I was basing my claim above by opening the JARs that Sun
> bundles with the JRE, and using Eclipse to build the class hierarchy
> diagram, and Eclipse says DecimalFormat has no subclasses. I also took a
> peek at the getInstance() source code, and it seems to always return
> DecimalFormat for now.

You can find subclasses using the java docs. Just click on "Use" to find
all dependencies, including subsclasses. Example:
http://java.sun.com/j2se/1.5.0/docs/api/java/text/class-use/NumberFormat.html


Timo
 
 
Oliver Wong





PostPosted: 2006-5-2 0:13:00 Top

java-programmer >> instanceOf operator new bie question
"Timo Stamm" <email***@***.com> wrote in message
news:44562a44$0$4499$email***@***.com...
> Oliver Wong schrieb:
>>
>> "Mike Schilling" <email***@***.com> wrote in message
>> news:5pv4g.63594$email***@***.com...
>>>
>>> "Oliver Wong" <email***@***.com> wrote in message
>>> news:GVr4g.196$zn1.164@edtnps90...
>>>>
>>>> [*] As of Java 1.5.0_06, DecimalFormat does not have any secret
>>>> subclasses,
>>>
>>> or if it does, they're really, really secret.
>>
>> Right ;) I was basing my claim above by opening the JARs that Sun
>> bundles with the JRE, and using Eclipse to build the class hierarchy
>> diagram, and Eclipse says DecimalFormat has no subclasses. I also took a
>> peek at the getInstance() source code, and it seems to always return
>> DecimalFormat for now.
>
> You can find subclasses using the java docs. Just click on "Use" to find
> all dependencies, including subsclasses. Example:
> http://java.sun.com/j2se/1.5.0/docs/api/java/text/class-use/NumberFormat.html

That would only show the publicly visible subclasses though.

- Oliver

 
 
Mike Schilling





PostPosted: 2006-5-2 0:24:00 Top

java-programmer >> instanceOf operator new bie question
"Oliver Wong" <email***@***.com> wrote in message
news:Bsq5g.2722$zn1.2089@edtnps90...
>
> "Timo Stamm" <email***@***.com> wrote in message
> news:44562a44$0$4499$email***@***.com...
>> Oliver Wong schrieb:
>>>
>>> "Mike Schilling" <email***@***.com> wrote in message
>>> news:5pv4g.63594$email***@***.com...
>>>>
>>>> "Oliver Wong" <email***@***.com> wrote in message
>>>> news:GVr4g.196$zn1.164@edtnps90...
>>>>>
>>>>> [*] As of Java 1.5.0_06, DecimalFormat does not have any secret
>>>>> subclasses,
>>>>
>>>> or if it does, they're really, really secret.
>>>
>>> Right ;) I was basing my claim above by opening the JARs that Sun
>>> bundles with the JRE, and using Eclipse to build the class hierarchy
>>> diagram, and Eclipse says DecimalFormat has no subclasses. I also took a
>>> peek at the getInstance() source code, and it seems to always return
>>> DecimalFormat for now.
>>
>> You can find subclasses using the java docs. Just click on "Use" to find
>> all dependencies, including subsclasses. Example:
>> http://java.sun.com/j2se/1.5.0/docs/api/java/text/class-use/NumberFormat.html
>
> That would only show the publicly visible subclasses though.

In fact, only *documented* publicly visible subclasses, which coulf be a
smaller set still.


 
 
Timo Stamm





PostPosted: 2006-5-2 0:54:00 Top

java-programmer >> instanceOf operator new bie question Mike Schilling schrieb:
> "Oliver Wong" <email***@***.com> wrote in message
> news:Bsq5g.2722$zn1.2089@edtnps90...
>> "Timo Stamm" <email***@***.com> wrote in message
>>> You can find subclasses using the java docs. Just click on "Use" to find
>>> all dependencies, including subsclasses. Example:
>>> http://java.sun.com/j2se/1.5.0/docs/api/java/text/class-use/NumberFormat.html
>> That would only show the publicly visible subclasses though.

Sorry, I didn't read the thread and missed the point.


> In fact, only *documented* publicly visible subclasses, which coulf be a
> smaller set still.

Yes, it only works with completely documented APIs like the JSE. Don't
try it on my code :)


Timo
 
 
Chris Uppal





PostPosted: 2006-5-2 18:12:00 Top

java-programmer >> instanceOf operator new bie question Oliver Wong wrote:

> > > [*] As of Java 1.5.0_06, DecimalFormat does not have any secret
> > > subclasses,
> >
> > or if it does, they're really, really secret.
>
> Right ;) I was basing my claim above by opening the JARs that Sun
> bundles with the JRE,

Not a safe guide in general -- the platform makes use of bytecode generation in
a couple of places already[*], and who knows how far that might go...

-- chris

[*] java.lang.reflect.InvocationHandler and in the implementation of reflective
access, and possibly others I don't know about.