instanceOf operator new bie question

D

dbaplusplus

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
 
D

Danno

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.
 
D

dbaplusplus

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
 
A

Arvind

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....
 
D

Danno

Just to add, you can use

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

If you know you want DecimalFormat straight up.
 
D

dbaplusplus

I know I can use that, but some Java Documentation says. you should
not create an instance of DecimalFormat directly.
 
D

Danno

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.
 
O

Oliver Wong

Danno said:
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
 
O

Oliver Wong

Mike Schilling said:
Oliver Wong said:
[*] 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
 
T

Timo Stamm

Oliver said:
Mike Schilling said:
Oliver Wong said:
[*] 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
 
O

Oliver Wong

Timo Stamm said:
Oliver said:
Mike Schilling said:
[*] 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
 
M

Mike Schilling

Oliver Wong said:
Timo Stamm said:
Oliver said:
[*] 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.
 
T

Timo Stamm

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
 
C

Chris Uppal

Oliver said:
[*] 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top