Wrox Programmer Forums
|
BOOK: Java Programming 24-Hour Trainer 2nd Edition
This is the forum to discuss the Wrox book Java Programming 24-Hour Trainer, 2nd Edition by Yakov Fain; ISBN: 978-1-118-95145-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Java Programming 24-Hour Trainer 2nd Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 10th, 2015, 03:19 AM
Registered User
 
Join Date: Oct 2015
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Question immutable strings

Page 49. Says Strings are special objects in Java.
'They are immutable' you can't change the value of a String that has been initialized.

Does it mean the compiler will allow it...but unexpected behaviour may occur in run-time.. Why the compiler stop from compiling

Below compile/with allowable changing of String objects :-

public class MyString {

String mystr = "Mary";

public static void main(String args[]) {
MyString mystring = new MyString();

System.out.println( mystring.mystr);
mystring.mystr = "Smith, Mary";

System.out.println( mystring.mystr);

}
}

Output
Mary
Smith,Mary
 
Old October 11th, 2015, 03:15 AM
Authorized User
 
Join Date: Feb 2012
Posts: 30
Thanks: 5
Thanked 4 Times in 4 Posts
Post Strings are immutable objects

Hi,

When you do
Code:
mystring.mystr = "Smith, Mary";
a new String object is created, as (I hope) the following drawing explains:
https://java4kids.moodlecloud.com/pl...24HTrainer.jpg

Cheers,
Cristina
 
Old October 12th, 2015, 05:05 AM
Registered User
 
Join Date: Oct 2015
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Strings are immutable objects

Thanks Cristina.
Well explained.
 
Old November 10th, 2015, 07:03 PM
Registered User
 
Join Date: Nov 2015
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think that you need to take a look at what means a reference type variable.
A string is immutable in JAVA. This means that you cannot change the text that it holds once it is initialized.
 
Old January 27th, 2016, 05:26 AM
Authorized User
 
Join Date: Feb 2012
Posts: 30
Thanks: 5
Thanked 4 Times in 4 Posts
Lightbulb string values are immutable in JavaScript as well

If you know JavaScript, it may be easier to understand.

The following piece of code

Code:
var myStr = "Mary";
myStr[0] = "S";
leads to TypeError: 0 is read-only.

However, the following is fine.

Code:
myStr = "Smith, Mary";
The contents of myStr cannot be altered, which does not mean that myStr cannot be changed.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Immutable objects in C# viveksrivastava C# 1 December 13th, 2007 05:11 AM
strings bschleusner C# 2005 3 February 27th, 2007 11:46 PM
strings Moharo Pro PHP 2 May 17th, 2004 06:15 AM
strings... slowhand BOOK: ASP.NET Website Programming Problem-Design-Solution 1 January 5th, 2004 06:27 AM
strings slowhand SQL Server 2000 1 January 5th, 2004 12:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.