Problem instatiating Integer
I'm usung the folowing imports:
java.util.*;
java.lang.*;
The following code won't compile for me:
code]
Integer numZero = new Integer (0);
[/code]
My IDE gives me this error:
"Type parameter 'Integer' cannot be instatiated directly"
If I try to compile I get the following error:
"unexpected type
found: type parameter Integer
required: class"
My JDK version is 1.5.0.04
I haven't seen this problem in the forums. Everything that I've read indicates that this is legal; What am I missing? I've been looking at this so long that my eyes are crossed.[
[648 byte] By [
cdskinna] at [2007-11-26 16:07:19]

The statement is OK, so it must be where you placed it. Post a minimal, complete program demonstrating your error.
unexpected typefound: type parameter Integerrequired: class"Hmmmm. I would suggest posting all your code. I think you have a syntax error somewhere that is throwing everything out-of-whack.
> unexpected type
> found: type parameter Integer
> required: class"
>
> Hmmmm. I would suggest posting all your code. I think
> you have a syntax error somewhere that is throwing
> everything out-of-whack.
Here' the code. The error occurs in the method buildEvents(). The 9th and 10th lines are giving me the error messages.
package D5;
import java.util.*;
import java.lang.*;
public class MessageBuilder
{
private int[] rzOne;
private int[] rzZero;
private static int numEvents;
private Map<Long, Integer> eventMap;
public MessageBuilder()
{
eventMap = new HashMap<Long, Integer>();
}
.....More methods...
public <Long, Integer> Map buildEvents(ArrayList <Integer> msgArray)
{
long nextEventTime;
nextEventTime = 1000;
int[] dataValue = {0, 0, 0};
Integer outvalue;
numEvents = 0;
Integer numZero = new Integer(0);
Integer numOne = new Integer(1);
for(Iterator<Integer> i= msgArray.iterator(); i.hasNext();)
{
outvalue = i.next();
if(outvalue == numZero)
{
dataValue = rzZero;
}
else if(outvalue == numOne)
{
dataValue = rzOne;
}
}
nextEventTime = makePulse(nextEventTime, dataValue);
return eventMap;
}// end buildEvents
Thanks for all of your help.
It appears your error is actually in the part of your code you did not show here.
THIS
public <Long, Integer> Map buildEvents(ArrayList <Integer> msgArray)
//SHOULD BE THIS:
public Map<Long, Integer> buildEvents(ArrayList <Integer> msgArray)
~Tim
EDIT: Also, you don't need to import the java.lang package.
Message was edited by:
SomeoneElse
Wow! Thanks guys.I NEVER would have guessed that a bad method declaration would mess up its internal variables. I didn't even look there.You're the best!Carol
> It appears your error is actually in the part of your> code you did not show here.It appears that I should look closer Genericy bits in future.
No problem, glad I could help. Interesting that you can even make that mistake and not have the compiler catch it. I assume due to Generics, it is a legal declaration.~Tim
> No problem, glad I could help.
>
> Interesting that you can even make that mistake and
> not have the compiler catch it. I assume due to
> Generics, it is a legal declaration.
>
> ~Tim
The little I know about Generics - as a matter of fact I was just reading yet another article on them when I read this thread - I can't understand why it did not flag it either. I still think Generics are a rats nest.
By the way, you should compare your Integer values with .equals.
if(outvalue == numZero)
should be:
if(outvalue.equals(numZero))
See the discussion of autoboxing here:
http://forum.java.sun.com/thread.jspa?threadID=5128016
As noted in that thread, if you compare an Integer against an int, it will unbox the Integer, and you will get the answer you expect. That is, you'd want this:
if(outvalue == 0)
{}
else if (outvalue == 1)
{}
> GenericyIs that a word?
> > Genericy> > Is that a word?Yes.
You might want also want to get into the practice of using
Integer numZero = Integer.valueOf(0);
Integer numOne = Integer.valueOf(1);
instead of
Integer numZero = new Integer(0);
Integer numOne = new Integer(1);
> No problem, glad I could help.
>
> Interesting that you can even make that mistake and
> not have the compiler catch it. I assume due to
> Generics, it is a legal declaration.
Correct. It marks the method as being parameterized by those two types.
More details here, I believe:
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
Ah yes.Genericy: (noun) the illegal or unauthorised act of copying generals.
> Genericy: (noun) the illegal or unauthorised> act of copying generals.[url= http://en.wikipedia.org/wiki/Genericy]Genericy[/url]Fixed it...Message was edited by: CaptainMorgan08
So your surname is Morgan. Hence the user name. DUH! ;)
> You might want also want to get into the practice of
> using
> > Integer numZero = Integer.valueOf(0);
> Integer numOne = Integer.valueOf(1);
>
> instead of
> > Integer numZero = new Integer(0);
> Integer numOne = new Integer(1);
>
Good point -- valueOf may cache Integer instances. But note that this method was introduced in version 1.5 -- is anyone still developing in < 1.5?
> So your surname is Morgan. Hence the user name. DUH!> ;)Once upon a time I think his name was in his profile. I think.Of related interest http://www.pcworld.com/article/id,128634-c,companynews/article.html
> Good point -- valueOf may cache Integer instances.> But note that this method was introduced in version> 1.5 -- is anyone still developing in < 1.5?Yes. All kinds. Some are less than 1.4.
> You might want also want to get into the practice of
> using
> > Integer numZero = Integer.valueOf(0);
> Integer numOne = Integer.valueOf(1);
>
Integer numZero = 0;
Integer numOne = 1;
But of course that is what autoboxing does automatically. That is why that method showed up in 1.5.
Sun's implementations (1.5 and 1.6) just checks if it between -128 and 127 and returns an integer from an array of Integers. But of course that is subject to change from version to version and from one VM to another.
> Of related interest> http://www.pcworld.com/article/id,128634-c,companynew> /article.htmlJust goes to show that Microsoft have more money than they know what to do with.
> So your surname is Morgan. Hence the user name. DUH!> ;)And you're... a fish? ;-)
Yep! So kiss my shiny scaly arse! ;)
You aren't a Stingray, by chance? Those things can be dangerous. ;-)
They deleted the article again and left me this message:
A tag has been placed on Genericy, requesting that it be speedily deleted from Wikipedia. This has been done under the criteria for speedy deletion, because it is a very short article providing little or no context to the reader. Please see Wikipedia:Stub for our minimum information standards for short articles. If you plan to add more material to the article, I advise you to do so immediately. Also please note that articles must be on notable subjects and should provide references to reliable sources which verify their content. Please do not remove the speedy deletion tag yourself. To contest the tagging and request that administrators wait a while for you to add contextual material, please affix the template {{hangon}} to the page and state your intention on the article's talk page. Feel free to leave a note on my talk page if you have any questions about this. Daniel J. Leivick 23:17, 24 January 2007 (UTC) on the User Talk page of the author.
ROFL!How are you going to provide references to reliable sources?
> ROFL!> > How are you going to provide references to reliable> sources?I was going to put in a link pointing to this thread. That's what I did with Stringify, but it (sadly) got deleted after a few weeks.
> > ROFL!
> >
> > How are you going to provide references to
> reliable
> > sources?
>
> I was going to put in a link pointing to this thread.
> That's what I did with Stringify, but it (sadly) got
> deleted after a few weeks.
OFCOL ... are you for real :P