0

In Java I want to create a data type, let's say Atom, which contains an index number, and when that index = 0, the Atom will contain a String datum, otherwise the Atom is just an index (Integer).

How can I do that?

I guess it would save some space if most Atoms are just indexes and only some contain Strings.

2
  • Are you meaning to create a class? Commented Dec 8, 2014 at 16:21
  • 1
    you could have two classes: SimpleAtom with an int and ComplexAtom extends SimpleAtom with a String. Commented Dec 8, 2014 at 16:21

3 Answers 3

3

The following Factory Pattern illustrates how a static factory method createAtom creates instances of different classes (all Atom), with different data.

Here I made index final as changing it to 0, will not change the class of the object. Also Atom has to offer access to the optional datum.

public class Atom {
    public final int index;

    private Atom(int ix) {
        this.index = ix;
    }

    public String getDatum() {
        return null;
    }

    public static Atom createAtom(int index) {
        return index != 0 ? new Atom(index) : new ExtendedAtom(index);
    }
}

class ExtendedAtom extends Atom {
    private String datum;

    ExtendedAtom(int ix) {
        super(ix);
    }

    @Override
    public String getDatum() {
        return datum;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

2

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();}
}

Comments

0

Create a small class with an index member variable, a flag checking if that index is zero and a String datum that will be non-null only if the flag is true (indicating the index is zero).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.