Difference between null and empty (“”) Java String

前端 未结 22 1682
再見小時候
再見小時候 2020-11-22 17:10

What is the difference between null and the \"\" (empty string)?

I have written some simple code:

String a = \"\";
String b         


        
22条回答
  •  时光取名叫无心
    2020-11-22 17:42

    This concept can be better understood from mathematics. Have you ever tried dividing a number (not zero) by 0 using a calculator e.g 7/0? You will get a result that looks like something this: undefined, not a number, null etc. This means that the operation is impossible, for some reasons (let's leave those reasons to be discussed another day).

    Now, perform this: 0/7. You will get the output, 0. This means that the operation is possible and can be executed, but you the answer is just 0 because nothing is left after the division. There is a valid output and that output is zero.

    In the first example, not only was the output invalid, the operation was not possible to execute. This is akin to null string in java. The second example is akin to empty string.

提交回复
热议问题