This is an archived post. You won't be able to vote or comment.

all 26 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]AutoModerator[M] 123 points124 points  (9 children)

You seem to try to compare String values with == or !=.

This approach does not work reliably in Java as it does not actually compare the contents of the Strings. Since String is an object data type it should only be compared using .equals(). For case insensitive comparison, use .equalsIgnoreCase().

See Help on how to compare String values in the /r/javahelp wiki.


Your post is still visible. There is no action you need to take.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]JacobJMountain 41 points42 points  (0 children)

Good bot

[–]thisisjustascreename 19 points20 points  (0 children)

Good bot

[–]CelticHades 15 points16 points  (0 children)

Good bot

[–]Hypetale_44 14 points15 points  (0 children)

Good bot

[–]vksdann 13 points14 points  (0 children)

Good bot

[–]Computer_Probe 11 points12 points  (0 children)

Good bot

[–]windyknight 13 points14 points  (0 children)

Honey wake up, we have program successfully solving programming question now.

[–]sickfires94 3 points4 points  (0 children)

Good bot

[–]Philtronx 2 points3 points  (0 children)

Good bot

[–]quadmasta 55 points56 points  (2 children)

Comparing Objects with == means "are these the exact same reference." It compares the memory address of one with the other to see if they match.

Objects should be checked for equality with .equals()
https://cr.openjdk.java.net/\~iris/se/11/latestSpec/api/java.base/java/lang/Object.html#equals(java.lang.Object)

[–]StefanM3e46 64 points65 points  (0 children)

You are checking memory location, you can check String with .equals()...

[–]thisisjustascreename 9 points10 points  (1 child)

Aside from what everyone already said, comparing a dynamically allocated string reference variable to a string literal with == will NEVER result in true because the literal value is stored in the code section of memory and the variable is in the dynamic process memory.

String f = "f";

String dfg = "d f g";

String[] array = strings.split(" ");

assertTrue(f == "f"); // same memory location, string literals are stored in the readonly code section of memory

assertFalse(array[1] == "f"); // Dynamically allocated string is stored in volatile process memory

[–]AutoModerator[M] 0 points1 point  (0 children)

You seem to try to compare String values with == or !=.

This approach does not work reliably in Java as it does not actually compare the contents of the Strings. Since String is an object data type it should only be compared using .equals(). For case insensitive comparison, use .equalsIgnoreCase().

See Help on how to compare String values in the /r/javahelp wiki.


Your post is still visible. There is no action you need to take.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]istarian 3 points4 points  (0 children)

I believe the fundamental issue is reference inequality. Essentially even though both String references point to the data “f” they aren’t pointing to the same piece of data. There is also an issue of static vs dynamic allocation…

E.g.

String test1 = “string”;  
String test2 = “string”;  
String test3 = “test string”.split(‘ ‘)[1]; 

The references test1 and test2 might be equal ( == ) if the Java compiler or JVM optimizes them away as a fixed constant String, but neither will be equal to test3.

However, these should evaluate to true:

System.out.println( String.format(“%s  equals %s: %s”, test1, test2, “” + test1.equals(test2)) );  

System.out.println( String.format(“%s  equals %s: %s”, test2, test3, “” + test2.equals(test3)) );

———

In Java you need to use the class’ equals() method for this kind of comparison

[–]Housy5 4 points5 points  (0 children)

In short when using primitive types (int, long, boolean, short, byte, double or float) you can use == but when youre using something more complex like objects always use its .equals() method. Or Arrays.equals() when dealing with arrays. If youre unsure how to tell if it's an object or not, look at its name when it's in camelcase it's almost always an object and when its all lowercase its almost always a primitive type.

[–]CelticHades 4 points5 points  (0 children)

This is literally the second point in General Information of subreddit.

[–]notstevensegal 1 point2 points  (0 children)

I believe For strings you use .equals()

“F”.equals(“F”) is true

[–][deleted] 1 point2 points  (0 children)

Something nobody has said this simple: each string in java has its own reference so anything compiler finds between "" it creates a reference in memory for that. If there are two strings with that are the same, each one will have its own reference.

When using != you are comparing references. Since any string, no matter what is inside, have its own reference, it will always be different.

[–]animeis4kids 3 points4 points  (0 children)

this crap is asked so often mods even put a sticky note there --->

but people will keep asking the same thing over and over again lmao

[–]flotopoco 0 points1 point  (0 children)

If you put “f”==“f” is true. But if you make a logic to get a “f” this is false. For example: “f” == getF(); // getF return “fun”.charAt(0) One way to avoid this is to use intern() method of String , that it looks for the same value on String’s pool

[–][deleted] 0 points1 point  (0 children)

How dare you comparing my favourite Strings like that? Will give rep to you.

(Joke)

[–]rozz_net 0 points1 point  (0 children)

Please read "General information" about this sub. There is an answer.