You have a basic misunderstanding of the concept. Arithmetic is performed on numbers, not on octal numbers or decimal numbers or hexadecimal numbers.
The octal/decimal/hex aspect refers only to the written representation.
As you probably know, to write a literal octal value in the code, you prefix it with a leading zero. Similarly to write a hex value you prefix it with 0x
.
To print a value in octal, use Integer.toOctalString(int)
.
To parse a string and interpret it as an octal value, use Integer.parseInt(value, radix)
, and specify 8
as the radix.
For example, you two examples become:
int value1 = Integer.parseInt("5", 8);
int value2 = Integer.parseInt("3", 8);
int result = value1 + value2;
System.out.println(Integer.toOctalString(result));
and
int value1 = Integer.parseInt("12", 8);
int value2 = Integer.parseInt("3", 8);
int result = value1 - value2;
System.out.println(Integer.toOctalString(result));