Search code examples
javamethodsunary-operator

How the 'NOT' operator(!) works when used with methods in Java?


This is an example of using access modifiers to avoid generating exceptions:

class FailSoftArray {
    private int a[];
    private int errval;
    public int length;

    public FailSoftArray(int size, int errv) {
        a = new int[size];
        errval = errv;
        length = size;
    }

    public int get(int index) {
        if(indexOK(index)) return a[index];
        return errval;
    }

    public boolean put(int index, int val) {
        if(indexOK(index)) {
            a[index] = val;
            return true;
        }
        return false;
    }
    private boolean indexOK(int index) {
        if(index >= 0 & index < length) return true;
        return false;
    }
}

public class TestMethod {
    public static void main(String[] args) {
        FailSoftArray333 fs = new FailSoftArray(5, -1);
        int x;

        System.out.println("\nError access message.");
        for(int i = 0; i < (fs.length * 2); i++)
            if(!fs.put(i, i*10))
                System.out.println("Index " + i + " outside the range of the array");

        for(int i =0; i < (fs.length * 2); i++) {
            x = fs.get(i);
            if(x != -1) System.out.print(x + " ");
            else
                System.out.println("Index " + i + " outside the range of the array");
        }
    }
}

I don't understand why in this case I have to use the operator (!) for the code to work correctly:

        System.out.println("\nError access message.");
        for(int i = 0; i < (fs.length * 2); i++)
            if(!fs.put(i, i*10))
                System.out.println("Index " + i + " outside the range of the array");

When I print it with (!) the results are what you would expect:

Error access message..
Index 5 outside the range of the array
Index 6 outside the range of the array
Index 7 outside the range of the array
Index 8 outside the range of the array
Index 9 outside the range of the array
0 10 20 30 40 Index 5 outside the range of the array
Index 6 outside the range of the array
Index 7 outside the range of the array
Index 8 outside the range of the array
Index 9 outside the range of the array

Without (!):

Error access message..
Index 0 outside the range of the array
Index 1 outside the range of the array
Index 2 outside the range of the array
Index 3 outside the range of the array
Index 4 outside the range of the array
0 10 20 30 40 Index 5 outside the range of the array
Index 6 outside the range of the array
Index 7 outside the range of the array
Index 8 outside the range of the array
Index 9 outside the range of the array

Can someone explain to me how exactly the operator (!) works in this case? Why is it needed and how does it affect the method?


Solution

  • The put function of FailSoftArray returns a boolean value true/false whether it was able to successfully put the new value val at index index within array a

    The ! signifies a logical NOT

    By using if(!fs.put(i, i*10)), you are effectively telling the compiler

    if(put was NOT able to place i*10 at index i)
        System.out.println("Index " + i + " outside the range of the array");
    

    Leaving the ! out would be like saying

    if(put succeeded)
        //Print error
    

    which is obviously not intended