0

Suppose that i have given below syntax:

Boolean isCapital = city.isCapital();
    String isCapitalName;
    if(isCapital == null) {
      isCapitalName = "";
    }

Means that i do not want else condition in my short for(The header of this blog) then what should be the syntax.

I want to minimize the use of if else condition in my project so that i want to use on liner if else.

Please guide.

MiniSu
  • 566
  • 1
  • 6
  • 22
  • Why are you assigning `isCapitalName` an empty string? What should `isCapitalName` be assigned to if `isCapital` is not null? – khelwood Apr 20 '18 at 13:56
  • There is no ternary operator without an `else` clause. But you can omit the `{}` e.g. `if(someCondition) isCapitalName=="";` in a single line – L.Spillner Apr 20 '18 at 13:56
  • 2
    @L.Spillner while true, most style guides (including [Google's Java Style Guide](https://google.github.io/styleguide/javaguide.html)) disallow the neglection of parentheses with good reason: later modifications are more bug-prone since you "assume" the parentheses are there. – Turing85 Apr 20 '18 at 13:59
  • I would expect a method called "isCapital()" to return either `true` or `false` not `true`, `false` or `null`. Have I missed something? – Dragonthoughts Apr 20 '18 at 14:00
  • @Turing85 thanks for the clarification. I knew that but Just wanted to open up the possibility ;). – L.Spillner Apr 20 '18 at 14:01

1 Answers1

6

You can set isCapitalName like so:

String isCapitalName = isCapital == null ? "" : null;

This has an identical behavior to your current code. It sets it to:

  • an empty string "" if isCapital == null
  • null by default

Edit taking into account @khelwood's comment:

The default value of an uninitialized local variable is not actually null, but it would cause an error if you used it in your code. I'm not sure why you would leave it uninitialized, however -- you probably want to choose a default value to put in the second clause of the ternary.

Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
  • 1
    Shorter `String isCapitalName = city.isCapital() ? "" : null;` no need for `isCapital ` – Youcef LAIDANI Apr 20 '18 at 13:59
  • 1
    It doesn't have identical behaviour to the OP's code. In the OP's code, `isCapitalName` is unusable because on some branches it is _unassigned_ (not null). – khelwood Apr 20 '18 at 13:59
  • @khelwood Isn't string set to `null` when uninitialized? Reference [this](https://stackoverflow.com/questions/5389200/what-is-a-java-strings-default-initial-value) – Jonathan Lam Apr 20 '18 at 14:00
  • 1
    @JonathanLam That's talking about a field, not a local variable. If a local variable is unassigned, the compiler regards it as having no value at all, not even null. – khelwood Apr 20 '18 at 14:03
  • 1
    @YCF_L I would be a little cautious to do that in case the `isCapital` variable is used elsewhere in the code. – Jonathan Lam Apr 20 '18 at 14:04
  • @khelwood I was unaware of that. I'll edit that into my answer. – Jonathan Lam Apr 20 '18 at 14:05
  • @JonathanLam the compiler will even throw an error if it can find an execution path to the use a local variable so that is has not yet been initialized before. – Turing85 Apr 20 '18 at 14:08
  • @Turing85 and @khelwood That's very interesting information! I've been so used to using JavaScript and its weak error checking and having everything set to `undefined` by default that I totally missed this feature of Java. – Jonathan Lam Apr 20 '18 at 14:10
  • 2
    @JonathanLam [JLS, §16](https://docs.oracle.com/javase/specs/jls/se9/html/jls-16.html): "*[...] For every access of a local variable or blank final field x, x must be definitely assigned before the access, or a compile-time error occurs. [...]*" – Turing85 Apr 20 '18 at 14:16