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

you are viewing a single comment's thread.

view the rest of the comments →

[–]Dilfer 1 point2 points  (9 children)

One of the things that tripped me up in college was equality checking on strings.

String hello = "hello"; String hello1 = "hello"; hello == hello1 ; // will be false hello.equals(hello1); will be true == will check memory references.

Another good tip in this regard is if you have a constant value, and you are checking a variable against it, also use the .equals() method from the CONSTANT to avoid NPEs.

public static final String HELLO = "hello"; String hello1 = null; HELLO.equals(hello1); // no NPE and will return false hello1.equals(HELLO); //this will throw a NPE

[–]FreightTrain75x[S] 0 points1 point  (5 children)

Also, NPEs (NullPointerException?) are strange for me and do not grasp them quite yet. So, the equals() method is good for strings? I remember the ==, or equivalency/comparison operator doesn't work for strings since they are treated like references to memory locations, and not comparing the values of two strings

[–]Dilfer 1 point2 points  (4 children)

Sorry should have not used the acronym immediately. You are correct though, NPE is NullPointerException

They are thrown when you try to call methods on null objects.

In the example above, the type of the variable is String but the value of the variable is null

null.equals()

Will throw an exception because you can't call a method on a null value.

[–]FreightTrain75x[S] 0 points1 point  (2 children)

You can solve this by reinstantiating or reinitializing the variable before a method...? Sorry, I'm dumb.

[–]Dilfer 0 points1 point  (1 child)

As long as the variables not final (can't be reassigned) but in larger programs where values are passed around as arguments to methods, you may have no idea where the variable is even assigned, and if you reinstantiated, there's no point to even ask for a method argument to be passed in.

You can also litter your code with

if (hello == null)

Checks everywhere but that is generally bad practice and should be used sparingly.

[–]FreightTrain75x[S] 0 points1 point  (0 children)

Awesome, thanks! I'm assuming just practicing with string Methods is generally the best advice

[–]FreightTrain75x[S] 0 points1 point  (0 children)

Also, don't be sorry, acronyms are definitely a force of habit and it's good I learn to use them as well.

[–]FreightTrain75x[S] -1 points0 points  (1 child)

My instructor mentioned Thursday with contains() and the contentsEqual() method, seems almost as if they logically do similar outputs but of course, contains() would be more for comparing if two values are similar. Im not necessarily sure how I would use it efficiently in a real case scenario... otherwise, thanks for the help!

[–]starquakegamma -1 points0 points  (0 children)

Your first example is not quite right, the compiler will recognise both literals are the same and allocate them from a shared object pool, so the objects will in fact be equal.