String equality

Hi,
I've recently had a problem with string equality. My problem is the following:
element.getName() == String2
(getName() method returns a string)
returns false, but both strings really seem to be equal, that is:
System.out.println(String1 +"/"+ String2 +"=" + (String1 == String2));
prints
LAMES/LAMES=false
which seems pretty strange to me.
I searched both is the javadocs and in the tutorial, but I did not find any solution to my problem. How that 2 strings containing the same Characters are not equal.
Nicolas

This has been stated NUMEROUS times throughout the forums...
== compares Objects (two String Objects will most likely NOT be equal, even if both contain the same characters in the same sequence)
String.equals(...) compares the equality of the contents of the Strings

Similar Messages

  • Help with StuckThread in java.lang.String.equals(String.java:619)

    I periodically get a stuck Execute thread on my Weblogic 8.15 server
    during a call to a Stateless Session Bean. When I do thread dumps, the
    thread is always stuck in the same place (in
    java.lang.String.equals(String.java:619) or in the call immediately above it
    java.util.LinkedList.indexOf(LinkedList.java:397)). Even though the thread
    dump indicates that the thread is in a runnable state, if I do multiple
    thread dumps over a period of time, the stack trace always indicates that
    the thread is in the same place. The thread remains stuck until Weblogic is
    restarted. Other client applictions can make session bean calls, but each
    stuck thread seems to still take up lots of CPU time. I have let the stuck
    threads run overnight, and the stack trace from the thread dump always shows
    them executing the same String/LinkedList code. In each case, our code is
    trying to iterate over a collection
    Does anybody know what could cause this problem, and how to fix it? I get
    StuckThreadMaxTime errors in the log:
    ####<Dec 4, 2005 10:47:25 AM EST> <Error> <WebLogicServer> <nybill>
    <myserver> <weblogic.health.CoreHealthMonitor> <<WLS Kernel>> <>
    <BEA-000337> <ExecuteThread: '4' for queue: 'weblogic.kernel.Default' has
    been busy for "1,263" seconds working on the request
    "ncss.billing.ejb.billAdmin.session.BillAdminSession_uli3xb_EOImpl", which
    is more than the configured time (StuckThreadMaxTime) of "1,200" seconds.>
    Here are some stack traces from different thread dumps (I have the full
    thread dumps if necessary):
    "ExecuteThread: '10' for queue: 'weblogic.kernel.Default'" daemon prio=5
    tid=0x7720eb98 nid=0xd68 runnable [571f000..571fdb0]
    at java.lang.String.equals(String.java:619)
    at java.util.LinkedList.indexOf(LinkedList.java:398)
    at java.util.LinkedList.contains(LinkedList.java:176)
    at
    ncss.billing.broadviewBill.xml.XMLBillCreation.getDailyCallSummary(XMLBillCr
    eation.java:1992)
    at
    ncss.billing.broadviewBill.xml.XMLBillCreation.getGraphs(XMLBillCreation.jav
    a:1931)
    at
    ncss.billing.broadviewBill.xml.XMLBillCreation.getLocations(XMLBillCreation.
    java:2618)
    at
    ncss.billing.broadviewBill.xml.XMLBillCreation.createXMLBill(XMLBillCreation
    .java:236)
    at
    ncss.billing.ejb.billAdmin.session.BillAdminSessionEJB.getBillAsXml(BillAdmi
    nSessionEJB.java:341)
    at
    ncss.billing.ejb.billAdmin.session.BillAdminSession_uli3xb_EOImpl.getBillAsX
    ml(BillAdminSession_uli3xb_EOImpl.java:100)
    at
    ncss.billing.ejb.billAdmin.session.BillAdminSession_uli3xb_EOImpl_WLSkel.inv
    oke(Unknown Source)
    at
    weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:477)
    at
    weblogic.rmi.cluster.ReplicaAwareServerRef.invoke(ReplicaAwareServerRef.java
    :108)
    at
    weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:420)
    at
    weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubjec
    t.java:363)
    at
    weblogic.security.service.SecurityManager.runAs(SecurityManager.java:147)
    at
    weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:415)
    at
    weblogic.rmi.internal.BasicExecuteRequest.execute(BasicExecuteRequest.java:3
    0)
    at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:219)
    at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:178)
    "ExecuteThread: '22' for queue: 'weblogic.kernel.Default'" daemon prio=5
    tid=0x772538d0 nid=0xe24 runnable [497f000..4fdb0]
    at java.lang.String.equals(String.java:619)
    at java.util.LinkedList.indexOf(LinkedList.java:398)
    at java.util.LinkedList.contains(LinkedList.java:176)
    at
    ncss.billing.broadviewBill.xml.XMLBillCreation.getMostExpensiveOrLongestCall
    s(XMLBillCreation.java:1892)
    at
    ncss.billing.broadviewBill.xml.XMLBillCreation.getTopTenReport(XMLBillCreati
    on.java:1798)
    at
    ncss.billing.broadviewBill.xml.XMLBillCreation.getTopTenReports(XMLBillCreat
    ion.java:1751)
    at
    ncss.billing.broadviewBill.xml.XMLBillCreation.getLocations(XMLBillCreation.
    java:2612)
    at
    ncss.billing.broadviewBill.xml.XMLBillCreation.createXMLBill(XMLBillCreation
    .java:236)
    at
    ncss.billing.ejb.billAdmin.session.BillAdminSessionEJB.getBillAsXml(BillAdmi
    nSessionEJB.java:341)
    at
    ncss.billing.ejb.billAdmin.session.BillAdminSession_uli3xb_EOImpl.getBillAsX
    ml(BillAdminSession_uli3xb_EOIm.java:100)
    at
    ncss.billing.ejb.billAdmin.session.BillAdminSession_uli3xb_EOImpl_WLSkel.inv
    oke(Unknown Source)
    at
    weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:477)
    at
    weblogic.rmi.cluster.ReplicaAwareServerRef.invoke(ReplicaAwareServerRef.java
    :108)
    at
    weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:420)
    at
    weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubjec
    t.java:363)
    at
    weblogic.security.service.SecurityManager.runAs(SecurityManager.java:147)
    at
    weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:415)
    at
    weblogic.rmi.internal.BasicExecuteRequest.execute(BasicExecuteRequest.java:3
    0)
    at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:219)
    at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:178)
    "ExecuteThread: '21' for queue: 'weblogic.kernel.Default'" daemon prio=5
    tid=0x76afb060 nid=0x498 runnable [48af000..48fdb0]
    at java.lang.String.equals(String.java:619)
    at java.util.LinkedList.indexOf(LinkedList.java:398)
    at java.util.LinkedList.contains(LinkedList.java:176)
    at
    ncss.billing.broadviewBill.xml.XMLBillCreation.getMostFrequentlyCalledNumber
    sOrCitiesReport(XMLBillCreation.java:1839)
    at
    ncss.billing.broadviewBill.xml.XMLBillCreation.getTopTenReport(XMLBillCreati
    on.java:1772)
    at
    ncss.billing.broadviewBill.xml.XMLBillCreation.getTopTenReports(XMLBillCreat
    ion.java:1743)
    at
    ncss.billing.broadviewBill.xml.XMLBillCreation.getLocations(XMLBillCreation.
    java:2612)
    at
    ncss.billing.broadviewBill.xml.XMLBillCreation.createXMLBill(XMLBillCreation
    .java:236)
    at
    ncss.billing.ejb.billAdmin.session.BillAdminSessionEJB.getBillAsXml(BillAdmi
    nSessionEJB.java:341)
    at
    ncss.billing.ejb.billAdmin.session.BillAdminSession_uli3xb_EOImpl.getBillAsX
    ml(BillAdminSession_uli3xb_EOImp.java:100)
    at
    ncss.billing.ejb.billAdmin.session.BillAdminSession_uli3xb_EOImpl_WLSkel.inv
    oke(Unknown Source)
    at
    weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:477)
    at
    weblogic.rmi.cluster.ReplicaAwareServerRef.invoke(ReplicaAwareServerRef.java
    :108)
    at
    weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:420)
    at
    weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubjec
    t.java:363)
    at
    weblogic.security.service.SecurityManager.runAs(SecurityManager.java:147)
    at
    weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:415)
    at
    weblogic.rmi.internal.BasicExecuteRequest.execute(BasicExecuteRequest.java:3
    0)
    at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:219)
    at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:178)
    The code in LinkedList that it seems to be executing is the following:
    Line#
    397 for (Entry e = header.next; e != header; e = e.next) {
    398 if (o.equals(e.element))
    399 return index;
    400 index++;
    401 }
    I am running Weblogic 8.15 on Windows 2000.
    Thanks for any help,
    - Don

    njb7ty wrote:
    I suggest dropping that example program and concentrating on reading a book on Java such as 'Head First in Java'. Otherwise, you will spend a lot of time trying to get something to work and gain little value from it.Likewise... Jumping into reflections before you can [read a stack-trace|http://www.0xcafefeed.com/2004/06/of-thread-dumps-and-stack-traces/] is like signing up a toddler for the New York Marathon... it's probably simply beyond your skill level... so step back... go read a book, do some tutorials, get your head around just the process of the designing, writing, compiling, running, and debugging java programs... and what the different diagnostics mean... Then, equipped with your nose-clip and your trusty stone ;-) you contemplate leaping into the deep end ;-)
    Cheers. Keith.

  • Test string equals syntax in configure.ac

    Hi all, I recently started a project that uses autoconf, and many examples around the place use the following syntax for string equality tests:
    if test "x$str" = "xyes"; then ...
    I was wondering why this is used as opposed to
    if test "$str" = "yes"; then ...
    which seems a bit more elegant and in plain bash seems to work fine (even with empty strings etc). Anyone know the reason why it is usually done the first way and not the second?

    but autoconf was made for POSIX sh (not sure what that supports exactly), and so many developers try to be overly cautious. I know we are not with the linux-only product.s.
    it might also be a holdover from people writing tests without the quotes, like  if x$var = xyes; then ...
    if you write without the x and $var is an empty string, you'll get a syntax error

  • String equal method Vs Object equal method.

    hello, Can anybody explain me difference between equal method in String class and equal method in Object class. We have equal method in object classes. and object class is the super class of all classes, so why we need equal method in String class.

    RGEO wrote:
    hello, Can anybody explain me difference between equal method in String class and equal method in Object class. We have equal method in object classes. and object class is the super class of all classes, so why we need equal method in String class.Because "equal" means different things for different objects. For a String, "equal" would mean that both Strings being compared have the exact same characters, in the same sequence. For an Integer, "equals" would mean that both objects have the same integer value.

  • Compareto versus equals performance for strings equality

    Hi
    Just wanted to know whether compareto and equals method performance for checking whether the two strings are equal or not .
    Which one is good with respect to bytecode , speed performance.
    Im not asking for the difference, only asking which one is good to use most number of times for comparing two strings for equilaty.
    Thanks.

    Hi
    Ya they are both for different purposes. No doubt.
    t which one is faster among the two for string
    equality?
    compareto or equals.
    thanksHis point is that it doesn't matter. You shouldn't worry about optimization for small details like this. Unless you are profiling your code and find a large slowdown with that code, then don't worry about their performance. Use the one that best suits your purpose. If you want to know if they're equal, use equals(), if you want to know their order, use compareTo().

  • Doubt in String Equals

    Doubt in String Equals_
    if(filePath != "") {
         getExcelData(filePath);
    }same as
    if(!filePath.equals("")) {
         getExcelData(filePath);
    }The can both be used interchangeably right ?

    kajbj wrote:
    (You should really open a textbook and read about Strings, or google)fortunatly i know this :)
    if you do
    String str = "abc"
    str =="abc" // will always return true.
    but str==(new String("abc")) // will always return false.
    But as per the query posted i am referring to the first case :)

  • String equals, matches,, startsWith..endsWith...

    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]);
    in the method is actaully return false ...
    I am passing the same value ...
    Please help..
    I don't understand why ...
    Thanks

    >>>
    "if (sa.equals(s) ...){ // it actually return true // others matches ..startsWiths .....
    however ...
    when I call method: toCompare(s[0]);
    in the method is actaully return false ...
    I am passing the same value ..."
    >>>
    I'm assuming that by toCompare you mean compareTo...?
    if so, compareTo() returns an int, not a boolean. If it returns 0, then the two Objects being compared are equal.

  • On String Equality (using ==)

    Hello. I am trying to create a program that will merge sentences on their first similar word occurence, i.e.:
    Given "good day. i would like to see you at the office"
    and "how are you feeling today?"
    It will produce "good day. i would like to see you feeling today".
    If there are no similar words, the program will just concatenate the Strings.
    I tested the examples above and I got "good day. i would like to see you at the office how are you feeling today". Obviously, something's wrong.
    So I fiddled around my code putting printlns here and there. Then I found out that my comparison loops doesn't stop. Equality ( == ) always evaluates to false.
    On my tester class, I tried the following:
    String intro = "good day. i would like to see you at the office";
    String outtro = "how are you feeling today?";
    String[] fooArray = intro.split(" ");
    String[] barArray = intro.split(" ");
    String foo = fooArray[7];
    String bar = barArray[2];
    System.out.println("Is foo equal to bar? " + foo == bar);The abovementioned code returned false.
    Then I tried the following
    String intro = "good day. i would like to see you at the office";
    String outtro = "how are you feeling today?";
    String[] fooArray = intro.split(" ");
    String[] barArray = intro.split(" ");
    String foo = fooArray[7];
    String bar = barArray[2];
    System.out.println("How about chars? " + foo.charAt(0) == bar.charAt(0));This time around, it returned true.
    So I'm guessing that maybe I should just create a function that will loop through the chars of the String and compare them.
    However, I am bothered by how Java handled foo == bar . Can someone explain to me why this happened? Is there a workaround to this one or should I always create a function that loops through chars everytime I want to compare Strings?
    Thanks!

    == compares references for equality. When you want to compare two Object instances for equality you use the equals method. This applies not only to String but any other type of Object..

  • String equal?

    I have some instrument which communicate with PC via RS232. So I use VISA write, read...
    It's working OK, but have next problem
    I want to compare two strings, one is constant other one will come out from VISA read. If I use indicator, it's show "hex"(for example 3E04 4B73 ) value from VISA, but if I use same wire to "equal?" to compare two strings it's return strange values...
    Let's see all together to see what's my problem:
    Rookie; LV 2011 on WIN 7
    Solved!
    Go to Solution.

    I don't know the definition of "strange values", especially for a boolean that can only be false or true. Can you explain?
    What is the display format of the diagram constant? (normal or hex?)
    Once you have the right valuue in the indicator, you can right-click the terminal and "create constant". Now you have a diagram constant that is guaranteed to be identical to the current value of the indicator.
    LabVIEW Champion . Do more with less code and in less time .

  • If (string.equals("""))

    How can I check a string for a quotation mark? I always get the compile error unclosed string literal.

    "String with /" quote mark".indexOf("\"")
    if this returns any positive integer then it has found the quote.

  • String.equals question...

    Hey, this is probably an easy problem for those who know what they're doing! I'm just a Computing student afterall.
    Why does this if statement not return true:
         public String translate(String opc) {
              String opCode = opc.substring(0,2);
              String mnemonic;
              if (opCode.equalsIgnoreCase("60"))
                   mnemonic = "iadd";
    etc
    According to System.out.println
    the opc is 6000
    and opCode is 60
    Help needed! Thanks!
    Graham

    Yeah it was just me being stupid, hehe. I had a list of if statements after each other, instead of else ifs, and so the else at the end was overwriting the variable. Eeek.
    Please tell me all programmers make as stupid mistakes! :p
    Thanks
    Graham

  • Compare 2 string equal false, open dialog

    Hi all,
    I developpe a function who compare if a certain link is in an list. This list can be full by the user with a File.openDialog.
    My function work only with link who haven't any space in the file name. I think it'is a encoding problem but i don't know how to fix this problem.
    If anyone can help me I would appreciate!
    Thanks you!
    My code look like this:
    //choose a certain file
    var array_of_files = File.openDialog ("selectionner une image ou plusieurs images", get_file_filter ([".psd", ".png", ".jpg", ".eps"], "choisir une image"), true);
    // add to an array, i add the decode because my file as space and i want that to display it without %
    myArray.push(File.decode(array_of_files[index].name));
    After I select all links and I foreach link I compare if the link is in the array myArray
    var array_all_links = doc_package.links;
    for(var i = 0; i<array_all_links.length; i++){
          is_link_valid (array_all_links[i])
    // this function doesn't work with link with space
    function is_link_valid(link_item){
        var is_link_valid = true;
        if(is_in_array(link_item.name, results.array_of_ignored_picture) ){
            is_link_valid = false;
        alert(is_link_valid);
        return is_link_valid;
    function is_in_array(myString, myArray) {
              for (x in myArray) {
                        if (myString == myArray[x]) {
                                  return true;
              return false;

    @Bastien – spaces (white space) in file names should be no problem at all…
    Just the composites you encountered.
    I did not test this:
    I don't know if this will "normalize" composite characters in file names, but you could also write the list of file or path names to a text file with Unicode UTF-16 encoding and extract an array by reading it out, so you could compair with another array.
    But I know that an UTF-16 encoding is required for the source txt or csv files for datamerge in InDesign if one is using eg. Umlauts in file names. Just discovered this and wrote about here:
    Jim04:
    Inserting an image from a CSV file
    http://forums.adobe.com/message/5606395#5606395
    I think, this wasn't one of the ways František Erben suggested.
    One was something with hash tags and a text file, the other one was using a "Unicode Normalizer"…
    But better ask for details  in the other thread:
    František Erben:
    Problem with UTF filenames on Mac OS X
    http://forums.adobe.com/thread/1275272?tstart=0
    Uwe

  • Using a SQL Query in an Alert and Matching a String

    I've created an alert in 12.0.4 using a SQL Query and the field that I'm trying to match is a string.  Originally the query returned multiple rows but when the alert still didn't fire, I modified the query WHERE clause to return only one row:
    NAME                                RESPONSE
    Are area lights working?            No
    My expression in the metric is RESPONSE.  In the Monitor I'm matching a string equal to No.  (Do I need double quotes around the matchvalue?  Single quotes?  No quotes?)  The metric is in the 15min scan group, the role is xMII Developers and I'm in that role. The monitor alert string is ' =  '.  Both metric and monitor are active and I've subscribed to the monitor.  Other alerts in the 15min scan group (all based on tag queries) are firing off properly.
    Why is nothing showing up in the Alert Log?
    David Macindoe

    David,
    Did you figure out the answer?  If not, I will try to find someone to address your question.
    Mike

  • Need to convert a string to boolean

    i have string true, false, i need to convert them to boolean, is it good to use String.equals(true/false) or is it good to use Boolean.valueOf(true/false)
    Regards,
    Surya

    See http://forum.java.sun.com/thread.jsp?forum=54&thread=455788&tstart=0&trange=30.
    Remember, '=' is assignment, '==' is equals...

  • Null String and Empty String problem

    Hello everyone,
    since i am totally new in JSP, i am getting problem in handling strings.
    Suppose i have a variable users = ""; then
    I want to ask when to use:
    if (users.equals(""))
    and
    if(users == "")
    in my code, variable users has value "regional" for regional users.
    and i am checking this code as:
    if (users.equals{"regional")) {
    out.print ("I am inside code");
    at that time, the code is throwing error (run time error)
    and when i changed the code as:
    if (users == "regional") {
    out.print ("I am inside code");
    this time, the code is not generating error but the part message "I am inside code " is not displaying. The code do not inserts inside the if condition
    I hope u understand my problem. Can anybody help me out with this.

    This has basically nothing to do with JSP, but with basic Java knowledge.
    When using the '==' operator to compare Objects (yes, String is actually a subclass of Object), then it will look if they are of the same reference. Using the '==' operator to compare primitive datatypes (int, boolean, char, etc) will look if they have the same value.
    That is why the Object class has the equals() method to give the ability compare with another objects. And you can only invoke it when the Object is actually instantiated. So if it is not null.
    if (string != null && string.equals("somevalue")) {
    // or
    if ("somevalue".equals(string)) {
    }should work.
    Edit rym82: this will not throw a NPE, but an ordinary compilation error ;)
    Message was edited by:
    BalusC

Maybe you are looking for

  • Samsung 4G LTE Mobile Hotspot SCH–LC11

    Have the Samsung 4G LTE Mobile Hotspot card (use with my tablet). My desktop PC does not have wireless capability. What are my options to connect my desktop to the Hotspot card?  I have read that buying a special WIFI USB adapter will work, or instal

  • Query to check internal table

    hi all, I have to check an internal table data(it_tab) It holds records like A       B     C       D       E        F 325     1     1     512     0     0010 325     1     1     548     36     0010 325     1     1     554     42     0010 325     2    

  • Restrict some document numbers in f-44 tcode

    Hello Experts, My client requirement is to restrict some document numbers when we run f-44 , here I am entering account and company code as input and after pressing enter here it displays some records. here among the list of displayed records, I want

  • My Macbook Pro restarts on it's own

    I'm running Yosemite.  All is running fine.  My battery is dead so it restarts if I unplug it, but this happens when it's sleeping. Please help! Kelley Hardware Overview:   Model Name:    MacBook Pro   Model Identifier:    MacBookPro8,2   Processor N

  • [solved] new systemd-sysctl applies 99-sysctl.conf before networkd?

    Recently pacman updated systemd. I use systemd-networkd for Ethernet (wired) control (Wireless LAN is hard blocked) I have this in /etc/sysctl.d/99-sysctl.conf net.ipv4.ip_forward = 1 This is to allow sharing of internet where my system is gateway. T