Firstly you can just use the primitive types:
int x = 12;
double d = 3.3456;
Secondly, you can optionally use the object wrappers for those numbers:
Integer x = new Integer(12);
Double d = new Double(3.3456);
Third, if you want the string representation you can do this simply with:
String s = Integer.toString(12);
or just:
int x;
String s = "x = " + x;
or even printf() like syntax:
int x = 12;
String s = String.format("x = %d", x);
Lastly, Java since version 5 has supported auto-boxing and unboxing, which may confuse you if you're not expecting it. For example:
Integer i = 1234; // autoboxes int to Integer
and
int i = new Integer(1234); // autounboxes Integer to an int
Just use the primitive types unless you need the Number wrappers. The most common reason to use them is to put them in collections (Lists, etc).
Videos
Firstly you can just use the primitive types:
int x = 12;
double d = 3.3456;
Secondly, you can optionally use the object wrappers for those numbers:
Integer x = new Integer(12);
Double d = new Double(3.3456);
Third, if you want the string representation you can do this simply with:
String s = Integer.toString(12);
or just:
int x;
String s = "x = " + x;
or even printf() like syntax:
int x = 12;
String s = String.format("x = %d", x);
Lastly, Java since version 5 has supported auto-boxing and unboxing, which may confuse you if you're not expecting it. For example:
Integer i = 1234; // autoboxes int to Integer
and
int i = new Integer(1234); // autounboxes Integer to an int
Just use the primitive types unless you need the Number wrappers. The most common reason to use them is to put them in collections (Lists, etc).
What you're looking at in the first example is called autoboxing / autounboxing. Java (starting with version 5) will automatically convert between 5 and Integer.valueOf(5), which constructs a wrapper of type Integer from an int primitive. But the latter (Integer x = new Integer(12)) is absolutely correct.
It doesnt work that way in the second case, however, if you wanted to write something like 5.toString(). Autoboxing only occurs when assigning a primitive to a wrapper type, or passing a primitive where the wrapper type is expected, etc. Primitive types are not Objects and thus have no properties to be referenced.
Re: "why new", it's because all reference (non-primitive) types in Java are allocated dynamically, on the heap. So (autoboxing aside), the only way to get an Integer (or other reference type) is to explicitly allocate space for one.
I understand what the purpose of the wrapper classes is but the naming convention confuses me?
double is Double, float is Float, boolean is Boolean so why is int Integer?
Is it that way just because it is and always has been? Or is it something to do with humans may not see a difference between an uppercase "I" vs a lowercase "i" in some fonts.