Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6123227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:59:27+00:00 2026-05-23T15:59:27+00:00

on JLS 3, 17.5 Final Field Semantics’s second discussion part: http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.5 It is said

  • 0

on JLS 3, 17.5 Final Field Semantics’s second discussion part: http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.5

It is said that myS can equals “/tmp”, I personally can not understand this. Any one can give more explain? Another point is what this example tells, does it mean if we want to share Global.s between multi thread, we need to make it final(if it is final, then can not change after construction) or need to sync when read and write? or declare a String array with length 1 and final so that can be changed and shared??

the original contents in JLS:


Consider the following example. One thread (which we shall refer to as thread 1) executes

Global.s = "/tmp/usr".substring(4);

while another thread (thread 2) executes

String myS = Global.s;
if (myS.equals("/tmp"))System.out.println(myS);

the explain in JLS:

String objects are intended to be immutable and string operations do not perform synchronization. While the String implementation does not have any data races, other code could have data races involving the use of Strings, and the memory model makes weak guarantees for programs that have data races. In particular, if the fields of the String class were not final, then it would be possible (although unlikely) that Thread 2 could initially see the default value of 0 for the offset of the string object, allowing it to compare as equal to “/tmp”. A later operation on the String object might see the correct offset of 4, so that the String object is perceived as being “/usr”. Many security features of the Java programming language depend upon Strings being perceived as truly immutable, even if malicious code is using data races to pass String references between threads.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:59:27+00:00Added an answer on May 23, 2026 at 3:59 pm

    Strings are implemented using a char[], offset, and count. The substring method constructs a new String object with the same char[] and a new offset and count. Based on the execution semantics, it would be possible for the new String to be partially initialized when thread 2 attempts to access it. According to the source, substring returns a new String object constructed with a simple constructor:

    644       // Package private constructor which shares value array for speed.
    645       String(int offset, int count, char value[]) {
    646           this.value = value;
    647           this.offset = offset;
    648           this.count = count;
    649       }
    

    So without marking char[], offset, and count as final in the String class definition, then thread 2 might see inconsistent values when it accesses them. If that happens, then the char[] could be set, but the offset and count might be wrong. If offset was still showing as the default of 0, you’d see the whole string. Of course, it would require amazing timing, specific reordering of instructions by the JIT, and a whole host of “luck” to make this happen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading this: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2 They say: Consider the example program: class Point { int
According to this documentation ( http://java.sun.com/docs/books/jls/third_edition/html/lexical.html , 3.10.6) an OctalEscape will be converted to
According to: http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html 4.5.2 Variables of Reference Type A reference type can hold a
Is there anything in Python akin to Java's JLS or C#'s spec?
I'm reading Effective Java Second Edition by Joshua Bloch and im confused by below
Is there a max length for class/method/variable names in Java? the JLS doesn't seem
The following compiles fine in my Eclipse: final int j = 1/0; // compiles
Is this valid Java? import java.util.Arrays; import java.util.List; class TestWillThatCompile { public static String
I've come across an oddity of the JLS, or a JavaC bug (not sure
Logically, if(!foo) and if(foo == false) are equivalent. How are they represented in Java?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.