To understand what null means in Java, let's look at an analogy with numbers: the number 0 symbolizes the absence of something, and null means the same thing when it comes to reference data types. If a field of a reference type (such as String, Object, or StringBuilder) is not explicitly assigned a value, then, by analogy with primitive types, it receives a default value, and that value is null:

Code Console output

public class Solution {

    public static int i;
    public static String s;

    public static void main(String[] args) {
        System.out.println(i);
        System.out.println(s);
    }
}
                    
0
null

But if you declare an array like this:


String[] strings = new String[12];

an array will be created containing 12 elements, and all of them will be null:

Code Console output

public class Solution {
    public static void main(String[] args) {
        String[] strings = new String[12];

        for (int i = 0; i < strings.length; i++) {
            System.out.println("Element " + i + ":" + strings[i]);
        }
    }
}
                    
Element 0: null
Element 1: null
Element 2: null
Element 3: null
Element 4: null
Element 5: null
Element 6: null
Element 7: null
Element 8: null
Element 9: null
Element 10: null
Element 11: null

As you can see, when concatenated with a string, the value null becomes the string "null". That said, if you call the toString() method on it, like this:


String[] strings = null;
System.out.println(strings.toString());

then you will get a NullPointerException (we'll discuss exceptions in detail later). The same thing happens if you try to call any other method on null (the exception is static methods, which you'll get to know shortly):


public static void main(String[] args) {
    StringBuilder sb = null;
    sb.append("test"); // This will compile, but there will be a runtime error
}

null is, among other things, a reserved keyword (like public or static), so you cannot create a variable, method, or class named null. Like the others, this keyword is case-sensitive (you may have noticed that we write null in lowercase everywhere). That means:


String firstName = Null; // Compilation error

String secondName = NULL; // Compilation error

String fullName = null; // This will compile

Let's look at what else you can and can't do with null:

  • You can assign null to any reference:

    
    StringBuilder sb = null;
                
  • null can be cast to any reference type:

    
    String s = (String) null; // This will compile, but doing this doesn't make any sense :)
                
  • null cannot be assigned to a primitive variable:

    
    int i = null; // This won't compile
                
  • null can be compared using == and !=

  • null == null returns true

In previous lessons, we talked about how everything in Java is an object, and every object has a type.

What can we say about null in this respect? null is a literal of a certain type, and this type has no name. And since this type has no name, it is impossible to declare a variable of this type, or to cast to it. Thus, null is the only representative of this unnamed type. In practice, we can ignore this type, and think of null as a special literal that can be assigned to any reference variable.

Things to remember:

  • null is the default value for reference data types
  • null means "no value"
  • If we call any method on an object whose value is null, the code will compile but at runtime we'll get a NullPointerException.
undefined
12
Task
New Java Syntax, level 12, lesson 4
Locked
From a small bucket to a big one
When two long numbers are multiplied, the result may exceed the capacity of the long type. If precision is not important, then one option could be to store the result as a floating point number. Implement the getSquare method. It should return the square of the input argument.
undefined
12
Task
New Java Syntax, level 12, lesson 4
Locked
Approximate value
When converting from integers to floating point numbers, the lower order parts of numbers may be discarded. Let's test this in practice. In the getDifference method, the incoming argument has already been converted to a float. Determine what was discarded during the conversion and return the missing
undefined
12
Task
New Java Syntax, level 12, lesson 4
Locked
Conscious choice
When a type is narrowed, part of a number might be discarded. Or it might not. Implement the isByte, isShort, and isInt methods. They should determine whether the input argument can be respectively converted to the byte, short, and int types "without serious consequences".
undefined
12
Task
New Java Syntax, level 12, lesson 4
Locked
Task No. 1 about integer type conversions
This task begins a series of tasks about integer type conversions. This is not a very difficult topic, but it often bewilders noobies because instructors sometimes put in at the very beginning, which is fundamentally wrong. But on Level 10, you're ready. Arrange the cast operators correctly to get the required result: d > 0. The operators are in the conditions.
undefined
12
Task
New Java Syntax, level 12, lesson 4
Locked
Task No. 3 about integer type conversions
"Nothing organizes your thinking as much as performing integer type conversions in your head. Of course, a compiler will also suffice". And now, the third task in the "Integer Type Conversion" series. We have some variables converted to another type, but we don't have enough of them. We need to add one type cast operation to obtain the require answer.
undefined
12
Task
New Java Syntax, level 12, lesson 4
Locked
Task No. 6 about integer type conversions
Here's the sixth task in the "Integer Type Conversions" series. What are we shooting for here? We want you to master these techniques so you don't have to relearn them. This time our task has too many cast operators. Superfluous conversions have crept in. Find and remove them to get the required answer.