S. Esteves
S. Esteves

Reputation: 453

Which is more efficient - String or created object

I have a couple of created object that takes only one private String field and respective getter and setter. This objects are immutable and will only serve the purpose to store a single String.

e.g.

public class Country {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

My question is, is it more efficient to use standard object String instead of created object Country for most use cases?

Upvotes: 0

Views: 421

Answers (3)

Rolf Schäuble
Rolf Schäuble

Reputation: 690

First of all, your Country class is not immutable. You can always call its setName method to change it later. To be immutable, it would have to look like this:

public class Country {

    private final String name;

    public Country(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

To answer your question: Yes, it is more efficient to only have the String without the wrapper class. With the wrapper class, you have two objects per country, without it, there is only one.

But you might still want the wrapper class, for two reasons:

  1. You can later add more information about a country, e.g. its timezone, currency, ...

  2. Type safety. Consider these two methods:

    void doSomething(Country country);
    void doSomething(String country);
    

    The first one only accepts Country objects, so it's hard to misuse. The second one accepts any kind of string, but you still have to make sure to only pass it a correct country name. In the first case, the compiler can help you avoid errors; in the second case it cannot.

You will have to weight these two aspect yourself and decide what's the best for your project, as there is no correct answer for everyone. But don't just pick the most memory efficient solution per default; maintainability and robustness are also worthwhile.

Upvotes: 2

tryingToLearn
tryingToLearn

Reputation: 11655

If you are sure that your object will contain a single String property

AND

you don't have any restrictions to be put on this property (e.g. disallowing numbers in this string or putting a restriction on length) go for Sting rather than custom object.

REASON?

  1. Strings are immutable which will save you the effort to make your custom object immutable.
  2. Java takes special care to manage memory in case of Strings(read String pool). So you avoid the chance of any possible memory leaks arising out of mistake.

EDIT:

When I say : if you don't have any restrictions to be put on this property use custom object what I meant is that although these restrictions can be put in without custom object also but if you do that in custom object, it will be more efficient in the sense that you won't have to duplicate your code.

Upvotes: 2

Suresh Atta
Suresh Atta

Reputation: 121998

If there are other properties like TimeZone, ShortCode etc will be added later go with Country class.

If you are sure that there will be only one property forever, the class is completely redundant. Just use a String as a property wherever you using it.

Upvotes: 1

Related Questions