Question

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?

Was it helpful?

Solution

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 :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top