Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #32974
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Java primitive variable objects |
| Date | 2015-11-02 16:29 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <n18kf8$l7s$1@dont-email.me> (permalink) |
| References | <33c95894-2638-4484-af5d-545701d34372@googlegroups.com> <n18clb$k3o$1@dont-email.me> <1c0652ed-b0b8-49e5-b0f1-c22cea97ce9a@googlegroups.com> |
On 11/2/2015 4:03 PM, Eric Douglas wrote:
> On Monday, November 2, 2015 at 2:16:30 PM UTC-5, Eric Sosman wrote:
>> I'd recommend not comparing Integers (or Bytes, etc.) with the ==
>> and != operators. Comparing them with int (byte, ...) values is okay
>> if the references are non-null, because unboxing occurs and you get a
>> value-based comparison. It's also okay to compare non-null references
>> with <, <=, >=, and >, because these also cause unboxing.
>>
>> To avoid nulls, try to use Integer (etc.) only when forced to and
>> prefer to use int (etc.) wherever you can. When you must use an
>> Integer -- for example, when you're storing values in a Collection --
>> be careful to null-check when you need to:
>>
>> Map<Thing, Integer> occurrences = ...;
>> for (Thing thing : collectionOfThings) {
>> Integer count = occurrences.get(thing);
>> occurrences.put(thing, count == null ? 1 : count + 1);
>> }
>>
>>> [...] Is there a difference in declaring static int constants versus Integers for memory use, performance?
>>
>> There's certainly a difference in memory use. An int value takes
>> four bytes, period. Using an Integer requires the Integer object itself
>> plus a reference variable that points to it. On my system (yours may be
>> different) that's sixteen bytes for the Integer and four more for the
>> reference. As mentioned above, though, some smallish Integers are
>> cached and re-used, so part of the twenty-byte total may be "free" or
>> at the least "amortized;" it's hard to come up with an exact measurement
>> of the int-to-Integer penalty.
>
> So, == and != are fine if we're comparing int to int, but if I need to compare Integer to int our best bet is to check for null and use the method
> public static void checkThis(final Integer checkMe) {
> int o = 1;
> if (checkMe != null && checkMe.equals(o)) {
> System.out.println("equal");
> } else {
> System.out.println("not");
> }
> }
> or this should be about the same
> if (checkMe != null && checkMe.intValue() == o) {
Fine. Or, perhaps,
if (checkMe == null) {
// nothing there
} else if (checkMe.intValue() == o) {
// something equal to o is there
} else {
// something unequal to o is there
}
> and a better option would be to avoid the primitive objects altogether.
"Altogether" is perhaps a bit strong, as there are situations where
you simply cannot use a primitive. I've mentioned Collections and Maps
and the like (which can only contain Objects), you've mentioned some of
the Swing framework, DecimalFormat#parse() returns a Number (which isn't
actually a wrapper class, but has some of the same issues), and so on.
Use the wrapper classes when an API demands them, or when you find it
useful to distinguish null from zero-or-whatever, but use the primitives
whenever you can.
By the way, "primitive object" is a peculiar turn of phrase. The
Java Language Specification uses "primitive type" to mean one of char,
boolean, byte, short, int, long, float, or double, and when people see
the word "primitive" they're likely to think you mean one of these. The
classes Character, Boolean, Byte, Short, Integer, Long, Float, and
Double are called the "wrapper classes" for the corresponding primitive
types, and I think you and your students might benefit from using that
terminology instead of the oxymoronic "primitive object."
> For example, I made a custom class which extends JTable. I have a custom method getCellStyle(int row, int col) which is returning Integer, and I'm trying to compare this to int constants (public static final class variables) for different custom cell styles. If this row, col does not reference a valid cell it's returning null. So instead I should be returning int, and if it's not a valid cell just return a valid int which corresponds to a constant that means not a valid cell.
Perhaps instead of an Integer your method should return a CellStyle
of your own devising. An enum might be attractive here, and would be a
bit more type-safe than a bunch of static finals. Just a suggestion.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Java primitive variable objects Eric Douglas <e.d.programmer@gmail.com> - 2015-11-02 10:11 -0800
Re: Java primitive variable objects Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-02 14:15 -0500
Re: Java primitive variable objects Eric Douglas <e.d.programmer@gmail.com> - 2015-11-02 13:03 -0800
Re: Java primitive variable objects Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-02 16:29 -0500
Re: Java primitive variable objects Jan Burse <janburse@fastmail.fm> - 2015-11-02 22:46 +0100
Re: Java primitive variable objects Jan Burse <janburse@fastmail.fm> - 2015-11-02 22:52 +0100
Re: Java primitive variable objects Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-02 17:47 -0500
Re: Java primitive variable objects Joerg Meier <joergmmeier@arcor.de> - 2015-11-05 22:19 +0100
Re: Java primitive variable objects Eric Douglas <e.d.programmer@gmail.com> - 2015-11-05 13:31 -0800
Re: Java primitive variable objects Arne Vajhøj <arne@vajhoej.dk> - 2015-11-07 22:21 -0500
Re: Java primitive variable objects Eric Douglas <e.d.programmer@gmail.com> - 2015-11-09 04:52 -0800
Re: Java primitive variable objects Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-09 08:54 -0500
Re: Java primitive variable objects Eric Douglas <e.d.programmer@gmail.com> - 2015-11-09 07:27 -0800
Re: Java primitive variable objects Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-09 10:51 -0500
Re: Java primitive variable objects Eric Douglas <e.d.programmer@gmail.com> - 2015-11-09 08:28 -0800
Re: Java primitive variable objects Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-09 11:57 -0500
Re: Java primitive variable objects Eric Douglas <e.d.programmer@gmail.com> - 2015-11-09 10:13 -0800
Re: Java primitive variable objects Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-09 13:28 -0500
Re: Java primitive variable objects Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-09 14:53 -0500
Re: Java primitive variable objects Nigel Wade <nmw@ion.le.ac.uk> - 2015-11-10 09:27 +0000
Re: Java primitive variable objects "Jeffrey H. Coffield" <jeffrey@digitalsynergyinc.com> - 2015-11-09 10:44 -0800
Re: Java primitive variable objects Martin Gregorie <martin@address-in-sig.invalid> - 2015-11-09 18:59 +0000
Re: Java primitive variable objects Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-09 15:02 -0500
Re: Java primitive variable objects Martin Gregorie <martin@address-in-sig.invalid> - 2015-11-09 22:57 +0000
Re: Java primitive variable objects Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-09 19:16 -0500
Re: Java primitive variable objects Arne Vajhøj <arne@vajhoej.dk> - 2015-11-09 20:55 -0500
Re: Java primitive variable objects Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-09 22:13 -0500
Re: Java primitive variable objects Arne Vajhøj <arne@vajhoej.dk> - 2015-11-09 20:40 -0500
Re: Java primitive variable objects Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-09 22:23 -0500
Re: Java primitive variable objects Martin Gregorie <martin@address-in-sig.invalid> - 2015-11-09 14:00 +0000
Re: Java primitive variable objects Eric Douglas <e.d.programmer@gmail.com> - 2015-11-09 07:18 -0800
Re: Java primitive variable objects Arne Vajhøj <arne@vajhoej.dk> - 2015-11-09 20:43 -0500
Re: Java primitive variable objects Arne Vajhøj <arne@vajhoej.dk> - 2015-11-09 19:38 -0500
Re: Java primitive variable objects Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-09 20:26 -0500
Re: Java primitive variable objects Martin Gregorie <martin@address-in-sig.invalid> - 2015-11-10 02:10 +0000
Re: Java primitive variable objects Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-09 22:12 -0500
Re: Java primitive variable objects Martin Gregorie <martin@address-in-sig.invalid> - 2015-11-10 13:01 +0000
Re: Java primitive variable objects Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-10 12:23 -0500
Re: Java primitive variable objects Eric Douglas <e.d.programmer@gmail.com> - 2015-11-10 10:10 -0800
Re: Java primitive variable objects Arne Vajhøj <arne@vajhoej.dk> - 2015-11-10 22:08 -0500
Re: Java primitive variable objects Martin Gregorie <martin@address-in-sig.invalid> - 2015-11-10 20:05 +0000
Re: Java primitive variable objects Eric Douglas <e.d.programmer@gmail.com> - 2015-11-10 13:21 -0800
Re: Java primitive variable objects Martin Gregorie <martin@address-in-sig.invalid> - 2015-11-10 23:30 +0000
Re: Java primitive variable objects Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-10 20:58 -0500
Re: Java primitive variable objects Gene Wirchenko <genew@telus.net> - 2015-11-10 22:30 -0800
Re: Java primitive variable objects Arne Vajhøj <arne@vajhoej.dk> - 2015-11-10 21:58 -0500
Re: Java primitive variable objects Arne Vajhøj <arne@vajhoej.dk> - 2015-11-10 21:56 -0500
Re: Java primitive variable objects Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-10 22:19 -0500
Re: Java primitive variable objects Arne Vajhøj <arne@vajhoej.dk> - 2015-11-10 22:42 -0500
Re: Java primitive variable objects Jerry Stuckle <jstucklex@attglobal.net> - 2015-11-10 23:07 -0500
Re: Java primitive variable objects Eric Douglas <e.d.programmer@gmail.com> - 2015-11-10 06:06 -0800
Re: Java primitive variable objects Lew <lewbloch@gmail.com> - 2015-11-16 14:47 -0800
csiph-web