Using existing fields to name a new object - Java [duplicate] - java

This question already has answers here:
Naming a new object after a string?
(5 answers)
Closed 9 years ago.
How do I use an existing field to name another object/field?
String test = "test";
String 2;
I want the variable name of string 2 to be test2. How do I import the name of the first string into the name of the 2nd one?

I want the variable name of string 2 to be test2.
Then name it test2. Type: "String test2" into your IDE.
How do I import the name of the first string into the name of the 2nd one?
That's completely impossible. Variables names are determined at compiled time and can not be dynamic based on the content of another variable.
What you might be looking for is Map<String, Object> that maps Strings to Objects, but it's unclear from your question.

Impossible with java, especially if you are using local variables. You cannot define variables given a name stored in a string, because there would thereafter be no way to refer to them.

Related

Why String addition using + allows moved to new address in java? [duplicate]

This question already has answers here:
String can't change. But int, char can change
(7 answers)
Closed 5 years ago.
I have got some information that string addition using + allows moved to new memory address in java but in case for int it is not happening why?
means
String a="fi-rstname";
a=a+" "+lastname;
a+" "+lastname moves to the new address instead of address of a
but for
int a=22;
a=a+2323;
a+2323; vale does not move to the new address for addition why?
String is immutable in Java. You can't change the content of immutable field after initialization. That is the reason, you create a new memory case.
You can get more information in this post : String is immutable. What exactly is the meaning? that explain well why String is immutable.
Because String is immutable so each time you make change in string it'll create new object
in int we can make changes because primitive datatype is mutable(changeable).

Understanding in creation of String literals in Java [duplicate]

This question already has answers here:
How can a string be initialized using " "?
(10 answers)
Closed 6 years ago.
I am new to stackoverflow which I am finding it very useful, thanks so much for a great community. While I've been understanding about Strings in java, I just wanted to know how are Strings literals source created? I understood most of the concept of Strings in java. It is a special kind of class that is final, I also understood when creating a String literal, an object will be created on the heap and its reference will be in the string constant pool. by using this.
String name = "Manchester";
I also understood that when a string is created using new operator, the reference will have the object on the heap and a reference in the string constant pool. By this way.
String name = new String("United");
As I did understand how the object was created on the heap using the new operator, what I didnt understand is how the object is created on the heap when we use double quotes " ". How is this accomplished? I went thought the source code of String.class too but it wasn't of any help for me.
Can anyone please let me know how this works? Thanks.
String name = "Manchester";
will search in constant pool of string(in heap) for the same valued object and if found will use that, if not will do this :
String name = new String("Manchester");
Do note that String is immutable, so it tries to use already present objects rater than creating one.
You can check it in this way:
String x = "a";
String y = "a";
x==y //true
And
String x = new String("a");
String y = new String("a");
x==y //false

how am i able to change value of same String? [duplicate]

This question already has answers here:
Java String Immutability and Using same string value to create a new string
(4 answers)
Closed 7 years ago.
i know Strings are immutable in java and i just read some theory about it
Once a String is declared it cannot be changed . i declared a String and changed it's value just by appending some value to it . it should not have happened
String s = "amol";
s = s + "abc";
System.out.println(s); //output is amolabc
The String s didn't get changed. When you did s + "abc", it created a new String object with the result of the operation.
You need to understand the concept of String Pool in Java. Please go through the link
http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html
You are right that Strings are immutable. Here, You didn't change the String s. All distinct Strings are stored in the Heap Memory. So, all the variables (say 10 variables ) containing the same String (say "Hello" ) will occupy only 5 bytes of memory. They all will point to the same location. Separate instances will not be stored for each of those variables.
Here, when you write s = s + "abc", a new string "amolabc" is created in the heap,and now the variable s just points to the new string in the heap. You didn't modify the value of "amol". You just made a new String.
The meaning of immutable is not like you thought to be.Being immutable guarantees that hashcode will always the same, so that it can be cashed without worrying the changes.That means, there is no need to calculate hashcode every time it is used.
here you are appending another string value to it,which can be done.But you cannot concatenate with another string.
By appending it is creating another string.
Refer here

How to name a variable as value of another variable in java? [duplicate]

This question already has answers here:
Accessing the value of a variable by its name as string in Java
(2 answers)
Closed 8 years ago.
I want to give name to a variable same as value of some other variable. I am new to java and have no idea how to do it .
Suppose String s="stack"; now suppose I want to **name integer variable as stack using s **.How can I do it?my variable name should be stack.
Exactly as described it is impossible. The closest things coming to my mind is Map:
Map<String, Object> map = new HashMap<String, Object>();
//some code to fill it
String s = "stack";
Object value = map.get(s);
Two words: You can't. (In Java)
EDIT
No alternative which does the same but here is something which does similar.
What you said is possible in language like PHP as
$s = 'stack';
${$s} = 'hello there';
echo $stack; // hello there
But it is not possible in strongly typed langauge like Java, C# etc.

Strings are immutable! But I can still modify and get a new copy of the original String? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's the advantage of a String be Immutable?
Strings are Immutable?
String s = "PROJECT";
Here s cannot be modified. But String s2=s.toLowerCase() will return me a new string. Here still I can get a new copy of the original string! But if Java still wants immutability, then why not restrict the user while modifying the string (throw an exception or something). Then why immutability? Can any one explain why?
There's a little misconception:
s isn't immutable. s can easily be assigned a new value (i.e. another refence to another String object) unless it is final.
And just because a String instance is immutable doesn't mean that you can't make another String that derives its value from that first String instance but is slightly different.
In fact that's the only way how you can "modify" strings in Java: you can't change the content of any given String object, but you can create a copy that has modified content (that copy is then again immutable!).
Strings are immutable, meaning that this is always true:
String s = "PROJECT";
String s2 = s.toLowerCase();
System.out.println(s.equals("PROJECT")); // Prints true
System.out.println(s.equals(s2)); // Prints false
In comparison, consider what would happen if Strings were mutable.
MutableString ms = "PROJECT";
MutableString ms2 = ms.toLowerCase();
System.out.println(ms.equals("PROJECT")); // Prints false
System.out.println(ms.equals(ms2)); // Prints true
This example may seem trivial, but it means that unless you actually re-assign the reference s then you can guarantee that s will not be changed by any piece of code.

Categories