-6
if(something == "") return;

Does it mean if something is " " aka empty, just exit the function? I tried searching different answers. None of them explained what this piece of code does.

HelpMe123
  • 1
  • 1
  • 5
  • `" "` (with a blank) is not the same as `""` (without a blank) – Turing85 Nov 06 '17 at 14:03
  • 1
    Possible duplicate of [A Return inside and outside an If Statement](https://stackoverflow.com/questions/18282883/a-return-inside-and-outside-an-if-statement) – Turing85 Nov 06 '17 at 14:04
  • 2
    First of all, `==` is used for comparison, not `=`. Plus, to compare string value (not reference), you need to use `String#equals`. – Andrew Li Nov 06 '17 at 14:04
  • `"None of them explained what this piece of code does."` - I wouldn't expect so, given that this code doesn't compile. `String` and `boolean` are incompatible types. – David Nov 06 '17 at 14:05
  • I made a mistake it was == – HelpMe123 Nov 06 '17 at 14:08
  • You should correct the code to show "==". In that case it looks like it would be saying if something is the same object as the empty string then return. – Daniel Gale Nov 06 '17 at 14:08
  • Did you try running a piece of code to see what would happen? – EJoshuaS - Stand with Ukraine Nov 06 '17 at 14:11
  • 1
    https://stackoverflow.com/a/513839/4411297 Here is an answer that mentions Java String Comparisons. I'm not sure which data type "something" is, but I am assuming it is a string. – Daniel Gale Nov 06 '17 at 14:17

2 Answers2

1

return immeadiately exits the current function you're in.

That means that no code is executed after the return has been executed with one exception:

try {
    ...
    return;
} finally {
    // this code will be executed even if a return is called inside the 'try' block!
}

You can use return; (no value returned) when you want to exit a void method.


On a sidenote: Don't compare strings with ==. Compare strings with .equals(...).


Back to your original question: If the code was like this:

if (something.equals("")) return;

Then yes, it would mean that the method returns if 'something' is empty!

ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
  • what can be the use for if(something == "") return; then? What I take from this is it's just like a comment – HelpMe123 Nov 06 '17 at 14:10
  • Check the link in my answer (thanks @Turing85) for details on the differences between `==` and `equals`. The code `something == ""` can evaluate to true under certain circumstances. But it almost never is the thing you want to do with strings! – ParkerHalo Nov 06 '17 at 14:11
  • @HelpMe123 I've updated my answer once more to get back to your original question. – ParkerHalo Nov 06 '17 at 14:18
1

You have 2 errors.

  1. = doesn't check if something is equal. = assigns a value to a variable. You need to use == for primitive datatypes (like integer).

  2. For strings you need to use string.equals(anotherString) to check if something is equal

ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
isaace
  • 3,336
  • 1
  • 9
  • 22