This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

OO basics doubt

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Can any one explain me why thje out put of following program came like this :
i1 == i2 = true
i1.equals(i2) = true
i3 == i4 = false
i3.equals(i4) = true


What I expecting was i3==i4 also true but it didnt happen why???
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C - trivial
D - A constructor without parameter MUST always exist. That's why sometime you declare a constructor like private A() {}; in order to not make the compiler create it (also if you do not need it).

consider a

class A {
public A() {} //default constructor
public A(String par) {} // another constructor
}

class B extends A{
public B() { super("ciao"); } //default constructor (so answer B is wrong)
public A(String par) {} // another constructor
}

A - is wrong because inside the method the varable are local
B - is wrong because B have a default constructor of the subclass can call a not default constructor of the parent
E - is wrong because a default constructor is created ALWAYS if it is not explicetely declared.
 
Maurizio Nagni
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there have to be an error in the web site datbase.... I am sure to have posted to another message!
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Divya:

Integer i is in the Wrapper class Integer.If it were equals your assumption would have been correct.

Remember:

== always compares the bit pattern and whether the two references refer to the same object.
equals compares the content(If two Wrapper classes are same and the content is similar it will result in true).
eg: Integer I = new Integer("4");
Integer f = new Integer("4");
I.equals(f) is true (same contents ,same class)
I==f is false (I and f donot refer to the same Object)
 
Maurizio Nagni
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ciao Divya,

this is not an answer just a note to say that in this case 8-bit seem to be the limit over which the == became false (member that 8-bit in java are signed so are beetwen -128 < x < 127): with 127 is still true; with 128 became false.
Curious.... I am looking for an explaination too.
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in K&B it is written::

In order to save memory, two instances of the following wrapper objects created through autoboxing will always be == when their primitive values are the same::
Boolean
Byte
Character from \u0000 to \u007f (7f is 127 in decimal)
Short and Integer from -128 to 127.
because 1000 is out of range of 127 hence you get that type of O/P

there may some misprinting as the matter writtne in bold was not their in the book..
you can refer to K&B errata..for such typos !!
 
Divya Gehlot
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for the explanation I know diffrence between == and equals.but my doubt is , for i1==i2 its showing true then why for i3==i4 o/p is false.Please explain me that.

howdy I repaired the line wrapping. service of the house. Bu.
[ August 03, 2007: Message edited by: Burkhard Hassel ]
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Divya


I tried the above given code i am also getting
same output
true
true
false
true

The reason for output false in the third case
is as you have used value 1000 for i3 and i4
which i think is out of range for an integer
(range for integer is -128 to 127)
so i have modified the above program as below



this time we get the output as
i1 == i2 = true
i1.equals(i2) = true
i3 == i4 = true
i3.equals(i4) = true

i hope it helps you to atleast some extent....
[ August 03, 2007: Message edited by: dhwani mathur ]
 
Priyam Srivastava
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Divya !

In order to save memory, two instances of the following wrapper objects created through autoboxing will always be == when their primitive values are the same::
Boolean
Byte
Character from \u0000 to \u007f (7f is 127 in decimal)
Short and Integer from -128 to 127.



read the above carefully it talks about '=='.

because i3 and i4 have their value greater than the above mentioned range, hence in that case above condition won't apply..

if you change the value of i3 & i4 to value between -127 to 128 then i3==i4 will give true.

but in this case where i3 and i4 have value 1000, '==' checks whether they(i3 & i4) are reffering to same object or not..and since they are not. hence false...

this is a special situation which you need to keep in mind..
i hoe this explanation helps!!
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

for the sake of completeness.

If the value is not in the range between -128 to 127 the behaviour is not defined by the standard. You may or may not get the same object reference.
 
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, seems like some of these posts were meant for another question. What's next up from int? long? you can't do .equals on long but == returns true.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic