This question already has answers here:
How can I get the memory location of a object in java?
(4 answers)
Closed 2 years ago.
described above. for example.
String s = "abc";
System.out.println(s); // this method will output the string, not the address
so how to view the address, thanks in advance.
Java has no concept of "address", so it is impossible to get the address of any object, including strings.
if u want an answer as u mentioned in the comment, Actually there is a way to do it that way.
String s = new String("abc");
System.out.println(Integer.toHexString(s.hashCode()));
this will return
1ae66
This answer was historically accepted as correct and will only work for classes that didn't override the hashCode() method
Related
This question already has answers here:
String variable interpolation Java [duplicate]
(5 answers)
Closed 1 year ago.
Is there any way to join a string and another variable together, that is also a string, without using the "+" operator. Specifically one similar to this one that is done in C#.
string str = $"Hello {userName}. Today is {date}.";
If there is any way to achieve a similar or same outcome in Java please let me know.
See https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
String.format("Hello %s. Today is %s.", userName, date);
This question already has answers here:
Immutability of Strings in Java
(26 answers)
Closed 3 years ago.
I realize that the hashCode of the variable name, is different after the "update", but objectively what makes a String object in fact immutable ?
public static void main(String[] args) {
String str = "AB";
System.out.println(str ); // AB
str = str .replace(str .charAt(0) ,'W');
System.out.println(str );//WB
}
EDIT 1 : The hashCode is based on the value of the variable and have no relation with memory adress.
EDIT 2 : I now understand that Strings are references and not Objects in it self.
I read back all the answers for this same question and found out good answers in topics like [this] (Immutability of Strings in Java). Thank you whos tried to help me and my excuses for any silly misunderstood.
I also recommend this articles here to who wants better understand how Strings works in Java :
https://www.pushkarrajpujari.com/article/strings-in-java/
and how references works :
https://javaranch.com/campfire/StoryPassBy.jsp
EDIT 3: I cannot DELETE this topic anymore, according with Stackoverflow "You cannot delete this question as others have invested time and effort into answering it." which I agree.
If you look at the documentation of replace(), it mentions:
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
Therefore, the replaced String is an entirely new String.
This question already has answers here:
Questions about Java's String pool [duplicate]
(7 answers)
Closed 8 years ago.
Actually I am bit confused that how many object created in below "code processing".
String s=new String("A");
s=s+"B";
Actually someone said that here 4 objects will be created but in whole processing but how not understand.
Please anyone can give me detail description also included memory part such string pool etc.
The first String created is literal "A", that is, if not interned
prior.
The second String is the instance generated by the new keyword.
The third one is literal "B", again, if not interned prior.
The last one is the concatenation of s and "B".
You have two String literal Objects, namely "A" and "B". Then you explicitly instantiate a new instance of "A" with new String("A");. Finally, the fourth instance is created when you perform the String concatenation s+"B"
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
i am trying to do a presence check in java on 10 JTextFields. I want it so that if all 10 of my textfield have something in them, it will do my code.
String input1 = tfQ1.getText();
String input2 = tfQ2.getText();
etc.
I have put
IF(input1==("")&&input2==("")&&input3==("")&&input4==("")&&input5==("")&&input6==("")&&input7==("")&&input8==("")&&input9==("")&&input10==(""))
{
//DO SCORES ETC
}
However, this doesnt do anything... (my button does not work weather there are things in the text fields or not)
Please and someone help with presence check validation? Thanks =)
instead of "==" operator you should use
input1.equals("")
if(input.equals("WhateverYouAreLookingFor")) {
//do this
}else {
//do this
}
== is a reference comparison, both objects point to the same location in memory. essentially it tests wether the two operands refer to the same object.
.equals() will only compare what is in the String. It can be overridden so two distinct objects can still be equal.
This question already has answers here:
Difference between casting to String and String.valueOf
(8 answers)
Closed 9 years ago.
This morning I found an interesting question --- cast object to string check if valid and I found there are two types of answers. One is to cast an object to String, the other one is to get the string representation of that object instead (eg. using String.valueOf() or toString()). My questions are: what is the best practice? what is the difference between them?
Before I asked this questions, I found several existing questions which are relevant, but I didn't find one which answers my question. Please forgive me if I missed the important one and hope you don't mind pointing me to the answers.
Thank you,
If the Object is not a String, a cast will throw a ClassCastException at runtime. For example:
Object o = new Object();
String s = (String) o; //Exception here
The difference between the other two solutions (toString vs. String.valueOf) is in the case of a null object. toString will throw an exception whereas String.valueOf() will simply return "null":
Object o = null;
String s = String.valueOf(o); //s = "null";
String t = o.toString(); //NullPointerException