Solution:
Why this is not possible?
Since String and Integer are not in the similar Object hierarchy.
Object
/ \
/ \
String Integer
The casting which you are attempting, performs only in case they are in the same hierarchy, for example
Object
/
/
A
/
/
B
In this instance, (A) objB
or (Object) objB
or (Object) objA
will perform.
Therefore, as others have mentioned meanwhile, to convert an integer to string use:
String.valueOf(integer)
, or Integer.toString(integer)
for primitive,
Or,
Integer.toString()
for the object.
No, Integer
and String
are different types. To convert an integer to string employ: String.valueOf(integer)
, or Integer.toString(integer)
for primitive, or Integer.toString()
for the object.
For int
types employ:
int myInteger = 1;
String myString = Integer.toString(myInteger);
For Integer
types employ:
Integer myIntegerObject = new Integer(1);
String myString = myIntegerObject.toString();
No. Each object can be casted to an java.lang.Object
, not a String
. In case you want a string representation of whatever object, you have to call the toString()
method; this is not the similar as casting the object to a String.
Objects can be converted to a string employing the toString()
method:
String myString = myIntegerObject.toString();
There is no such rule about casting. For casting to work, the object should really be of the type you're casting to.
You can't cast apparently anything to a String
that isn't a String
. You must use either:
"" + myInt;
or:
Integer.toString(myInt);
or:
String.valueOf(myInt);
I prefer the second form, however I think it's personal choice.
Edit OK, here's why I offer the second form. The first form, at the time compiled, could instantiate a StringBuffer
(in Java 1.4) or a StringBuilder
in 1.5; one more thing to be garbage collected. The compiler doesn't optimise this as far as I could say. The second form also has an analogue, Integer.toString(myInt, radix)
that lets you seclude whether you want hex, octal, etc. In case you need to be consistent in your code (entirely aesthetically, I guess) the second form can be exercised in more places.
Edit 2 I occupied you intended that your integer was an int
and not an Integer
. In case it's meanwhile an Integer
, only employ toString()
on it and be done.
You must call myIntegerObject.toString() in case you want the string representation.
Casting is different than converting in Java, to exercise informal terminology.
Casting an object implies that object meanwhile is what you're casting it to, and you're only saying the compiler about it. For example, in case I have a Foo
reference that I know is a FooSubclass
example, then (FooSubclass)Foo
says the compiler, "don't alter the example, only know that it's really a FooSubclass
.
On the other hand, an Integer
is not a String
, although (as you point out) there are methods for obtaining a String
that illustrates an Integer
. Because no instance of Integer
can ever be a String
, you can't cast Integer
to String
.
convert it back to the main Integer
and cast it to Long
obj.setID(((Integer) row.get("ID")).longValue());
//obj.setID(((Long) row.get("ID")));
Java basic:
Integer num = 1;
Long numInLong = num.longValue(); // Integer to Long
Long numInLong2 = Long.valueOf(num); // Integer to Long