Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Exploring the Java Collection Framework Maps

Pavle Delic
Pavle Delic
8,088 Points

Why "Integer" and not "int" in maps declaration?

Why Map<String, Integer>, instead of Map<String, int> ?

2 Answers

Brice Roberts
Brice Roberts
22,415 Points

int is a primitive data type. It only defines that the value is an integer in binary form. It has no inherent methods or fields that you can call. It is not an object and can not be used in an Object reference.

Integer is a class from java.lang.Number. It has many methods that you can call to perform additional actions on the field. It is a wrapper for the int type, which allows for the data type to be referenced by the Java compiler.

You could not use any methods or calls that deal with sorting the value in your map, because it would only be a binary value, and not a POJO.

Thought Experiment

Imagine a Fedex package.. (Think of Fedex as the Java Language)

You box up some knick knacks, and Fedex gives you a package label. The package could have anything in it, you know what is inside the package, but Fedex doesn't. So Fedex classifies your package by weight and size, and then gives it a tracking label. This tracking label comes with many forms of identification for Fedex to use to ship your package, without knowing what the package is. But, you both classify the box as a "package".

You call the recipient (for the purpose of this example, think of the recipient as the Java class and method you are trying to write) and say, "Have you received my knick knacks yet?" The recipient knew they were receiving knick knacks, so they say no.

So you call Fedex and say, "Where are my knick knacks". After a few moments of trying to figure it out, the representative then asks for some identifying information, because they have many thousands of packages of knick knacks, and without being able to reference the specific package you are calling about, their guess is as good as yours. You both know this is a package, but you are referring to it by different methods. FedEx needs their tracking information, but for your purpose, you only know it as a package.

So back to your question, you tell your Map to accept Integer, so that when you are compiling the code you write, it is able to use functions that act like your package's tracking label. (like "Integer.getSize" or "Integer.toString" )

telling the map to accept just a raw binary value, is like telling Fedex to find your package by calling it "knick knacks).

If the class you are creating does not need advanced options, you can definitely just use int as a param, but you will not be able to use the advanced methods that come with the Integer class.

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Pavle,

int and Integer are two different things. int is a primitive type, whereas Integer is a class with a type of int. Primitive types have no methods and only store the actual value, but the Class will hold a reference and can have methods used on it.

If you Google "int vs Integer Java" you'll get some helpful results. This one in particular from Stack Overflow explains it well.

Keep Coding! :) :dizzy: