Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Primtitives vs. Object vars  (Read 1238 times)
0 Members and 1 Guest are viewing this topic.
Offline beni

Senior Newbie





« Posted 2009-04-07 13:32:36 »

Hello,
please point me to the approprate post (or website), if this has answered before - the search function unfortunately did not spill out anything usable.

I wonder if it is better to use primitive types instead of the boxed versions, when possible, and just convert if neccessary, ie. favorint int over Integer.
Or is generally using the boxed versions not that much of an overhead?
Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #1 - Posted 2009-04-07 14:03:00 »

This depends a lot on what you're using them for, but in general:

The primitives are faster and take up less memory. Only used the boxed versions when you absolutely need objects. (Like for storing integers in a hashmap).

As always with programming, there are exceptions. Wink

Play Minecraft!
Offline Mr_Light

Senior Member




shiny.


« Reply #2 - Posted 2009-04-07 15:44:23 »

I usually let the case "does null make sense as a value" decide.

Then again I hope we get something like @NotNull and vm optimalisations to make primitives obsolete. They are somewhat of a workaround..

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

JGO Kernel


Medals: 283
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2009-04-07 15:49:26 »

Goodness no! Primitives aren't objects.

Cas Smiley

Offline Markus_Persson

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #4 - Posted 2009-04-07 16:07:19 »

[Primitives] are somewhat of a workaround..

I don't think they are.

Without primitives, java would get fairly weird. How would you implement an Integer class without an int?
Would "12" become an Integer literal instead of an int literal? It'd degenerate into the same mess Strings are in where we have to use "if (12.equals(myInt))" instead of "if (myInt==12)"

Primitives are probably more a conscious design decision, both to make the jvm able to run faster by allowing it to make more assumptions, and to make the language easier.

What's missing in java are both references to primitives and any way to abstract references to different primitives. There's some casting and (auto)boxing going on, but it's not really elegant or efficient.
We need proper template support in the compiler!

[disclaimer: I am aware that it's possible to design a pure OO language with no primitives]

Play Minecraft!
Offline Mr_Light

Senior Member




shiny.


« Reply #5 - Posted 2009-04-07 16:37:45 »

heh  Grin

Hmm yeah I did forget about object identity, that would get annoying wouldn't it.. and I don't mean having to call equals.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2009-04-07 21:47:04 »

1. Primtives as objects
2. Operator overloading

var x = 100++;   Huh

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ewjordan

Junior Member





« Reply #7 - Posted 2009-04-07 23:40:44 »

1. Primtives as objects
2. Operator overloading

var x = 100++;   Huh
Not sure I get it.  Are you saying that you'd like to be able to do that, or that it would be Evil?

To me, the immutability of an object is a separate matter from whether it follows primitive or object semantics, and the immutability of a literal should make that code snippet illegal, not the fact that integers are primitives.

I think Scala pretty much gets it right - everything is an object as far as the language is concerned (even integer literals effectively have methods, for instance 0.until(100) generates a list from 0 to 99), but under the hood optimizations are applied so that there's not too much autoboxing going on if it's not necessary.

It's only comfortable there because operator overloading and infix notation are allowed, though.

My main object vs. primitive gripe in Java is that primitives are "blessed" by the language, in the sense that you can't define your own, you just have to be thankful for the few that you have (and there are too few - if we can't define our own, we should at least have complex and arbitrary precision floats/ints, if not vectors and matrices as well).  To be fair, this is true in Scala as well, you can't define your own types and get the performance benefits of primitive types.  But at least you can get the same semantics and ease of use, which is a huge win if you ever need to use more complicated number objects.
Offline Gudradain
« Reply #8 - Posted 2009-04-07 23:52:25 »

I learn operator overloading in C++ some months ago and I didn't really like it. Sure you have more possibility with that but it just make it harder to understand the code...
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2009-04-08 00:44:56 »

Not sure I get it.  Are you saying that you'd like to be able to do that, or that it would be Evil?

Ofcourse that'd be evil.

I was just failing to point out that more freedom is not always a good thing.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (53 views)
2014-04-15 18:08:23

BurntPizza (51 views)
2014-04-15 03:46:01

UprightPath (65 views)
2014-04-14 17:39:50

UprightPath (48 views)
2014-04-14 17:35:47

Porlus (65 views)
2014-04-14 15:48:38

tom_mai78101 (89 views)
2014-04-10 04:04:31

BurntPizza (149 views)
2014-04-08 23:06:04

tom_mai78101 (245 views)
2014-04-05 13:34:39

trollwarrior1 (203 views)
2014-04-04 12:06:45

CJLetsGame (210 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!