This question already has answers here:
What is the difference between "text" and new String("text")?
(13 answers)
How do I compare strings in Java?
(23 answers)
Closed 3 years ago.
What is the difference between creating Object, for example in my code, type Obj with operator "new" and Object type String?
public class Objs {
int a;
public Objs(int a)
{
this.a = a;
}
public static void main(String[] args)
{
String str = new String("Hello");
String str1 = str; // (str1 == str) == true
str += ", world!!"; // after this (str1 == str) == false - Why?
Objs o = new Objs(4);
Objs o1 = o; //(o == o1) == true
o.a += 9; // after this (o == o1) == true also
}
}
Why after I'm changing value of "str", references "str" and "str1" become not equal, but if I'm doing the same with class Obj references stay equal?
Here:
str += ", world!!"
you're creating a new object with changed value (that's how + operator works for Strings in java). Here:
o.a += 9;
you're modifying the field of the object, but it's still the same reference.
Because a String type in Java is immutable that means a string object can not be modified, instead, a new string object is created with the given expression.
str += ", world!!"; // at this line, a new str object is created with given concatenation, hence str loses reference to the previous object and now points to the new one - that is the reason.
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
Im trying to find if the first two letters of a string are "hi". If it does it shold return true, and if not it should return false. I used substring to find the values of the given string, but when the condition comes up true it isn't returning true. I must not be understanding something about Java, which Im new to.
Here is my code:
class Main {
public boolean startHi(String str) {
String str1 = str.substring(0,1);
String str2 = str.substring(1,2);
if(str1=="h" && str2=="i"){
return true;
}
if(str!="hi" || str.length()<=2){
return false;
}
else{
return false;
}
}
public static void main(String[] args) {
System.out.println(new Main().startHi("hi ho"));
System.out.println(new Main().startHi("hi"));
System.out.println(new Main().startHi("howhi"));
}
}
The string starts with "hi" and it sees that, but it returns false.
You could use String.startsWith(String prefix)
public boolean startHi(String str) {
return str.startsWith("hi");
}
So after all you probably don't need your own startHi() method, but can use standard Java API.
It's not returning true because you have to compare strings with the equals() method.
if("h".equals(str1) && "i".equals(str2)){
return true;
}
If you use == to compare objects it will check if it's the same object so it checks if the memory addresses of the objects are the same.
The string class overrides the equals() method to check for content.
If you're creating a string like this
String s1 = "Hi";
Java will put "Hi" in the so called string literal pool
so if you are creating a second string
String s2 = "Hi";
Java will not create a second Object but will refer to the "Hi" in the string literal pool.
Now you could do compare the two strings like s1 == s2 and it would be true
because the two references s1 and s2 point to the same object.
But what the substring() method does is new String("xyz") and if you declare a string like this a new object will be created and the comparison with == will return false because the two references obviously don't point on the same object.
Try this...
if(str1.equals("h") && str2.equals("i")) //equals use instead of (==) Operator.
instead Of
if(str1 =="h" && str2 == "i")
OR
if(str.startsWith("hi") // this will also works
This question already has answers here:
String.equals versus == [duplicate]
(20 answers)
Closed 7 years ago.
I was reading about == operator in java and found that it used to compare memory reference the below example is from given link.
String obj1 = new String("xyz");
// now obj2 and obj1 reference the same place in memory
String obj2 = obj1;
if(obj1 == obj2)
System.out.printlln("obj1==obj2 is TRUE");
else
System.out.println("obj1==obj2 is FALSE");
Note in the code above that obj2 and obj1 both reference the same
place in memory because of this line: “String obj2 = obj1;”. And
because the “==” compares the memory reference for each object, it
will return true. And, the output of the code above will be:
After that I write code randomly to check == operator but why it returning true in this example?
String obj1 = "ABC";
String obj2 = "ABC";
if(obj1 == obj2)
System.out.println("obj1==obj2 is TRUE");
else
System.out.println("obj1==obj2 is FALSE");
Does "ABC" string saved in the one memory place then obj1 and obj2 sharing that memory reference?
Even int also returning true.
int obj1=3;
int obj2=3;
Strings are a bit special as they use String interning.
So yes, behind the screens those two strings have the same memory reference (but do not count on it for string comparison. See this question).
Replace your strings by
Object obj1 = new Object();
Object obj2 = new Object();
and you will get the expected output.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
public class llearning1 {
public static void main(String[] args) {
String text = "is";
String x = "what is good";
String y[] = x.split(" ");
for (String temp: y) {
if (temp == text) {
System.out.println("found");
} else {
System.out.println("nothing");
}
}
}
}
output:
expected : code should display "found"
but it is displaying "nothing"
Compare the String with equals() method not with == operator
== operator is used to compares the reference of the object.
change if (temp == text) to if (temp.equals(text))
String is Object and object equality checks with .equals() method.
so try:
if(temp.equals(text))
== operator is used for object reference comparison means two reference is pointing to the same object or not or primitive(int, double, ...) value comparison.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
I probably made a logical error somewhere but I do not know where.
The output is always FALSE even though the condition seem to be TRUE
public class Test {
public static void main(String[] args) {
String str1 ="Hello world";
String str2 ="Hello world";
if (checkSubstring(str1,str2)){
System.out.println("Cool");
}
else
System.out.println("Not cool");
}
static boolean checkSubstring(String str1, String str2) {
String s1 = str1;
String s2 = str2;
if (s1.substring(4)== s2.substring(4)){
return true;
}
else
return false;
}
}
You should always use equals method to test for the content of string.
== operator checks whether two reference are pointing to same object or different ones. And since s1.substring() and s2.substring() will generate two different string objects, so comparing their reference will give you false boolean value.
So, in checkSubstring method, you should compare your substring like this: -
if (s1.substring(4).equals(s2.substring(4))) {
return true;
} else {
return false;
}
s1.substring(4)== s2.substring(4)
In the above Change it to s1.substring(4).equals( s2.substring(4))
- Objects in Java are compared using the equals() method.
- As String is an Object in Java, it must be treated in the same way.
- Moreover if you are trying to compare 2 String irrespective of their Case, then use equalsIgnoreCase().
- "==" is used for comparing the primitive types, and also to check whether 2 Object Reference Variables are pointing on the same object on the heap or not.
Eg:
Dog d1 = new Dog();
Dog d2 = d1;
if (d1 == d2){}
you need equals function
static boolean checkSubstring(String str1, String str2) {
String s1 = str1;
String s2 = str2;
if (s1.substring(4).equals(s2.substring(4))){
return true;
}
else
return false;
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java String.equals versus ==
public class S_eaqual {
public static void main(String[] args) {
String s1 = "one", s2 = "two";
if (s1 + s2 == "onetwo") {
System.out.println("Yes..equal");
}
}
}
This type of comparison shows errors. Is this not the right way of comparing strings?
Two String objects can be compared using == operator. So why this is showing error?
String should be compared using equals method.
String s1 = "one", s2 = "two";
if("onetwo".equals(s1+s2)) {
System.out.println("Yes..equal");
}
Try this...
String s3 = s1 + s2
if(s3.equals("onetwo")) {
...
== compares if they refer to the same object, and the result of s1+s2 isn't in this case, and the .equals() method on string compares that the values are the same. In general, you only use == for primitive value comparisons. Although you can do it for objects iff you intend to check to make sure that two references point to the same object.
use (s1+s2).equals("onetwo"); instead
Use equals:
if (s1.concat(s2).equals("onetwo")) System.out.println("Yes..equal");