2

What is the difference between this data class

data class Greeting(
     val name: String,
     val country: String? = null
)

VS

data class Greeting(
     val name: String,
     val country: String = ""
)

Is their any preferred way when we use null and empty values such as "",0

Vikash Singh
  • 43
  • 1
  • 7
  • 3
    This is opinionated, but don't ever use `0` or `""` as an indicator that there is no value. This is just confusing and error-prone. Use nulls instead. The only situation where I could choose empty value instead of null is empty collection. Depending on the case empty list may be easier to handle than null. – broot Feb 27 '22 at 08:30
  • Does this answer your question? [Difference between null and empty ("") Java String](https://stackoverflow.com/questions/4802015/difference-between-null-and-empty-java-string) – Endzeit Feb 27 '22 at 12:54
  • @broot Strong agree on 0, strong disagree about “” in some situations. – Tenfour04 Feb 27 '22 at 16:03
  • @Tenfour04 Yes, I guess there are such cases. For example, search query when searching through some data. Conceptually, we assume search query is always there, but empty string is understood as "no filtering". – broot Feb 27 '22 at 17:36

0 Answers0