my dog learned polymorphism*
The moose likes Beginning Java and the fly likes change given string literal value to another value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "change given string literal value to another value " Watch "change given string literal value to another value " New topic
Author

change given string literal value to another value

mikali singh
Greenhorn

Joined: Jan 22, 2013
Posts: 12
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.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2849
    
  11

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.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

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).


OCPJP
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

What is wrong with



Technically, neither string has changed. You are now simply pointing the reference variable s to a new, different string.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: change given string literal value to another value