• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stack object  RSS feed

 
Jim Longmore
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey all,

I'm trying to push a series of floats onto a Stack object. Problem is when I pop them off and cast them back to float the compiler gives me an unconvertible types error... I thought every object in Java was a subclass of Object? Or have I missed something?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you casting to float, or Float? Are you working with JDK 1.5, where the rules change a bit due to "autoboxing?" If you show us the pushing and popping code, we can probably tell you where you're going wrong.
 
Jim Longmore
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've since changed the code to use a double instead of a float.

I am using 1.5.0, what is this autoboxing?

This is the relevant part of the code I'm using. If you want me to post the entire code, I can do that too.



I get the error on the "sigma += Math.pow(((double)inList.pop() - mean),2);" line.

I also get an warning related to the push operation when I compile with the Xlint:unchecked flag.

Is it just me or does the JVM not like the Stack class?
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi -

What I think Ernest was implicitly asking:
"Are you using 'Float' (a Java class, and hence an object),
or 'float' (a Java primitive, and *not* an object)?"

If you've already got it working by using "double", then I guess the question is probably moot. Otherwise, you might wish to consider trying something like this:





As far as "auto-boxing":
In previous versions of Java, you had to manually interconvert between Java primitives (like "int") and their corresponding class wrappers (like "Integer"). Java 5 (JDK 1.5.x) simplifies your life with the new language feature "auto-boxing".

You can read more about it here:

http://www.javaworld.com/javaworld/jw-04-2004/jw-0426-tiger1.html

'Hope that helps .. PSM
[ July 26, 2005: Message edited by: Paul Santa Maria ]
 
Jim Longmore
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I see now. Slighlty confusing, but I get it

btw, I was using the primitives, not the classes, hence my problem.
[ July 27, 2005: Message edited by: Jim Longmore ]
 
Jim Longmore
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bah! Looks like the uni computers, the machines on which its being marked are running 1.4. Oh well, I know for now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!