This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
I'm new to programming, and I'm using java. Here's a program I wrote:
class HelloApp {
static String hi;
public static void main(String[] args) {
int length = args.length;
if (length > 0) {
hi = args[0];
sayHi();
}
}
static void sayHi() {
if (hi == "hello") {
System.out.println("Hello!");
}
}
}
My question is: Why doesn't inputting "java HelloApp hello" make "Hello!" appear on the next line?
if (hi == "hello") {
should be:
if (hi.equals("hello")) {
== checks if the two references are pointing to same object in the heap, while equals() checks if the two String references are pointing to objects that having the same String value.
you are comparing strings in a wrong way...
How do I compare strings in Java?
static void sayHi() {
if (hi.equals("hello")) {
System.out.println("Hello!");
}
}
this should solve your problem..
== tests reference equality, not string equality. You want to use .equals instead:
if (hi.equals("hello")) {
...
}
You are comparing Strings incorrectly. The proper way to check for string equality is via str.equals(anotherStr), or str.equalsIgnoreCase(anotherStr). The == operator checks if the strings point to the same memory location, not the contents.
as neilvillareal said you can make use of str.equals(anotherStr), or str.equalsIgnoreCase(anotherStr) methods,
Please refer following link for understanding difference between '==' & 'equals/equalsIgnoreCase'method : http://www.java-samples.com/showtutorial.php?tutorialid=221
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
The answer should be good but it always gives output bad, I have tried everything
Here is the code :
import java.util.*;
public class stringuses {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=new String("i am ayush");
System.out.println("Who am i ?");
String ans=sc.nextLine();
if(str==ans)
{
System.out.println("good");
}
else
{
System.out.println("bad");
}
}
}
You should not use == to compare Strings in Java because that compares the in-memory reference of the two String objects, not their values. Use the equals() method of the String object instead. So in your code, the usage of the equals() method to compare the str and ans Strings would be,
str.equals(ans)
Hope this helps you! If it did, please mark my answer as "Accepted". Good day!
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:
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:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have java syntax. Sory I'm still not figure out why this happen.
public class TestString {
public static void main(String [] args){
int i=-1;
String a=((Object)i).toString();
if(a=="-1"){
System.out.println("Same");
}else{
System.out.println("Not");
}
}
}
And then the result is "Not" what the problems why -1 string different with -1 int in object?
You have to use .equals() on string to compare.
String's equal method overridden in such a way.
Try
public class TestString {
public static void main(String [] args){
int i=-1;
String a=((Object)i).toString();
if(a.equals("-1")){
System.out.println("Same");
}else{
System.out.println("Not");
}
}
}
Strings have to be compared with the .equals() method.
You are using reference equality instead of object equality.
Since "-1" in the literal table has a different heap address than a newly allocated a, it returns false.
If for some reason you find yourself allocating a lot of String objects that share the same value(s) and want to test them by reference instead, consider using String#intern():
final String a = [...].intern(); // allocated
if (a == SOME_INTERNED_STRING_1) {
[...]
else if (a == SOME_INTERNED_STRING_2) {
[...]
}
==
compares reference which is not same in your case so if you want to compare string values then use
.equals() method
The == operator checks to see if two objects are exactly the same object . You can go for equals method to check if the value are equal or not
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");