• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubts on string class and overloading  RSS feed

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not using overloading nor overriding in that example. You are reassigning the reference to the String "xyz".
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You are not using overloading nor overriding in that example. You are reassigning the reference to the String "xyz".

iam asking separately about overloading and overiding,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
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

ok then whats it refers string is final,string is immutable we cannot change values
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

so how could possible cannot change content of the object and state of the object while declaring string.is that above code which is object because str means its reference right.i will check the link
 
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sekhar kiran wrote:iam asking separately about overloading and overiding,

I am not sure what you are asking about here? Overloading and Overriding occur in method invocation, in example here, you are using simply object instance.
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

String is immutable not final, which means that you cannot alter them once create. So, in this case, two new instance will be created.
Check this also. Hope it helps.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
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.
Check this also. Hope it helps.
Excellent link, but the real question is about reassigning.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
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.
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.

At simply, immutable and final are very different, not sure about the confusion. The class is final which is very well fine for me to make it immutable.

 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muhammad Khojaye wrote:
Muhammad Khojaye wrote: . . .
String is immutable not final

not sure about the confusion.

The confusion is that the String class is final, you said that it is not final
 
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)


The str variable is a reference to a String object. So str is one place in memory, and stores the memory address of a String object (instance) or is null. Making up some memory addresses:



What is immutable is the String object value on the right. What str refers to. But str can be made to refer to many different String objects throughout the lifetime of the program. However, each String that it refers to is immutable - the string cannot be changed in memory (although when no variable is referencing it, it will be deleted).



 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so in above code ,which is the object?what is immutable meant for?how should we use in above program
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



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
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Red Smith wrote: . . .
String str = "abc";

is equivalent to

String str = new String("abc");

(although there is a slight difference . . . ) . . .
There is more than a slight difference. The second declaration has two String objects in, the first only one.

The remainder of the post appears correct, however.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Red Smith wrote: . . .
String str = "abc";

is equivalent to

String str = new String("abc");

(although there is a slight difference . . . ) . . .
There is more than a slight difference. The second declaration has two String objects in, the first only one.

The remainder of the post appears correct, however.

i think str it refers one object and another one the values passed to method creates 2nd object ,is there any wrong?
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


Either statement [ String str = "abc"; | String str = new String("abc") ] ,

Do almost exactly the same thing:

- Both create a new reference variable named str that can refer to String objects. I guess it is considered as being created on the stack.
- In both cases str will refer to a String object.
- In both cases, the String object they refer to is NOT created on the stack.
- In both cases, the String object they refer to is immutable.

So conceptually they are identical above the covers. A String reference is created and it refers to a String object that has a value of "abc".
Under the covers it is different. The second way is standard Java - create the new String object on the heap. The first way is handled in a unique way for efficiency and space savings. It gets put into an area that is called, at least informally, the String Pool. The idea is that if you reuse the literal "abc" in your program it will just point to the "abc" String object that is already in the String Pool. Since String objects are immutable it can point any number of reference variables to a String Pool String object and there is no worry about conflicts.

The documentation for the String intern() method talks about the String Pool. Using the intern method you can place any string into the String Pool.

Wikipedia has an article on String interning:
String_interning
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Red Smith wrote: . . .
String str = "abc";

is equivalent to

String str = new String("abc");

(although there is a slight difference . . . ) . . .
There is more than a slight difference. The second declaration has two String objects in, the first only one.

The remainder of the post appears correct, however.


What are the two String objects in the second one?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 objedt and new String(abc) is another object,am i correct orwrong.
String str;
here str is reference varible means ,then what is the object here?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sekhar kiran wrote:str is one objedt and new String(abc) is another object,am i correct orwrong.

Wrong. str is never an object - it is a reference variable
sekhar kiran wrote:String str;
here str is reference varible means ,then what is the object here?

There is no object. str is a reference variable that at this point in time does not refer to any object.
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

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
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 . . .
Agree 100%
. . . There is no object. str is a String reference which refers to no object. It has the value null.
It only points to null when it is a field. If it is a local variable, it is undefined until it is definitely assigned.
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

i cant understand,can you explain with simple,
i run the program but it shows error ie
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



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
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Your questions are getting very confusing because you keep swapping between abc (without quotes) and "abc" (with quotes).
In Java these are not equivalent and the answers to your questions will depend on which you meant.
"abc" is a String literal
abc is (probably) a String variable, but as you haven't declared it anywhere we can't say for sure.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i edited my post check it Stuart A. Burkett
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't edit posts
If you look at what you are trying to compile it has a mistaken S in.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't edit posts
If you look at what you are trying to compile it has a mistaken S in.

yes i compile and run the program
output:true
false
why 2nd is false which having same value
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

ok i will check, as the redsmith explanation i have one little confusion ie
String str="abc";---------->so here str is reference ,here no object
String str=new String("abc");--------------->so here also str means reference ,so here two objects will be created what are they?
and one clarification,so if we provide

so output will be aaa, am i correct.so reassigning last reference will be comes the output when we want to print str
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sekhar kiran wrote:String str="abc";---------->so here str is reference ,here no object

Wrong. str is a reference, and "abc" is an 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?

Already answered: http://www.coderanch.com/forums/posts/list/625395#2860493
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!