There is no way to make a single class like that, because Java does not support "union" types. If you define a class with an int and a String, the space for both the int and the reference to a String would be allocated, defeating the space-saving purpose of what you are trying to achieve.
You could store your "atoms" as java.lang.Object values, but you would need to check their type and cast every time you need to obtain the index or the string. This approach is cumbersome, because primitive ints are wrapped in java.lang.Integer objects, adding to storage requirements.
A cleaner approach would be defining an interface for your Atom, and defining two classes, a StringAtom and an IntAtom, to store the two atom kinds in your program:
interface Atom {
boolean hasInt();
boolean hasString();
int getInt();
String getString();
}
class StringAtom implements Atom {
private final String s;
public StringAtom(String s) {this.s = s;}
boolean hasInt() {return false;}
boolean hasString() {return true;}
int getInt() {throw new IllegalStateException();}
String getString() {return s;}
}
class IntAtom implements Atom {
private final int n;
public IntAtom(int n) {this.b = b;}
boolean hasInt() {return true;}
boolean hasString() {return false;}
int getInt() {return n;}
String getString() {throw new IllegalStateException();}
}
SimpleAtomwith anintandComplexAtom extends SimpleAtomwith aString.