• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

change given string literal value to another value

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to change the given string literal value to another value using only one variable and without using string buffer and builder. for example-
String s= "hello dudes" to String s= "hello java DUDES beignners" using only one variable s.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Strings can't be changed. That's a good concept to drill into yourself early and often. However, you can assign a different String value to the variable s, which may be what you meant. I'm not clear on your assignment though. Are you supposed to transform the first String "hello dudes" to the second based on some rules? Keep in mind we won't do your homework for you, but if you're clearer about the problem, we can point you in the right direction.
 
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mikali singh wrote:how to change the given string literal value to another value using only one variable and without using string buffer and builder. for example-
String s= "hello dudes" to String s= "hello java DUDES beignners" using only one variable s.



Remove the second declaration of the "String s". you can directly assign a new value to variable "s" which was already defined (which will lose its previous reference to "hello dudes" from the constant pool).
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is wrong with



Technically, neither string has changed. You are now simply pointing the reference variable s to a new, different string.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic