• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Wrapper

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






ans:

i1 == i2, returns False in both the cases explain.

1) In the first case i understand
that both the statments are completly different. But i thought to save memory i2 would be
refering to i1.

2) i2 with Integer.valueof() would be expecting a integer value but its a string out there
i thought i would result in a compilier error. but it didnt happen.


I think both needs the knowledge of API. I was unable to sort out.

Please explain.

Thanks in advance
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java 5.0 API:
static Integer valueOf(String s)
Returns an Integer object holding the value of the specified String.

So, perfectly legal way of defining an Integer object. Looks like you understand the == not coming up as the same because they are not the same object, so I'll not comment on that. Do you not have access to the API docs?
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by srinivas sridaragaddi:
Hi,

ans:

i1 == i2, returns False in both the cases explain.

1) In the first case i understand
that both the statments are completly different. But i thought to save memory i2 would be
refering to i1.

2) i2 with Integer.valueof() would be expecting a integer value but its a string out there
i thought i would result in a compilier error. but it didnt happen.

I think both needs the knowledge of API. I was unable to sort out.

Please explain.

Thanks in advance



Hi again Srinivas

i1 and i2 are two separate instances of Integer and i1 == i2 compares the hashcode values of the Objects, not the numeric values. Try using i1.equals(i2) and you'll get true.

The Integer Wrapper has an overloaded valueOf() method that accepts a string, and converts it internally to the proper value.

Hope this helps...
Aloha,
Doug
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
srinivas quotes


In the first case i understand that both the statments are completly different. But i thought to save memory i2 would be refering to i1.


In order to do that, and make wrapper objects refer to each other you have 2 ways:

In this code all objects refer to One object, because they are at the range of(-128 and 127).
At line 1 you created using autoboxing, at line 2 you created using valueOf(int)
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for your reply

Exactly craig i didnt understand how both references are different

Integer i1 = Integer.valueOf(42);
Integer i2 = Integer.valueOf("42");

When the syntax for wrapper constructor says that they are the 2 different constructors for creaing a wrapper. I was expecting both to refer to the same object as "new" keyword is not used so that a new reference is created.

or

is it a short cut as String s="abc" is same as String s=new String("abc");
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Creats one String object in the String pool.

Creats two String objects, one in normal(non pool) memory- 's' will refer to that one, and one in the String pool.
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i thought String s="abc" is a short cut or alias of
String s=new String("abc") is that correct or not. Now i am getting confused .

Ahmed thanks for your explanation my doubt was not concerned with that. i wanted to know whether first is a short cut of second.

do you have any idea about this....
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i thought String s="abc" is a short cut or alias of
String s=new String("abc") is that correct or not. Now i am getting confused .



Hi Srinivas

First statement is not alias nor shortcut of the second statement. Both has two different meaning which Ahmed Yehia already explained in the previous post.
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i thought String s="abc" is a short cut or alias of
String s=new String("abc") is that correct or not.


As you saw they are different, but because Strings are used frequently in code you can create one using a string literal directly.
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

because Strings are used frequently in code you can create one using a string literal directly.



so Ahmed , both statements cannot be treated as short cut but
String s="abc" can be thought as a direct way of creating a object. here a new object is created right? so that later it will be eligible for garbage collection if s is set to null (s=null);
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look at the source code for class Integer, which you can find in the file src.zip in your JDK installation directory, you'll see that the Integer.valueOf(String s) method looks like this:

So, it always returns a new Integer object; it does not return an Integer object from the cache if the value is between -128 and 127. I don't know why Sun implemented it like this; it would have been smarter if they did this:

But unfortunately that's just not how they did it.
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a TON JESPER,

I was unable to understand anything from API, thanks again for the wonderful explanation. Can i request you to answer my other posts as i have huge doubt. which is uncleared till now.

Thanks in Advance
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic