Results 1 to 20 of 27
- 01-05-2009, 07:44 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 1
- Rep Power
- 0
- 01-05-2009, 07:57 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
First of all, please choose the correct sub-forum next time posting here. Read our FAQ page as well.
Go through the Integer array and copy each element to the int array. Did you try that?
- 01-06-2009, 07:46 AM #3
int x;
Integer s=12;
x=(int)s;
System.out.print(x);
cut and paste above code in your main method of ur java class,and run the application.
i hope u will be clear by the code,
- 01-06-2009, 01:38 PM #4
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
REALLY DOES THIS WORK??? I don't think so... How can the compiler convert between an Integer object and a primitive type? Anyway this would be a bad practice, even if it worked
Originally Posted by makpandian
That is the right way
Originally Posted by Eranga
- 01-06-2009, 03:14 PM #5
Integer is a "wrapper" around the primitive type. That means it holds the primitive type as a field. Look at the API listing for Integer for methods that explicitly return the primitive type. The method mentioned above also works, since the compiler recognizes that Integer is a wrapper for int.
- 01-06-2009, 04:19 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, the way makpandian pointing is working. But it's not good practice at all. Why you need an explicit casting there, useless and very bad logic it is for me. Casting an object into a primitive is the case.
- 01-06-2009, 08:01 PM #7
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
I have to quote myself:
I tried and I realized it works. But I think that is NOT the right way to solve his problem. First of all he wants to convert between arrays, not just single instances...
Originally Posted by raffaele181188
And the cast is very unpleasant to read. With generic types there's no real need to use casts in Java nowdays. Maybe there's still some exception, but I think you seldom need to cast. And this is not the case, you MUST use the intValue() method, and copy each element in the array using a for loop
- 01-07-2009, 03:01 AM #8
- 01-07-2009, 11:06 AM #9
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
You're right. I know that story but I had never seen an explicit cast between the primitive value and its object counterpart. I believed autoboxing to be a kind of "compiler's magic" introduced to help young and unexperienced developers. And I still think so, since mixing ints and Ingtegers leads to "mystic" code. I always use:
Originally Posted by fishtoprecords
And there's no real need in this situation to use such a nasty cast.Java Code:Integer i = new Integer(anIntVariable); .... int anInt = i.intValue();
IMHO
- 01-08-2009, 02:20 AM #10
I don't see any "nasty cast" in either option. Or no real difference between them
There are fewer characters in the second version.Java Code:int an1Int = i.intValue(); int an2Int = (int) i;
- 01-08-2009, 10:46 AM #11
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
Yes, you save 5 characters. But why USING CASTS? If you have a "regular" OO way to do that, simply invoking a method...
I have to repeat, nowdays there's no real need to use casts in Java code... At least in simple exercises...
It's a C practice to convert from a type to another one with a cast.
- 01-08-2009, 07:12 PM #12
Well, you seem to have a theological argument with them, and I can't argue theology.
casts are needed occasionally, not as a general practice, but sometimes. More often with the left-over C constructs of int, float, double, byte, etc. rather than pure OO flavors of Integer, Float, etc.... but the language includes the crocks from C, and they get used every day in every program.
There are other things to worry about, IMHO
- 01-08-2009, 07:49 PM #13
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
@fish
It' not a theological argument... It's simply bad programming practice, it's about elegance. Why using casts (a language fature) when you have a method (a programming feature) that does the same thing? It's simply useless in this case.
Java creators introduced generics to avoid casts. And I think now there's no real need to cast anything.. Can you make me an example in which you are forced to cast (eg there are no other ways) between Objects (not primitives)?
- 01-08-2009, 08:18 PM #14
- 01-08-2009, 08:28 PM #15
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
You're right, but there are times when you need a meter, a way to judge something. For example if a child said:I learned long ago that the way to peace is to not argue religion
30 = 5 + 5 + 5 + 5 + 5 + 5
You would:
"Hey, that is not the right way! 30 = 5 x 6"
However both work... But the second is truly the best approach! In math sciences elegance has always been considered an important quality, and programming is a math application. Casts were needed when Java was created, nowdays there's no need. If you can:
If you teach someone the right way to do something then you prevent him from making mistakes (and casts between objects are error-prone) and a cast is not needed in this case
Originally Posted by myself
- 01-08-2009, 08:40 PM #16
- 01-08-2009, 08:52 PM #17
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
YES THEY ARE THE SAME!!! :D
The thing is, in 2009 no one has to do 5 + 5 + ..., everyone should 5 x 6
Casts are the same. You will always be able to cast but why doing it, since the Java creators too felt casts are dangerous?
In this case there is no need to cast. And if you can make me that example I'll always be waiting for you
- 01-08-2009, 08:56 PM #18
- 01-08-2009, 09:05 PM #19
Member
- Join Date
- Dec 2008
- Location
- Italy
- Posts
- 79
- Rep Power
- 0
Yes I have. Official Sun Tutorial:
Generics
Sun introduced generics, there MUST be a reason... Have you found your example to demonstrate the need for casts?
- 01-09-2009, 03:28 AM #20
Sun introduced Generics in Java 1.5. There is lots of code that was written before then. And generics are not cleanly backwards compatible, so that old code is not going away anytime soon.
Plus, generics are just compile time, they do nothing at run time.
You must cast every time you override equals. While there are many ways to do it, I generally do it something like:
Java Code:public boolean equals(Object obj) { if (obj instanceof SomeClass) { SomeClass anSC = (SomeClass) obj; return equals(anSC); } else return false; } public boolean equals(SomeClass arg) { // real implementation }
Similar Threads
-
Integer to String
By zervine in forum Forum LobbyReplies: 3Last Post: 09-12-2008, 12:07 PM -
Integer length
By jithan in forum New To JavaReplies: 1Last Post: 06-12-2008, 03:35 PM -
How to convert integer to the hexadecimal and octal number
By Java Tip in forum java.langReplies: 0Last Post: 04-06-2008, 07:40 PM -
Breaking down an integer
By Emily in forum New To JavaReplies: 1Last Post: 03-06-2008, 06:39 PM -
Integer vs int
By bugger in forum New To JavaReplies: 1Last Post: 11-14-2007, 09:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks