Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

instanceOf operator new bie question

Thread view: 
dbaplusplus@hotmail.com - 27 Apr 2006 18:52 GMT
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 - 27 Apr 2006 19:18 GMT
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@hotmail.com - 27 Apr 2006 20:06 GMT
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 - 27 Apr 2006 20:19 GMT
> 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.
[quoted text clipped - 5 lines]
>       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....

--
Arvind
Danno - 27 Apr 2006 20:48 GMT
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@hotmail.com - 27 Apr 2006 21:05 GMT
I know  I can use that, but some Java Documentation  says. you should
not create an instance of DecimalFormat directly.
Danno - 27 Apr 2006 21:22 GMT
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 - 28 Apr 2006 18:03 GMT
> Yeah, it is a weird sentence too. I never quite understood the meaning
> of it....
[quoted text clipped - 6 lines]
> 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 - 28 Apr 2006 22:01 GMT
> [*] 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 - 01 May 2006 16:06 GMT
>> [*] 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 - 01 May 2006 16:33 GMT
Oliver Wong schrieb:

>>> [*] As of Java 1.5.0_06, DecimalFormat does not have any secret
>>> subclasses,
[quoted text clipped - 6 lines]
> 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 - 01 May 2006 17:13 GMT
> Oliver Wong schrieb:
>>
[quoted text clipped - 12 lines]
> 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 - 01 May 2006 17:23 GMT
>> Oliver Wong schrieb:
>>>
[quoted text clipped - 14 lines]
>
>    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 - 01 May 2006 17:54 GMT
Mike Schilling schrieb:
>> "Timo Stamm" <timo.stamm@arcor.de> 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 - 02 May 2006 11:12 GMT
> > > [*] As of Java 1.5.0_06, DecimalFormat does not have any secret
> > > subclasses,
[quoted text clipped - 3 lines]
>     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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.