Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 10th, 2015, 03:19 AM
Registered User
Points: 58, Level: 1
Points: 58, Level: 1 Points: 58, Level: 1 Points: 58, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
  #2 (permalink)  
Old October 11th, 2015, 03:15 AM
Authorized User
Points: 159, Level: 3
Points: 159, Level: 3 Points: 159, Level: 3 Points: 159, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Location: Romania
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
Reply With Quote
  #3 (permalink)  
Old October 12th, 2015, 05:05 AM
Registered User
Points: 58, Level: 1
Points: 58, Level: 1 Points: 58, Level: 1 Points: 58, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2015
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Strings are immutable objects

Thanks Cristina.
Well explained.
Reply With Quote
  #4 (permalink)  
Old November 10th, 2015, 07:03 PM
Registered User
 
Join Date: Nov 2015
Location: USA
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.
Reply With Quote
  #5 (permalink)  
Old January 27th, 2016, 05:26 AM
Authorized User
Points: 159, Level: 3
Points: 159, Level: 3 Points: 159, Level: 3 Points: 159, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Location: Romania
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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:23 PM.


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