Results 1 to 11 of 11
Thread: Java Challenge [II]
- 01-19-2010, 08:30 AM #1
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
Java Challenge [II]
Hey am reopening the thread for the java challenge. Lets not have the mistakes
of the previous thread.
Same rules apply.
Question:1. The person who answers the current question should try to ask the next
question so that it helps the people to learn new stuff and keep the thread
moving.
2. Everyone should cooperate, dont really care if you google the question main purpose is to learn it.
3. NO FLAMING at any costs.
Try to attach an image to the frame where the application provides a simple
mechanism for the user to choose an image and it should be pasted.
Pretty easy one..just need to know the correct API
- 01-20-2010, 05:41 AM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Wiat, so like a tree of images and you click on and it displays an image in a JPanel? or a button for each image and it copies it to the clipboard. Iam confused.
- 01-20-2010, 09:02 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Here's a much more tricky one (no programming involved); if you all love autoboxing so much, explain the output:
kind regards,Java Code:import java.util.*; public class Autoboxing { public static void main(String[] args) { Object foo = new Long(0xcafebabedeadbeefL); List<Object> bars = new ArrayList<Object>(); bars.add(true ? (Long)foo : (Long)foo); bars.add(true ? (Long)foo : (Number)foo); bars.add(true ? (Long)foo : (Double)foo); bars.add(true ? (Long)foo : ((Long)foo).longValue()); System.out.print("== :"); for (Object bar : bars) System.out.print(" "+(foo == bar)); System.out.println(); System.out.print("equals:"); for (Object bar : bars) System.out.print(" "+foo.equals(bar)); System.out.println(); } }
Jos
- 01-20-2010, 10:06 PM #4
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
the 1st line checks wether both of them relate to the same instance rather than the whole value itself. So, its got the respective truth values
I am not sure of the last value, maybe when you do long.longvalue(), it creates a new instance or something, so it returns false eventhough values are the same.
where the 2nd line checks for the value itself.
- 01-20-2010, 11:22 PM #5
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Wow, that is very confusing because I though (true ? a : b) would always return a, but when I replace "bars.add(true ? (Long)foo : (Double)foo);" with "bars.add((Long)foo);" it will return true for both instead of false.
-
I'm probably cheating here, but I peeked, and the JLS gives some guidance here. Expressions
The ternary operation:
always returns something, and regardless if it returns b or c, it can only return one type meaning it has to decide what type to return before it "operates". If the 2nd and 3rd operands are of different types, then some logic must be done to decide the most appropriate return type.Java Code:a ? b : c
-
- 01-21-2010, 12:13 AM #8
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Oh, I get it so it always returns foo(because of the true) but it wont always cast it as (Long).
Last edited by collin389; 01-21-2010 at 12:16 AM.
-
See reply #6. The JVM converts the (Long)foo to a double using the rules of "Binary Numeric Promotion". For details please look here: Conversions and Promotions
- 01-21-2010, 04:27 AM #10
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
- 01-21-2010, 03:18 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Here's a bit of help: there are two mechanism working against/for you:
1) the type of the b?t:f expression is the widest type of operands t and f, e.g. if t is a long and f is an int the type of the entire expression is long
2) autoboxing: whenever an object is needed the primitive type is boxed to its corresponding wrapper type, e.g. long --> Long
kind regards,
Jos
Similar Threads
-
Regarding Java Challenge Thread
By Fubarable in forum New To JavaReplies: 0Last Post: 01-10-2010, 07:58 PM -
Debug challenge: how do you debug a Java program?
By CJSLMAN in forum New To JavaReplies: 60Last Post: 07-04-2009, 10:28 AM -
java programming challenge Number 1
By tacosalad in forum New To JavaReplies: 5Last Post: 01-16-2008, 10:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks