• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why is that output ?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It prints 12. How does it work ?

When I tried to put other wrapper class in place of Integer like Short or Byte ,it does not compile .Why ?
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When I tried to put other wrapper class in place of Integer like Short or Byte ,it does not compile .Why ?



Well, you need to show us what you did here? How can we figure out what the compile error is, if you don't show us what you did? .... It would also be helpful, if you show us the compiler error too.

Henry
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you study how keys are managed for Maps ? What kind of special methods are used for keys ? How does a Map know a key is equals to another one ? Try to study that first if you haven't done it yet.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Henry said, you need to show more (be as helpful in your exposition of the problem, so that we may be able to better help you.)

As for the output of the code above, here is a hint: Integer overrides hashCode() and equals() from Object, whereas StringBuffer doesn't. Can you figure out what is going on with that information in mind?
 
A Agr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here I have 2 Questions :

1. Why does it print 12 and not 22 ?

2. When I try running code by replacing Integer by say Short



It gives compiler error as Short(int) is undefined .Short has 2 constructors ,one that takes short and other that takes string . But why is it giving error with value '1' wherein 1 is permissible value for short.

If Integer overrides hashcode and equals method,then there is still possible for 2 objects to have same hashcode and different values.So output as 12 is still not clear ?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A Agr wrote:
It gives compiler error as Short(int) is undefined .Short has 2 constructors ,one that takes short and other that takes string . But why is it giving error with value '1' wherein 1 is permissible value for short.


The way you can apply an automatic narrowing conversion from short to int is in an assignment statement where the RHS is a compile time constant in the representation range of the LHS. For example:


But that doesn't work for method arguments.

A Agr wrote:
If Integer overrides hashcode and equals method,then there is still possible for 2 objects to have same hashcode and different values.So output as 12 is still not clear ?


Do you know how the Map interface's put() method works? To put it simply, keys must be unique. So when you add the second mapping, since there is already a mapping with that key (since the method will determine that both keys are identical, since their hashcodes are the same and they are equal according to the Integer's equals() method,) it will replace the first mapping, and the method will return the previous value mapped from that key.
 
Hey cool! They got a blimp! But I have a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic