equal 1 « Development « Java Data Type Q&A

1. The correct way of waiting for strings to become equal    stackoverflow.com

In a Swing app a method should continue only after user enters a correct answer. The correct answer is stored in a String with user answer being set by a listener ...

2. Java String.equals versus ==    stackoverflow.com

This code separates a string into tokens and stores them in an array of strings, and then compares a variable with the first home ... why isn't it working?

public static void ...

3. Java string comparison?    stackoverflow.com

String parts is String[6]:

[231, CA-California, Sacramento-155328, aleee, Customer Service Clerk, Alegra Keith.doc.txt]
But when I compare parts[0] with "231":
"231" == parts[0]
the above result is false, I'm confused, so could anybody tell me why? ...

4. Compare two Java objects with a check for null    stackoverflow.com

I feel like such a novice for asking this question, but is there a method in the JDK that compares two objects for equality, accounting for nulls? Something like this:

public ...

5. ObjectUtils.equal(x,y) null can work    stackoverflow.com

ObjectUtils.equal(x,y) x if is null can work?

6. Checking to see if a string is a palindrome with String.equals()    stackoverflow.com

I had a question regarding a basic program I was writing said whether a word such as racecar is a palindrome or not. All my methods which reverse the string, strip ...

7. Equivalent of Java's IndexOf() in Scheme?    stackoverflow.com

I am developing a small program in Scheme but I got stuck. Is there anything similar to Java's indexOf() that I could use in Scheme?

8. Comparing StringBuffer content with equals    stackoverflow.com

StringBuffer sb1 = new StringBuffer("Java");
StringBuffer sb2 = new StringBuffer("Java");
System.out.println(sb1 == sb2);
Here both are returning false. How is it possible?

9. How to check if two objects are equal even if they could be null    stackoverflow.com

Sometimes, I see this:

if (a.equals(b)) do(something);
However, if a is null, a NullPointerException is thrown. Assuming when a==null and b==null or if just a==b that I would want to do(something). ...

10. Is "long x = 1/2" equal to 1 or 0, and why?    stackoverflow.com

if I have something like:

long x = 1/2;
shouldn't this be rounded up to 1? When I print it on the screen it say 0.

11. what is c/c++ equivalent way of doing '>>>' as in java (unsigned right shift)    stackoverflow.com

I wanted to know how i can do '>>>' shifting of java in c/c++. thanks

12. How to check if my string is equal to null?    stackoverflow.com

I want to perform some action ONLY IF my string has a meaningful value. So, I tried this.

if (!myString.equals("")) {
and this
if (!myString.equals(null)) {
and this
if ( (!myString.equals("")) && (!myString.equals(null))) {
and this
if ( ...

13. Java: equivalent to C's strnicmp? (both startsWith and ignoreCase)    stackoverflow.com

String string1 = "abCdefGhijklMnopQrstuvwYz"; String string2 = "ABC"; I had been using string1.startsWith(string2), which would return false in the above example, but now I need to ignore case ...

14. Is it a bad idea if equals(null) throws NullPointerException instead?    stackoverflow.com

The contract of equals with regards to null, is as follows:

For any non-null reference value x, x.equals(null) should return false.
This is rather peculiar, because if o1 != ...

15. What is the java equivalent to javascript's String.fromCharCode?    stackoverflow.com

What it the java equivalent of javascript's:

String.fromCharCode(n1, n2, ..., nX)

16. Strings in Java : equals vs ==    stackoverflow.com

  String s1 = "andrei";
  String s2 = "andrei";

  String s3 = s2.toString();

  System.out.println((s1==s2) + " " + (s2==s3));
Giving the following code why is the second comparison ...

17. Should I use string.isEmpty() or "".equals(string)?    stackoverflow.com

The title basically says it all. I'm usually testing this alongside a string == null, so I'm not really concerned about a null-safe test. Which should I use?

String s = /* ...

18. Java equals() ordering    stackoverflow.com

If I try to do a .equals() on a null string in java, a null pointer exception will be thrown. I am wondering, if I am trying to compare if a ...

19. Demonstrating string comparison with Java    stackoverflow.com

I want to demonstrate with a few line of code that in Java, that to compare two strings (String), you have to use equals() instead of the operator ==. Here is something ...

20. Using '==' instead of .equals for Java strings    stackoverflow.com

I know this has been asked before, but in spite of recommendations to use .equals() instead of the == comparison operator, I found that == works all the time:

String s1 ...

21. Question about == operator in java    stackoverflow.com

public class Demo {  

    public static void main(String[] args) {

        String s1 = "Hello";

22. String comparison and String interning in Java    stackoverflow.com

When should one compare Strings as objects and when should one use their equals method? To make sure, I always use equals, but that doesn't seem very efficient. In what situations ...

23. equal() and equalsIgnoreCase() return false for equal strings    stackoverflow.com

I'm working with eclipse IDE (Version: 3.4.2) on a mac and I have met the following issue. When comparing between strings using equal() or equalsIgnoreCase() methods I receive false even when the ...

24. Java null check why use == instead of .equals()    stackoverflow.com

In Java I am told that when doing a null check one should use == instead of .equals(). What are the reasons for this?

25. Comparing two java objects on fly (data type not known)    stackoverflow.com

I need to compare different data objects. Can any one tell me how can i do this. I don't know what are the data types i will get priorly. If i need ...

26. Difference between isEmpty and equals("")    stackoverflow.com

Possible Duplicate:
Should I use string.isEmpty() or “”.equals(string)?
I am wondering, what difference is there between isEmpty and equals("")? They would appear to be the exact ...

27. In JDK 1.6, can String equals operation can be replaced with ==?    stackoverflow.com

As I study the source code of some open source products, I find code like:

if (a=="cluser")
a is a String variable. Can a String equals operation be replaced with ==?

28. String .equals opposite    stackoverflow.com

I have this line of code:

if(command.equals("e") || command.equals("d")) {

29. Equal strings aren't equal (==) in Java?    stackoverflow.com

Possible Duplicate:
Strings in Java : equals vs ==
Hi guys, why is this Java code not working correctly (the if statement is always being read ...

30. Problem with equals Strings in Java    stackoverflow.com

I have two "equal" Strings. When I print both Strings they look exactly the same in the screen. But when I compare the Strings the result is "false" and using .length ...

31. null and the symmetry of equals    stackoverflow.com

Equality is supposed to be symmetric, right?

Object someObject = new Object();
Object NULL = null;

NULL.equals(someObject) => NullPointerException
someObject.equals(NULL) => false
What is the rationale for not having the second form throw a NullPointerException?

32. Difference between String.isEmpty() and String.equals("")    stackoverflow.com

I created a "Color Chooser" with three textboxes where the user defines rgb values.
To check if values entered are correct (only numbers between 0-255) I'm using the following:

public Color getColor() {

33. String is not equal to string?    stackoverflow.com

String[] letters  = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "L"};

Scanner inp = new Scanner(System.in);
String input = (inp.nextLine());
String[] cord = input.split("");

for(int x = 0; x < 10; x++)

34. Distinguishing Equivalent DataTypes    stackoverflow.com

In Java, suppose I have a method that takes in two lists of integers, and returns an arrayList of integers from the first list, based upon the second list.

public ArrayList<Integer> func(ArrayList<Integer> ...

35. equivalent of + in StringBuffer    stackoverflow.com

I have been using String to do the following.

String x = "a";
String y = "b";
String z = y+""+x;
x = y;
y = z;
So the final values of x and y are b ...

36. I dont understand this ("string" == "string") example    stackoverflow.com

I found this java code on a java tutorial page:

if ("progress" == evt.getPropertyName())
http://download.oracle.com/javase/tutorial/uiswing/examples/components/index.html How could this work? I tought we HAVE TO use the equals() method for this situation (string.equals("bla"))? ...

37. Comparing two strings with "==": when will it work?    stackoverflow.com

Say you have three strings,

String s1 = "string one";
String s2 = new String("string one");
String s3 = "string one";
I know it is true that s1 == s2 is false, but I read ...

38. Check if a string is equal or substring of another    stackoverflow.com

I am trying to check if one string is the same as another or maybe if it's a part of it with the code below:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class Comparison {


39. Why is myString.equals("aString"); different from "aString".equals(myString);?    stackoverflow.com

I heard several times that in using boolean equals(Object o) to compare Strings, it's better to put the constant on the left side of the function as in the following:

  • Bad: myString.equals("aString");
  • Good: ...

40. How do you check if a string is not equal to an object or other string value in java?    stackoverflow.com

I have been trying to make a clock that the user can set. I wanted the user to be asked questions and they answer in words like yes or no. I ...

41. How to prevent null check before equals    stackoverflow.com

I find stuff like this rather annoying and ugly in equals methods:

if (field == null)
    if (other.field != null)
        return false;
else ...

42. Whats the difference between using String.Equals(str1,str2) and str1 == str2?    stackoverflow.com

Possible Duplicate:
whats the difference between “.equals and ==”
Here is my code snippet :
String str1 = new String();

// This will print "Unequal"
if(str1 == "") {

43. simple string.equals working incorrectly - Java    stackoverflow.com

Been racking my brains with this for ages now and it's just stumped me. I have a simple string comparison:

public static void login() 
    isIncorrectInput = true;


44. how do I code to check if a string does not equal another string? a value?    bytes.com

I understand how to use the equals, and know that there is an option to use '!' for unequal check, but in my case, I am testing against a value in ...

45. equals(Object) of String    coderanch.com

Here is equals method in a class Foo, will it run faster if I use the hashcode of String at line return obj.equals(this.id) ? Public class Foo { private String id = ""; public boolean equals(Object obj) { if(obj == this) return true; if(obj == null || !(obj instanceof(Foo))) { return obj.equals(this.id); } } } [ March 07, 2005: Message edited ...

46. why equals() not overridden in StringBuffer    coderanch.com

String is considered one of the 'Value' classes (not sure if that's my term or if I picked it up somewhere). This includes all the primitive wrapper classes and String. They share some characteristics. They are all immutible. They all override equals, hashCode, and toString. I think they also have some other common methods. To my knowledge no other class in ...

47. Object.equals() vs String.equals()    coderanch.com

In you downloaded the SDK from Sun, it came with a zip file of source code, src.zip. By all means unzip it and poke though it! In it you'll find that Object's equals method is defined as: public boolean equals(Object obj) { return (this == obj); } And String's equals method is defined as: public boolean equals(Object anObject) { if (this ...

48. equals() in StringBuffer    coderanch.com

49. problerm with string equals    coderanch.com

Want code? Here you go: This segmet of code is an intent to encode a non ASCII String into an ASCII one: public class ByteSeparatorEncoder implements SimpleStringEncoder { private final String separator = "e"; public String decodeComplex( String simpleString ) { String[] byteRepresentation = simpleString.split( separator ); byte[] bytes = new byte[ byteRepresentation.length ]; for( int i = 0 ; i ...

50. How to cut string into equal length peaces?    coderanch.com

MacTosh-- I'm unclear on the rules that apply to breaking up your input string, "~~this~~is~not~~easy~", where "~" stands for a space. So I'll proceed on the assumptions I've gleaned from answers to these questions: 1) Do the leading spaces (here, the two before "this") count in the length of the first word? 2) Does the first word end with the "s" ...

51. String s = new String().equals( . . . . )    coderanch.com

no. see code for equals method public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = count; if (n == anotherString.count) { char v1[] = value; char v2[] = anotherString.value; int i = offset; int j = anotherString.offset; while (n-- != 0) { if (v1[i++] != ...

52. obj != null and !(obj.equals(null))    coderanch.com

Hi, Are obj != null and !obj.equals(null) the same ? Sometimes I have the impression that obj != null doesn't work. When I replace the code with !obj.equals(null) it works. I have the impression that if the object is instantiated in the class itself I can use !=. If I call method that returns an object I have to use obj.equals(null) ...

53. String.equals() confusion    coderanch.com

Hi, I'm having a problem with Vector.contains() I can remember a LONG time back, when I was just a greenhorn, Cindy Glass and I had a discussion about this. But now it's jumped up and bit my behind again! String one = "hello world"; String a = "hello"; String b = "world"; String two = a + " " + b; ...

54. StringBuffer and equals()    coderanch.com

I think I've got the answer: posted February 24, 2001 10:00 AM -------------------------------------------------------------------------------- No, StringBuffer.equals() does not behave in the same way as String.equals(). In the javadoc for StringBuffer, you can see that it just inherits the default implementation (Object.equals()), and The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference ...

55. aString;equals(bString) on null-object    coderanch.com

It is true that the .equals method will throw a NPE when the object calling it is null; there is no real way around this (other than modifying the JVM somehow, as I inferr your intent from your post). However, it is possible (and even preferable) to write your own equals method outside the context of the object (i.e., make it ...

56. Why are same strings not equal    coderanch.com

The "==" operator compares two physical objects in memory to see if they occupy the same location in RAM -- i.e., to see if they are physically the same object. That won't be the case in your example; the two strings are being dynamically created by calling "replace", and each one will be a distinct object created just for that purpose. ...

57. Null and NullPointers with "==" and ".equals()"    coderanch.com

Hello ranchers: I am working on my understanding of Null initialization values and NullPointers. After declaring a new array object such as: String[] s = new String[10]; My understanding is that each element of the array is initialized at Null (because it is an array of String objects). Using the "==" comparison operator in the following code results in a "true" ...

59. difference between string equals.    coderanch.com

Consider the following: class TestString { public static void main(String[] args) { String s1 = "Apple"; String s2 = "Apple"; String s3 = new String("Apple"); String s4 = new String("Apple"); String s5 = s3; String s6 = "apple"; // compare bit patterns System.out.println(s1 == s2); // prints true System.out.println(s2 == s3); // prints false System.out.println(s4 == s3); // prints false System.out.println(s3 ...

60. null equals null?    coderanch.com

I'm not sure what the intent here is, since the code provided will not compile. However, it looks like you are adding arrays into a List. As far as I know, .equals() for arrays is the same as ==; it compares if the physical objects are the same. public class ArrayTest{ public static void main(String argv[]){ String[] a1 = new String[]{null, ...

61. string equals question    coderanch.com

62. Setting strings equal to each other    coderanch.com

Try this. public class StringBasics { public StringBasics() { } public static void main(String[] args) { new StringBasics().test(); } public void test(){ String s1 = new String("A");//not recommended use String s1 = "A" String s2 = new String("A");//not recommended use String s2 = "A" if(s1 == s2){ System.out.println("references are equal"); } if(s1.equals(s2)){ System.out.println("values are equal"); } s1 = s2; if(s1 == ...

63. String == and equals() doubt    coderanch.com

class Java_StudyNotes{ public static void main(String args[]){ String i="null"; String j=new String("null"); if (i==j)//i and j are object having same value(but different object ) so false System.out.println(i==j);//true else System.out.println(i!=j); //fasle if(i.equals(j))//i and j are objects having same value(no matter about identity)just sees equality System.out.println(i.equals(j));//true else System.out.println(!i.equals(j)); //fasle } } I thought it would output false true.But it gave true true.how? (i==j)---------I ...

64. thou shalt never be equal to null?    coderanch.com

Hello guys, Is it possible to check if something is equal to null? And receive a boolean answer? I'm pretty confused with this one, why I can have a value of null for an object and then right after can't I do boolean stuff on it? please check what I did: String userConfirmation = users.get(0).getConfirmationEmail(); ---> It is an object that ...

65. Can something equal null?    coderanch.com

I may be thinking unclearly, it is a tad early, but if i need to check a string to see if it is null, how would i go about doing this? My first guess was string.isNull(), but alas there is no method like that. So I went to my friend Google and everything I saw there said string==null. I guess I ...

66. Strings should be equal    coderanch.com

Hi there, I'm going a bit mad trying to find out what is wrong with my code and I'd be grateful for any help. Basically, I have a little program that should test my French vocabulary. I have got a list of vocab in an sql database. I retrieve this vocab and turn it into an ArrayList of VocabElement objects These ...

67. String "contains" equivalent - But should look for case-in-sensitive string    coderanch.com

Hi all, There is a String array.I have look for a string which would contain in each string of that array. Say one of the strings in my array is str and my searchPattern string is searchString. Normal str.contains(searchString) would look for a case-sensitive pattern. Do I have any API that would not consider case. Using Patterns,it can be done as ...

68. Equals VS indexOf    coderanch.com

Hey, I don't think it will make a lot of of difference, you could also use the matches method, I prefer using this as it allows you to add a REGEX which is I think is more powerful. so... String url = "www.mysite.com/News"; url.matches("News"); // true url.matches("News$"); // true as long as news is at the end of the String Also ...

70. Why StringBuffer not override equals??    coderanch.com

71. String comparison styles in equals    coderanch.com

Hi, please see the code below. There are two styles to override equals, both are working. Need an expert opinion on small detail: as name is String in BParent, should I prefer use String.equals() instead of "==" in order to compare key String values within Object's overridden equals method? I am aware of the String Pool but don't really get my ...

72. string.equals("");    coderanch.com

Hey guys. Im sorry if my question will be a little vague and "noob" but i have this problem ive been thinking for 2 hours and im tired,i hope maybe you can help again. I read a string from the keyboard and i use an if to determine what "word" is it. The if (xx.equals("exit"){ seems to be working fine,but the ...

73. overriding equals() for StringBuilder    coderanch.com

Hi, I wrote a piece of code which would add objects to a Set. I have overridden equals() and hashcode() ,so that no duplicate object gets added in the Set. Following is the object i am adding to the Set class MessageAndId { StringBuilder message; Integer id; public StringBuilder getMessage() { return message; } public void setMessage(StringBuilder message) { this.message = ...

74. Why does cons remain equal to null?    coderanch.com

Any idea what's wrong here? : import java.util.*; import java.io.*; import java.lang.*; public class TestThree { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print ("What's your name [DELETED]?"); String name = in.nextLine(); String firstName = in.next(); System.out.print("How old are you [DELETED]?"); int age = in.nextInt(); System.out.print("Hello, " + name + ", next year you will be " ...

76. String == vs equals    coderanch.com

77. comparing two values with equalsIgnoreCase and equals with NULL as first value.    coderanch.com

public class EqualIgnoreForNull { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String stuff =null; String nonstuff = "see"; if (stuff.equalsIgnoreCase(nonstuff)) { System.out.println("EQUAL"); } else{ System.out.println("different"); } String stuff1 =null; String nonstuff1 = ""; if (stuff1.equals(nonstuff1)) { System.out.println("EQUAL1"); } else{ System.out.println("different1"); } } } The above code returns me NULL POINTER EXCEPTION ,since ...

78. Trouble with String.equals( String) and ==    coderanch.com

I was working through Just Java 2 when I stumbled on a problem. Frankly, it is giving me a headache... Anyone with some idea what is going on? Now: String A="end"; String B="end"; if( A ==B ) /* whatever */; if( A.equals( B ) /* whatever */; ought to work the same, only they do not. Here is cut-and-paste of the ...

79. string comparison with "=" and ".equal"    java-forums.org

public class Test { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; //String s1 = new String("hello"); //String s2 = new String("hello"); if(s1 == s2) System.out.println("true"); } } and it printed true when i run the program. I thought s1 and s2 pointed at different memory address so it shouldn't have printed anything. on my ...

80. Using string.equals    forums.oracle.com

Blindly making members static is generally the wrong approach to fixing the "cannot access non-static" error. Instead, create an instance of the appropriate class and use that instance to access the non-static members and methods you're using. As for your new error, a variable exists only within the nearest enclosing pair of curly braces. This is called the scope of a ...

81. String Not Equals    forums.oracle.com

82. String equals, matches,, startsWith..endsWith...    forums.oracle.com

I got some problem and it is very weird for me ... For example: call method: toCompare("hello"); method: toCompare(String s) { String sa [] = //get all the filename from a file ...// for examle sa contain hello // loop if (sa.equals(s) ...){ // it actually return true // others matches ..startsWiths ..... } however ... when I call method: toCompare(s[0]); ...

83. Strings - is there a !equals ?    forums.oracle.com

Hey now, it's not as bad as what I did one day. I worked and worked and worked on some code but could not figure out why it wouldn't compile, finally I e-mail my teacher and when he replyed I was mashing many many buttons on my keyboard with my head. I had came straight out of Algebra II and evedently, ...

84. String.equals()    forums.oracle.com

85. equals on a null string    forums.oracle.com

On a side note, if you know for certain that pclass and pclass.name are both non-null, you can skip the initial null check, since x.equals(null) is always supposed to return false (and in particular, does so for String and all other API classes that I know of). Also note that if className is not supposed to be null--that is, if it's ...

86. Question: equals() for objects, not necessarily strings    forums.oracle.com

Because that's the way the equals() method for the Exp2 class (whatever it is; you didn't post it) was written (or not written). There's nothing magic about equals() methods. It's just a standard method with a standard signature, used to determine whether things are considered equal. Just because numeric values are used to initialize an object doesn't mean that the equals() ...

87. using equals on substring    forums.oracle.com

88. multiple string comparison with equals    forums.oracle.com

89. String.equals() throws error.    forums.oracle.com

90. String: not equals instead of equals???!!    forums.oracle.com

91. String equals() vs ==    forums.oracle.com

String a="Selva"; String b="Selva"; if(a==b) of course this above if loop will return true because value of two objects are string suppose if declare like this String a="String"; String b="Kumar" if(a==b) This loop return false because these two object value are different but you are saying == operator checking Object reference .these two object also belongs to same String class . ...

93. problem with string.equals    forums.oracle.com

94. Doubt in String Equals    forums.oracle.com

95. how to compare two strings to check they are equal    forums.oracle.com

Barman: "Sorry, we're out of beer, can I offer you something else" StringBuilder: "OK, I'll have a Scotch and soda." String: "Pint of beer for me, please." Barman: "Didn't you hear me, we're out of beer." String: "Pint of beer for me, please." Barman: "I said there's no beer!!" String: "Pint of beer for me, please."

96. Strings not equal if passed in from the command line?    forums.oracle.com

But if the contents of the string are the same are they therefore not the same object from the string pool?? Listen == will check if it is the same object. equals() will check the contents. When you compile that class how would the compiler know that the arg[0] passed in will be "alan"? it doesnt. arg[0] is a different String ...

98. String compare using S .equals() and ==    forums.oracle.com

When comparing strings, what is the different between using stringx.equals(stringy) and stringx == stringy? As others have said, equals compares contents (because String overrides it to do so) and == compares reference values--that is, whether both references point to the same object. To add to that a bit: You'll want to use equals. Even though == might work sometimes, it's not ...

99. String equals implementation    forums.oracle.com

It seems like atleast as of JDK1.5 implementation of java.lang.String class's equals method (public boolean equals(Object anObject)), in case the two strings are of same length, entire content is compared. Won't it be worthwhile to also ensure that - if their hash is available, the hash matches before comparing the entire string character arrays? Seems like that would offer a significant ...

100. equals(name.charAt(n)) doesn't work?    forums.oracle.com