What is the difference between zero, 0 and nothing?

What is the difference between null , 0 and nothing ?

I cannot find a question regarding all three of them.

For example:

If I get input from string and parse it to int .

 return Integer.parseInt(sc.nextLine()); 

Or, if I ask if there is a string != "" Or is not nothing , I am confused about what to use when and why.

I am confused by which one to use when checking data.

+9
java null zero
source share
8 answers

null means that the variable contains a reference to a space in memory that does not contain an object.

0 is a numeric data type with a value of 0.

Nothing really does not exist, however I think you can think of it as an empty String String "" , which is simply a String data type that does not contain a value.

If you look at this from the Javascript paradigm, this can be misleading. In Java, variables are not true and false, so the type is considered in comparison.

+15
source share

0 is a number, you use it to check if the numeric value (int, short, float, double, etc.) is the number 0.

null is the value of a link that is not specified anywhere, you use it to make sure that the link is really referring to something.

nothing not part of Java.

The closest thing for nothing (for: no information whatsoever) is the declaration of the void method. He claims that the method returns literally nothing.

Note that 0, null and the empty string "" are values ​​and therefore contain information.

For example, 0 is the answer to "What is subtracted 5 from 5?" (among others).

null is a negative response to "Does this thing point to an object?".

"" "may be the answer to the question:" What is the longest sequence β€œX in your name?” if your name is not β€œXanthippe”.

+8
source share

There is no β€œnothing” in Java. There is a null reference, meaning that the variable does not refer to any object, therefore it is nothing but the concept of undefined , as in Javascript.

Also note that String comparisons should be done using equals , not != Or == , so string.equals(otherString) .

The value 0 is the default value that int gets when declaring an instance variable.

+7
source share

"Null" in Java, an object reference does not point to any object; it is null.

"Nothing" , Java did not call anything "Nothing"

"0" means the string object exists with the value "0"

"" means an empty string, i.e. String object exists with value ""

+5
source share
  • Nothing means anything in Java.

  • 0 means an integer value greater than -1 and less than 1.

  • "Zero" means that if any Link to an object is not specified anywhere in the memory, then it is called a "Zero" link.

+5
source share

The best approach for checking String objects in Java is to use org.apache.commons.lang.StringUtils.

If you do not want to use StringUtils , you can sc.nextList() check that sc.nextList() not equal to zero ( != null sc.nextList() , if it is not equal to zero, then you can check that it is different from " "( != "" ).

+3
source share

For example:

 int s; //s=null int s = 0 ; //s=0 String s="";// s=nothing 
+2
source share

A null variable is not assigned any value. The amount of memory (variable) is empty or contains the value of garbage. and the variable is 0, the variable has a value of "0". The amount of memory has a value of "0" stored in it.

NULL basically means a value or an unknown value. NULL can be empty, meaningless, or even not initialized. For example, when we say that the Employee bonus is equal to zero, this means that the person does not receive any bonus or is not entitled to receive a bonus. Now, if the value is 0, its average recipient receives a bonus, but at the moment the bonus is zero.

+2
source share

All Articles