JAVA String: Difference between Null and ""?


    public static void main(String[] args) {
    String frase;

    String palabra = "";

    String mostrar = null;

    int i;
    int c = 0;

    frase = JOptionPane.showInputDialog("Ingrese al frase: ");
    frase += " ";
    for (i = 0; i < frase.length(); i++) {
        palabra = palabra + frase.charAt(i);
        if (frase.charAt(i) == ' ') { //asi se valida una cadena vacia
            if (palabra.length() - 1 > c) {
                c = palabra.length() - 1;
                mostrar = palabra.trim();
            palabra = "";
    System.out.println("La palabra de mayor longitud es: " + mostrar);
    System.out.println("Su longitud es: " + c);


I came across this code and what I wanted to know is … why use the String show = null and not a String show = "";? When changing to "" and running the program it does not recognize the longest word, I would appreciate your answer.


The two options mean different things, and should be used accordingly.

  • null means "no value"

  • "" means "there is a value". Whether this value is the empty string or not is not relevant.

Now, it's up to your logic what makes the most sense to initialize.

  • If there is a valid default value for that attribute, you simply initialize with that value. That value will depend on logic, and can even be null .

  • If there is no valid default value, it is best to leave it null . The reason is that if you assign a special instance (for example, "" in the case of String ), vas a estar teniendo que distinguir siempre entre si ese valor es el caso especial o no. Y si alguna vez te dejas esa comprobación, te vas a comer el error en vez de tener un NullPointerException` 1 .

For example, these days I'm modifying an OS program that has the freaking craze that when it doesn't find an element in the DB, it returns an "empty" object instead of null . So I have to do such that

 MiBean miBean = dao.getBean(clavePrimaria);
 if (miBean.getId() == 0) {
    throw new Exception("No hay bean con " + clavePrimaria);

If I forget to check, hopefully I will have an unexpected error in hacerCosasConElBean() and will have to parse the code until I find the error. If I'm unlucky, it will do something I don't have control of 2 and I won't find out until much later.

If the code returned null would do

 MiBean miBean = dao.getBean(clavePrimaria);
 if (miBean == null) {
    throw new Exception("No hay bean con " + clavePrimaria);

It is not more complicated, and in case I forgot the check, I would jump a NullPointerException that would indicate exactly the point and the failure of the error.

Your example is similar. Unless "" is a valid value, the only purpose of initializing "" is to hide some possible error that makes the variable not be assigned a value, and to confuse whoever reads your code into believing that "" is a valid value and nothing happens if some branch of execution does not assign a value to that variable.

So "" should be used if and only if initializing that value is optional and "" works as the default.

1 The rule is "fail as soon as possible." If you have a programming error, you want the program to crash when you hit the error, not introduce unexpected behavior that causes you to crash the program much later, which makes it more difficult to debug.

2 Including the risk of tampering with my system data incorrectly.

Scroll to Top