Results 1 to 11 of 11
  1. #1
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default Java Challenge [II]

    Hey am reopening the thread for the java challenge. Lets not have the mistakes

    of the previous thread.

    Same rules apply.
    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.
    Question:
    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

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    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.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Here's a much more tricky one (no programming involved); if you all love autoboxing so much, explain the output:

    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();
    	}
    }
    kind regards,

    Jos

  4. #4
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    Here's a much more tricky one (no programming involved); if you all love autoboxing so much, explain the output:

    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();
    	}
    }
    kind regards,

    Jos
    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.

  5. #5
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm probably cheating here, but I peeked, and the JLS gives some guidance here. Expressions

    The ternary operation:
    Java Code:
    a ? b : c
    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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by collin389 View Post
    Wow, that is very confusing because I though (true ? a : b) would always return a,...
    This is correct, and in the example above, it always is returning a, or "a" converted.

  8. #8
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    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.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  10. #10
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    Quote Originally Posted by Fubarable View Post
    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
    How abt the last one(long.Longvalue()). i am little confused over there. why is it showing false in the 1st case.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by [RaIdEn] View Post
    How abt the last one(long.Longvalue()). i am little confused over there. why is it showing false in the 1st case.
    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

  1. Regarding Java Challenge Thread
    By Fubarable in forum New To Java
    Replies: 0
    Last Post: 01-10-2010, 07:58 PM
  2. Debug challenge: how do you debug a Java program?
    By CJSLMAN in forum New To Java
    Replies: 60
    Last Post: 07-04-2009, 10:28 AM
  3. java programming challenge Number 1
    By tacosalad in forum New To Java
    Replies: 5
    Last Post: 01-16-2008, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •