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]
# 1
The statement is OK, so it must be where you placed it. Post a minimal, complete program demonstrating your error.
DrLaszloJamfa at 2007-7-8 22:29:34 > top of Java-index,Java Essentials,Java Programming...
# 2
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.
cotton.ma at 2007-7-8 22:29:34 > top of Java-index,Java Essentials,Java Programming...
# 3

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

cdskinna at 2007-7-8 22:29:34 > top of Java-index,Java Essentials,Java Programming...
# 4
It appears your error is actually in the part of your code you did not show here.
cotton.ma at 2007-7-8 22:29:34 > top of Java-index,Java Essentials,Java Programming...
# 5

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

SomeoneElsea at 2007-7-8 22:29:34 > top of Java-index,Java Essentials,Java Programming...
# 6
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
cdskinna at 2007-7-8 22:29:34 > top of Java-index,Java Essentials,Java Programming...
# 7
> 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.
cotton.ma at 2007-7-8 22:29:34 > top of Java-index,Java Essentials,Java Programming...
# 8
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
SomeoneElsea at 2007-7-8 22:29:34 > top of Java-index,Java Essentials,Java Programming...
# 9

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

abillconsla at 2007-7-8 22:29:34 > top of Java-index,Java Essentials,Java Programming...
# 10

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)

{}

doremifasollatidoa at 2007-7-8 22:29:34 > top of Java-index,Java Essentials,Java Programming...
# 11
> GenericyIs that a word?
floundera at 2007-7-8 22:29:34 > top of Java-index,Java Essentials,Java Programming...
# 12
> > Genericy> > Is that a word?Yes.
cotton.ma at 2007-7-8 22:29:34 > top of Java-index,Java Essentials,Java Programming...
# 13

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);

Caffeine0001a at 2007-7-8 22:29:34 > top of Java-index,Java Essentials,Java Programming...
# 14

> 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

jverda at 2007-7-8 22:29:34 > top of Java-index,Java Essentials,Java Programming...
# 15
Ah yes.Genericy: (noun) the illegal or unauthorised act of copying generals.
floundera at 2007-7-21 16:39:44 > top of Java-index,Java Essentials,Java Programming...
# 16
> 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
CaptainMorgan08a at 2007-7-21 16:39:44 > top of Java-index,Java Essentials,Java Programming...
# 17
So your surname is Morgan. Hence the user name. DUH! ;)
floundera at 2007-7-21 16:39:44 > top of Java-index,Java Essentials,Java Programming...
# 18

> 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?

DrLaszloJamfa at 2007-7-21 16:39:44 > top of Java-index,Java Essentials,Java Programming...
# 19
> 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
cotton.ma at 2007-7-21 16:39:44 > top of Java-index,Java Essentials,Java Programming...
# 20
> 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.
cotton.ma at 2007-7-21 16:39:44 > top of Java-index,Java Essentials,Java Programming...
# 21

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

Caffeine0001a at 2007-7-21 16:39:44 > top of Java-index,Java Essentials,Java Programming...
# 22
> 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.
floundera at 2007-7-21 16:39:44 > top of Java-index,Java Essentials,Java Programming...
# 23
> So your surname is Morgan. Hence the user name. DUH!> ;)And you're... a fish? ;-)
CaptainMorgan08a at 2007-7-21 16:39:44 > top of Java-index,Java Essentials,Java Programming...
# 24
Yep! So kiss my shiny scaly arse! ;)
floundera at 2007-7-21 16:39:44 > top of Java-index,Java Essentials,Java Programming...
# 25
You aren't a Stingray, by chance? Those things can be dangerous. ;-)
CaptainMorgan08a at 2007-7-21 16:39:44 > top of Java-index,Java Essentials,Java Programming...
# 26

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.

CaptainMorgan08a at 2007-7-21 16:39:44 > top of Java-index,Java Essentials,Java Programming...
# 27
ROFL!How are you going to provide references to reliable sources?
floundera at 2007-7-21 16:39:44 > top of Java-index,Java Essentials,Java Programming...
# 28
> 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.
CaptainMorgan08a at 2007-7-21 16:39:44 > top of Java-index,Java Essentials,Java Programming...
# 29

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

abillconsla at 2007-7-21 16:39:44 > top of Java-index,Java Essentials,Java Programming...