Results 1 to 18 of 18
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By JosAH

Thread: Help in Understanding the code

  1. #1
    CodeX Pro is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Help in Understanding the code

    Java Code:
    import java.util.HashMap;
    import java.util.Map;
    
    public class ExplainTheCode {
        
        private static Map<Integer, Integer> map;
        private static int value = 0;    
        {
            value += map.get(1) + map.get(2);
        }
        
        static{
            map = new HashMap<Integer, Integer>() {{
                put(1,2000000000); put(2, 1000000000); put(3, 300);
            }};
            value *= map.get(3);
        }
        
        public ExplainTheCode() {
            System.out.println("value: " + value);
        }
        
        public static void main(String[] args) {
            new ExplainTheCode();
        }
    }
    What is the order of execution and why?

    Why is the output -1294967296 (how is it calculated)?

    How does this part work (what happens in the background)?
    Java Code:
    map = new HashMap<Integer, Integer>() {{
        put(1,2000000000); put(2, 1000000000); put(3, 300);
    }};

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: Help in Understanding the code

    I would recommend that to view the order of execution you use an IDE (e.g. Eclipse, NetBeans,...) and then single step thru the program to see what is happening. It would also be helpful to have the source code of the JDK so that the IDE can reference it during execution.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    CodeX Pro is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: Help in Understanding the code

    Quote Originally Posted by jim829 View Post
    I would recommend that to view the order of execution you use an IDE (e.g. Eclipse, NetBeans,...) and then single step thru the program to see what is happening. It would also be helpful to have the source code of the JDK so that the IDE can reference it during execution.

    Regards,
    Jim

    I have run the code in netbeans IDE but the output is trickier to understand and I want to know whats happening and why this works in this way

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default Re: Help in Understanding the code

    The static { ... } and { ... } blocks outside of any method are a static initializer and initializer respectively. The first is run after the class has just been loaded and the second initializer is run before a constructor is run. The funny negative numer is the result of integer overflow caused by the multiplication of those two big numbers in the map.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    CodeX Pro is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: Help in Understanding the code

    Quote Originally Posted by JosAH View Post
    The static { ... } and { ... } blocks outside of any method are a static initializer and initializer respectively. The first is run after the class has just been loaded and the second initializer is run before a constructor is run. The funny negative numer is the result of integer overflow caused by the multiplication of those two big numbers in the map.

    kind regards,

    Jos
    Thank You sir your reply was helpful and my first two questions are answered, now sir can you please explain me the third question that I asked you

  6. #6
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Help in Understanding the code

    Hi,
    For the 3rd question you asked. You are creating a HashMap called map in this case and you are then populating it with key-values(integer,integer). The put method adds these values to the newly created map.

    Regards

  7. #7
    CodeX Pro is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: Help in Understanding the code

    Quote Originally Posted by lannie1980 View Post
    Hi,
    For the 3rd question you asked. You are creating a HashMap called map in this case and you are then populating it with key-values(integer,integer). The put method adds these values to the newly created map.

    Regards

    I know that much very well sir but then why two levels of {{ .... }} is used , musts be something different.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: Help in Understanding the code

    That's how you pass block of statements to be executed during instance creation. Check out this example:

    Java Code:
    public class Example {
       public static void main(String[] args) {
          /** 
           * Typical invocation
           */
          Foo f1 = new Foo();
          f1.showMsg(); // Displays Hello
          
          /** 
           * Override showMsg, single level of braces
           */
          Foo f2 = new Foo() {
             public void showMsg() {
                System.out.println("World");
             }
          };
          f2.showMsg(); // Displays World
          /**
           * Alter instance field -- used two levels of braces to add
           * statements to be executed.  Without doing any method calls the println
           * and msg assignment will be executed.
           */
          Foo f3 = new Foo() { 
             {
                System.out.println("Java is lots of fun!!");
                msg = "Hello, World";
             }
          };
          // Java is lots of fun!!  is printed
    
          f3.showMsg(); // Displays Hello, World
       }
    }
    
    class Foo {
       String msg = "Hello";
       public void showMsg() {
          System.out.println(msg);
       }
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default Re: Help in Understanding the code

    Quote Originally Posted by CodeX Pro View Post
    I know that much very well sir but then why two levels of {{ .... }} is used , musts be something different.
    If you construct a HashMap, you do this:

    Java Code:
    Map<Integer, Integer> map= new HashMap<Integer, Integer>();
    If you want to extend it anonimously, you do this:

    Java Code:
    Map<Integer, Integer> map= new HashMap<Integer, Integer>() {
       ...
    };
    But now you're (almost) free to stick in any code you want; also an initializer can be put there:

    Java Code:
    Map<Integer, Integer> map= new HashMap<Integer, Integer>() {
       {
          // initialization code goes here ...
       }
    };
    Reformatting the above gives this:

    Java Code:
    Map<Integer, Integer> map= new HashMap<Integer, Integer>() {{
          // initialization code goes here ...
    }};
    There you have your double curly brackets.

    kind regards,

    Jos
    CodeX Pro likes this.
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    CodeX Pro is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: Help in Understanding the code

    Quote Originally Posted by JosAH View Post
    If you construct a HashMap, you do this:

    Java Code:
    Map<Integer, Integer> map= new HashMap<Integer, Integer>();
    If you want to extend it anonimously, you do this:

    Java Code:
    Map<Integer, Integer> map= new HashMap<Integer, Integer>() {
       ...
    };
    But now you're (almost) free to stick in any code you want; also an initializer can be put there:

    Java Code:
    Map<Integer, Integer> map= new HashMap<Integer, Integer>() {
       {
          // initialization code goes here ...
       }
    };
    Reformatting the above gives this:

    Java Code:
    Map<Integer, Integer> map= new HashMap<Integer, Integer>() {{
          // initialization code goes here ...
    }};
    There you have your double curly brackets.

    kind regards,

    Jos
    You're a genius. Tell How can I gain more proficiency in Java

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

    Default Re: Help in Understanding the code

    Quote Originally Posted by CodeX Pro View Post
    You're a genius. Tell How can I gain more proficiency in Java
    Study, study, study and practice, practice, practice and keep a load of scribbling paper nearby (a whiteboard is handy too).

    kind regards,

    Jos
    CodeX Pro likes this.
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: Help in Understanding the code

    Gosh! I wish I had provided an example which demonstrated that! :)

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: Help in Understanding the code

    Quote Originally Posted by jim829 View Post
    Gosh! I wish I had provided an example which demonstrated that! :)

    Regards,
    Jim
    Sometimes my old lecturer genes rear their ugly head (*)

    kind regards,

    Jos

    (*) not to worry though, I have pills for that ;-)
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    CodeX Pro is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: Help in Understanding the code

    Quote Originally Posted by JosAH View Post
    If you construct a HashMap, you do this:

    Java Code:
    Map<Integer, Integer> map= new HashMap<Integer, Integer>();
    If you want to extend it anonimously, you do this:

    Java Code:
    Map<Integer, Integer> map= new HashMap<Integer, Integer>() {
       ...
    };
    But now you're (almost) free to stick in any code you want; also an initializer can be put there:

    Java Code:
    Map<Integer, Integer> map= new HashMap<Integer, Integer>() {
       {
          // initialization code goes here ...
       }
    };
    Reformatting the above gives this:

    Java Code:
    Map<Integer, Integer> map= new HashMap<Integer, Integer>() {{
          // initialization code goes here ...
    }};
    There you have your double curly brackets.

    kind regards,

    Jos
    I got it this far but another question came to my mind that if the hashmap is acting as an initializer then when and how is it called.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default Re: Help in Understanding the code

    Quote Originally Posted by CodeX Pro View Post
    I got it this far but another question came to my mind that if the hashmap is acting as an initializer then when and how is it called.
    I don't understand your question; a map is thing; it can only do things with itself; it can't initialize another object or class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: Help in Understanding the code

    The initializer block for the HashMap is executed during instance creation. Did you see the example I posted earlier?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  17. #17
    CodeX Pro is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: Help in Understanding the code

    Quote Originally Posted by jim829 View Post
    The initializer block for the HashMap is executed during instance creation. Did you see the example I posted earlier?

    Regards,
    Jim
    but there is no initializer block for hashmap

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default Re: Help in Understanding the code

    Quote Originally Posted by CodeX Pro View Post
    but there is no initializer block for hashmap
    But you are anonymously extending the HashMap class and there's an initialization block in that extension; carefully reread the examples above again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Understanding Problem in Code !
    By Jack_Tauson_Sr in forum New To Java
    Replies: 3
    Last Post: 03-25-2013, 02:20 AM
  2. Need help understanding steganography code
    By nrott in forum New To Java
    Replies: 3
    Last Post: 02-04-2012, 11:20 AM
  3. Urgent help understanding code
    By varons666 in forum New To Java
    Replies: 3
    Last Post: 06-03-2011, 07:51 PM
  4. need help understanding part of code
    By mustachMan in forum New To Java
    Replies: 7
    Last Post: 02-09-2011, 07:11 PM
  5. trouble understanding code help
    By yasmin k in forum New To Java
    Replies: 4
    Last Post: 11-16-2009, 09:46 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
  •