Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Strings literals

 
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Literal strings within the same class in the same package represent references to the same String object.
2. Literal strings within different classes in the same package represent references to the same String object.
3. Literal strings within different classes in different packages likewise represent references to the same String object.

I found these facts in the enthuware answers ! But I just can't understand what it means , can somebody explain please !    
 
Greenhorn
Posts: 2
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this stackoverflow post about string literals
Some of the posted answers explain the difference between these two statements:

To clear up your confusion a little bit from your post, if you create two string variables
that have the same literal, they will be referenced by the same object even if they are in different classes and/or packages.

Now lets say we want to create another variable that contains the string literal from the first statement above.

The variable s and x are now referencing the same memory location while the variable t does not since we created a new object for the string literal.

When you check the equality of the three you should get the following results.


I hope this helps!


 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lilou Laure wrote:I found these facts in the enthuware answers ! But I just can't understand what it means , can somebody explain please !


It simply means that the following code snippet will print true
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah okok ! thanks roel and elliot for your answers !
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic