How to define unix permission constants (READ, WRITE, EXECUTE)

347 views Asked by At

I want to define these constants and | them for different operations to generate correct permissions.

Defining them as :

public static final int READ = 4;
public static final int WRITE = 2;
public static final int EXECUTE = 1;

gives me correct result as expected, like READ | WRITE | EXECUTE or WRITE | EXECUTE.

Does defining them as

public static final int READ = 0x4;
public static final int WRITE = 0x2;
public static final int EXECUTE = 0x1;

give me any benefit?

1

There are 1 answers

1
zjm555 On BEST ANSWER

Since they are equivalent in hex or decimal, that only adds in terms of readability for other developers. It's functionally the same.

Although, if you are doing it for readability, octal would be even better given the underlying system:

public static final int READ = 04;
public static final int WRITE = 02;
public static final int EXECUTE = 01;

or even more obvious:

public static final int READ = 1<<2;
public static final int WRITE = 1<<1;
public static final int EXECUTE = 1;

But that might be excessive :)