Campbell Ritchie wrote:You are not using overloading nor overriding in that example. You are reassigning the reference to the String "xyz".
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:You didn't change the value of the string. The String "abc" still exists. What you did was change what the reference str points to.
Think of it as erasing someone's home address from a piece of paper, and writing a new address on that piece of paper. You did nothing to the original house, but if you now go to the address written down, you will see a different house.
Jesper de Jong wrote:First, you have to understand that variables in Java are references, not the objects themselves. So, str is a reference to a String object - it's not a String object itself.
When an object is immutable, it means that you cannot change the state of the object (you cannot change the content of the object). String objects are indeed immutable - once a String object is created, its content never changes.
In line 1 of your code, you declare the variable str to refer to a String object that contains the value "xyz".
In line 2 of your code, you change the variable. It now refers to whatever String object abc refers to. Note that the original String that contains "xyz" is not modified - it's just that the variable str now looks at a different String object.
Have a look at these Campfire Stories to understand how variables work: Cup Size -- a story about variables and Pass-by-Value Please.
sekhar kiran wrote:iam asking separately about overloading and overiding,
sekhar kiran wrote:i know the basic examples ,but what kind of situation we use them,ok but string is final right,so it cannot change their values
That is at best confusing. The String class is in fact marked final. What you mean is that the String reference in the code posted is not final, which means it can be reassigned.Muhammad Khojaye wrote: . . .
String is immutable not final, which means that you cannot alter them once create. So, in this case, two new instance will be created.
Excellent link, but the real question is about reassigning.Check this also. Hope it helps.
Campbell Ritchie wrote:
That is at best confusing. The String class is in fact marked final. What you mean is that the String reference in the code posted is not final, which means it can be reassigned.Muhammad Khojaye wrote: . . .
String is immutable not final, which means that you cannot alter them once create. So, in this case, two new instance will be created.
Muhammad Khojaye wrote:
Muhammad Khojaye wrote: . . .
String is immutable not final
not sure about the confusion.
sekhar kiran wrote:i know details of string ,ie string is final ,it is immutable,
why here output will be dispalyed as xyz,why cant abc;
when we use overloading and overiding,i know basic examples and details,but in real time which situation we use overloading and overiding.simple explaantion will be helpful
(note:please change the subject as doubt on string and overloading,overiding concepts)
sekhar kiran wrote:so in above code ,which is the object?
what is immutable meant for?
how should we use in above program
Red Smith wrote:
sekhar kiran wrote:so in above code ,which is the object?
String str = "abc";
is equivalent to
String str = new String("abc");
(although there is a slight difference in that the String object from a bare "abc" is said to be stored in a different place than 'new String("abc")' )
str is the reference variable | String("abc") is the object
There are two things in memory - one with a name - str - that is a reference variable. The other, has no name and is the String object. str contains a value that allows Java to find the String object.
[str] -------------------> [String("abc")]
str can be made to "refer" to another String object with an assignment statement but the String("abc") in memory cannot be changed. It can only be deleted by the garbage collector once no reference variable refers to it.
The String class contains no methods which allow it's data to be changed. No appendChar(), no upperCaseChar(), no replaceChar() etc.
That is what is immutable - the object that was created - String("abc") has no "mutators" - methods that change its data. Functions that in other languages might change the string, are designed in Java to make a copy and change the copy. For example, toUpperCase().
If the String object was mutable, that method would look like
void toUpperCase() { ...
and you use it like this:
str.toUpperCase();
Calling it would make the String modify itself to become uppercase. There would no longer be "hello" in memory, only "HELLO".
But since Strings are immutable the method is
String toUpperCase() { ...
and must be called like
String upper_my_string = my_string.toUpperCase();
A new uppercase String object is created based on the existing one, is made uppercase, and returned - the existing lowercase String object is left unmodified.
References can give the illusion that object data is mutable, like in the following:
)
It appears that an object was modified, but only the reference my_string was modified. A second String object was created - String("HELLO") - while the initial String object - String("hello") was left unmodified.
Then the location of the new String object was returned from the function and assigned to the my_string reference - making it refer to the new String object ( String("HELLO") ) and not the old ( String("hello") ).
what is immutable meant for?
I read that they made Strings immutable for security reasons. But immutable data in general has the advantage of preventing unwanted changes by another class. If you pass the same reference to two different objects, they will now both refer to the same data. One - objectA - may want to keep that data the same, the other - objectB - may want to change it. If the data is immutable, objectB is forced to make a copy to change the value, leaving the original data untouched, and keeping things correct with regard to objectA and the data. If the data is mutable, objectB can change the data, leaving objectA with a value that it does not want, a value that is incorrect to objectA.
how should we use in above program
There is more than a slight difference. The second declaration has two String objects in, the first only one.Red Smith wrote: . . .
String str = "abc";
is equivalent to
String str = new String("abc");
(although there is a slight difference . . . ) . . .
Campbell Ritchie wrote:
There is more than a slight difference. The second declaration has two String objects in, the first only one.Red Smith wrote: . . .
String str = "abc";
is equivalent to
String str = new String("abc");
(although there is a slight difference . . . ) . . .
The remainder of the post appears correct, however.
sekhar kiran wrote:
as per your explanation isclear and understood,but as per my knowledge i had some small doubts related to above explanation;as i studies ie
in above points anything wrong means update me
Campbell Ritchie wrote:
There is more than a slight difference. The second declaration has two String objects in, the first only one.Red Smith wrote: . . .
String str = "abc";
is equivalent to
String str = new String("abc");
(although there is a slight difference . . . ) . . .
The remainder of the post appears correct, however.
Jesper de Jong wrote:There's one String object for the literal "abc", and then a new String object that copies its data from the first String object.
sekhar kiran wrote:str is one objedt and new String(abc) is another object,am i correct orwrong.
sekhar kiran wrote:String str;
here str is reference varible means ,then what is the object here?
sekhar kiran wrote:
Jesper de Jong wrote:There's one String object for the literal "abc", and then a new String object that copies its data from the first String object.
str is one object and new String(abc) is another object,am i correct or wrong.
String str;
here str is reference varible means ,then what is the object here?
Red Smith wrote:
sekhar kiran wrote:
Jesper de Jong wrote:There's one String object for the literal "abc", and then a new String object that copies its data from the first String object.
str is one object and new String(abc) is another object,am i correct or wrong.
In the second statement is creates a String object in the String Pool to hold the "abc" literal, just like it does in the first. And then it passes a reference to that object to the String constructor which then goes and makes a new String object on the heap - copying the "abc" value from the object it was passed a reference to.
String str;
here str is reference varible means ,then what is the object here?
There is no object. str is a String reference which refers to no object. It has the value null.
Agree 100%Red Smith wrote: . . . In the second statement is creates a String object in the String Pool to hold the "abc" literal, just like it does in the first. And then it passes . . .
It only points to null when it is a field. If it is a local variable, it is undefined until it is definitely assigned.. . . There is no object. str is a String reference which refers to no object. It has the value null.
sekhar kiran wrote:
then which is the object? what the object refers in a program,as i studied if we want to create object means syntax: classname obj=new constructor();
l
Campbell Ritchie wrote:No, they are conceptually different. Their weakest‑precondition transformer effects are different.
The weakest precondition for the first line to establish the postcondition that there is one String object "abc" is true.
The weakest precondition for the second line to establish the postcondition that there is one String object "abc" is false.
They might make str point to a string equals to "abc" in both cases, but you can easily demonstrate that they are different with the operator you should not use.Remember all references to "abc" correspond to the one interned object.
Red Smith wrote:
sekhar kiran wrote:
then which is the object? what the object refers in a program,as i studied if we want to create object means syntax: classname obj=new constructor();
l
In most cases it is fine, or maybe preferred, to consider the variable as the object. For instance:
System.out.println(total_sales);
It is normally stated that we are printing total_sales, rather than the more correct, we are printing the object referred to by the total_sales reference.
But in some cases, like in your original post for this thread, it is necessary to think of the variable as an object reference and not an object.
sekhar kiran wrote:
oh ok,but in below code could you tell which is object
here two objects are created ,so as your explanation i got one object ie new String(abc),what is 2nd object
Campbell Ritchie wrote:Don't edit posts
![]()
If you look at what you are trying to compile it has a mistaken S in.
Campbell Ritchie wrote:The idea is to demonstrate that although the Strings all have the same content, in one instance you have one object and in the other instance you have two objects, so as to show that the two initialisations are not equivalent to each other.
You can read about == in the link I sent earlier: here. You can read more about Strings here.
And I see it was my mistake with the S. Sorry.
sekhar kiran wrote:String str="abc";---------->so here str is reference ,here no object
sekhar kiran wrote:String str=new String("abc");--------------->so here also str means reference ,so here two objects will be created what are they?
Steve
Don't get me started about those stupid light bulbs. |