why is the most simple thing not working?
This may be stupid but why does the if-block not execute? Both of the strings should be the same. Either I am seriously missing something or something has gone fatally wrong.
Code:
String test = "blah552";
String words = test.replaceAll("[0-9]","");
System.out.print(words);
if(words == "blah")
System.out.print("It worked");
Re: why is the most simple thing not working?
Don't compare strings with ==, instead use the .equals() method.
Re: why is the most simple thing not working?
Because words and "blah" are two different references of two different objects.
Re: why is the most simple thing not working?
because you used == instead of equals.
use if(words.equals("blah")) instead and it should work.
Re: why is the most simple thing not working?
Quote:
why does the if-block not execute?
(I think this is the correct way of posing the question. It is precisely the simplicity that would suggest the code is working correctly.)
When you compare two expressions with == what is compared is the value of the expressions. In the case of words and "blah" these values are reference values and quite often we can have different reference values (references to different objects) but we still want to consider the objects they reference as the "same" in some sense. In this case words and "blah" have different values (refer to different things), but we would expect that the two different things the values refer to are made up of the same characters in the same order.
The solution in such cases is to use the equals() method. Every class has an equals() method and its job is to report when the (possible distinct) instances can be considered equal. Each class defines equality by its own lights. In the case of String, equality is defined to be "the same characters in the same order".
So try
Code:
if(words.equals("blah"))
This point applies to any class, not just String.
Re: why is the most simple thing not working?
Thank you so much. I have spent about a year programing in c++ and never had this problem.
I need to stop running into new languages without relearning the basics first.
Re: why is the most simple thing not working?
Quote:
Originally Posted by
pbrockway2
(I think this is the correct way of posing the question. It is precisely the simplicity that would suggest the code is working correctly.)
When you compare two expressions with == what is compared is the value of the expressions. In the case of words and "blah" these values are reference values
Isn't "blah" anonimous object of String type?
Re: why is the most simple thing not working?
Quote:
Originally Posted by
diamonddragon
Isn't "blah" anonimous object of String type?
I have heard of anonymous classes but not anonymous objects. No, blah is nothing but a String object pure and simple.
Re: why is the most simple thing not working?
Quote:
Originally Posted by
Fubarable
I have heard of anonymous classes but not anonymous objects. No, blah is nothing but a String object pure and simple.
I'v read somewhere that instance of class that is not referenced to any variable is called anonymous object.
Say:
Re: why is the most simple thing not working?
Quote:
Originally Posted by
diamonddragon
Isn't "blah" anonimous object of String type?
<jargon>
"blah" is a string literal. As Fubarable points out there is no such thing as an anonymous object defined in the Java language.
In the context of the OP's question (using "blah" in connection with ==), "blah" is also an expression as is words. More particularly "blah" is a constant expression.
As I said before == compares the values of these expressions. In this context == and != are said to be reference equality operators. The behaviour of reference equality operators is described in a way which anticipates what the OP observed: "While == may be used to compare references of type String, such an equality test determines whether or not the two operands refer to the same String object. The result is false if the operands are distinct String objects, even if they contain the same sequence of characters. The contents of two strings s and t can be tested for equality by the method invocation s.equals(t)".
</jargon>
Quote:
I have spent about a year programing in c++ and never had this problem.
You can think of reference values (the values of the expressions "blah" and words) as pointers. There is, however, no pointer arithmetic: all you can do with these pointers is dereference them with dot - eg, "blah".length() or words.length() - or compare them with == or !=. Much the same thing would occur in C/C++ if you compared two char* for equality. The pointers may well compare as different even if the memory pointed to consisted of the same sequence of char values.
Re: why is the most simple thing not working?
So, does it means "blah" is String instance and, at the same time, reference to itself?
Re: why is the most simple thing not working?
Quote:
Originally Posted by
diamonddragon
So, does it means "blah" is String instance and, at the same time, reference to itself?
You, me, and everybody else can think of and talk about "blah" as a String instance, or as a String object. But that's not how the JLS defines things. (the fact that the JLS does not follow the casual speech of the rest of us is one reason why my post marked its "wisdom" as <jargon>).
The way things are actually defined is that those six characters - quote-b-l-a-h-quote - constitute a constant expression. This expression has a value which is used with the reference equality operators. The value of the expression is a reference to a String object or instance.
So, no, "blah" is not a String instance: it is a String literal, a constant expression, whose value is a reference to a String instance.
The JLS guarantees that that quote-b-l-a-h-quote will always refer to the same String instance. (see my link before to what a String literal is) And that's a good thing. Because it means we can say "blah" is "a String instance" instead of the more ponderous "a constant expression whose value is a reference to a String instance" with out getting into trouble. There is just one unique String instance associated with the 6 characters "blah" appearing like that in some source code, even if there may be lots of String instances with exactly that sequence of 4 characters.
-----
Programming in Java is rather being like the prisoners in Plato's cave: primitive values aside, we are always dealing with references to things and never with the things themselves. We behave, as Socrates suggested we might, by speaking of the shadowy references as if they were actual instances. It turns out that Plato was wrong - at least in this context - we don't need to be concerned with the instances themselves (what he called "forms") it is enough to know the behaviour of the references.
Re: why is the most simple thing not working?
Re: why is the most simple thing not working?
Just one more question.
Is ok if say:
"blah" is a String literal of the String reference type?
"blah" is a constant expression which reference value is reference to String instance?
Re: why is the most simple thing not working?
Quote:
Originally Posted by
pbrockway2
Programming in Java is rather being like the prisoners in Plato's cave: primitive values aside, we are always dealing with references to things and never with the things themselves. We behave, as Socrates suggested we might, by speaking of the shadowy references as if they were actual instances. It turns out that Plato was wrong - at least in this context - we don't need to be concerned with the instances themselves (what he called "forms") it is enough to know the behaviour of the references.
If instances are "forms", does it mean references are "essences"?
Re: why is the most simple thing not working?
Quote:
What is difference between reference type and reference value in "blah" case?
The reference type of "blah" is String. (again that is documented at JLS 3.10.5 which I linked to before.)
The value of the expression/literal "blah" is a pointer to an object which is an instance of String. This is quite general, not just for String 4.3.1 Objects: "An object is a class instance or an array. The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object".
In summary "blah" is of type String and has a value which is a pointer to an instance of String. We know *lots* about the reference value: we know what will happen if we send it to System.out.println(), we know what we will get back if we call length() with it, etc. But we know precious little about the instance itself, only what we can determine with the reference equality operators (eg the OP learnt that it is not the same as the instance pointed to by words).
Re: why is the most simple thing not working?
Quote:
Originally Posted by
diamonddragon
If instances are "forms", does it mean references are "essences"?
No, references are the "appearance" of things in a Java program.
It might have been a distraction to drag forms into it. For Plato there was a reality standing behind the appearances (and he thought it had to do with forms), just as you can think of instances as the real things to which references refer.
(Essences are a whole other kettle of fish. Opinion was, and remains, divided as to whether the essence of a thing comes from its relationship to forms or whether it is a real, physical, aspect of the thing's substance. Nothing to do with Java. And, quite possibly, nothing to do with anything.)
Re: why is the most simple thing not working?
Quote:
Originally Posted by
diamonddragon
Just one more question.
Is ok if say:
"blah" is a String literal of the String reference type?
"blah" is a constant expression which reference value is reference to String instance?
This is the edited version of your last question... :)
Yes. Perfectly OK. (... a constant expression whose reference value is a reference ...).
Indeed, among friends - and we are all friends here - it's quite OK to say that "blah" is a String, or an instance of String, or a String object (as Fubarable did).
Re: why is the most simple thing not working?
So, if we have class instance, and no reference variable, we still have reference(pointer) to that object?
Re: why is the most simple thing not working?
Yes, you don't need a variable to have a pointer. "blah".length() is an example.
Another would be "blah".split("a")[0]. Not only is "blah" acting as a reference (*), but so is "blah".split("a"). And not a variable in sight!
(*) Of course I mean "is an expression whose value is a reference..."
-----
I'm not quite sure what you mean by a "class instance". But, yet another example would be System.out which is a reference (pointer) to an instance of PrintStream.