Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (710)
Games in Android Showcase (212)
games submitted by our members
Games in WIP (784)
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  
  Does Java optimise this...  (Read 1833 times)
0 Members and 1 Guest are viewing this topic.
Offline K.I.L.E.R

Senior Devvie

Java games rock!

« Posted 2005-03-02 06:14:13 »

public Point getLocation()
     return new Point(this.x, this.y);

I've read, from a random site, that Java optimises this piece of code to use the same object but with different values, so you will not be creating objects all the time.

Basically Java's optimisation does is have a pointer to the Point object and just replaces the appropriate x and y values.
Is this true or have I been reading way too much crap?

Is there a name for a "redneck" programmer?

Unemployed. Wink
Offline Orangy Tang

JGO Kernel

Medals: 57
Projects: 11

Monkey for a head

« Reply #1 - Posted 2005-03-02 06:45:07 »

I have no idea if it's optimised out, but I personally prefer an ImmutablePoint interface with getX/Y methods. Then your Point can implement that and you just have to return the point. Kinda a workaround for not having a proper C++ style 'const'.

[ - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline erikd

JGO Ninja

Medals: 16
Projects: 4
Exp: 14 years


« Reply #2 - Posted 2005-03-02 06:58:06 »

Hmmm... What if you do this:
for (int i = 0; i < 10000; i++) {
 ¬†this.x = this.y = i;
 ¬†point[i] = getLocation();

This would surely create new Point objects, right?

Anyway, there's 2 reasons why I think you shouldn't depend on an optimization like this:

1) Java doesn't optimize this, maybe a specific JVM does.
You don't want to depend too much on VM specific optimizations.

2) It doesn't make any sense to create new Point objects for getting the location of some object everytime you query it. Point has getters and setters anyway.

EDIT: 3) What Orangy Tang said  Grin

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline K.I.L.E.R

Senior Devvie

Java games rock!

« Reply #3 - Posted 2005-03-02 07:06:08 »

Recently I've been doing a lot of reading about VM optimisations, Sun doesn't mention everything the VM optimises.

I wish they would.

Is there a name for a "redneck" programmer?

Unemployed. Wink
Offline c_lilian

Senior Devvie

Projects: 1

Java games will probably rock someday...

« Reply #4 - Posted 2005-03-02 07:08:03 »

Swing API is full of "duplicate" methods like getPoint() / getPoint(Point p)

Meaning if you have already a mutable point, and you know you can share it across method calls, just pass it as reference and there won't be any object allocation, and if you haven't got one, a new instance will be created...


Offline Micke

Senior Newbie


« Reply #5 - Posted 2005-03-02 11:53:31 »

I tend to use not duplicate methods, but pass-return types like:

public Vector3f getWhatever(Vector3f dest, ...) {
 if (dest == null) {
   dest = new Vector3f;
 return dest;
Offline Virum

Junior Devvie

Like a leaf in an icy world, memories will fade

« Reply #6 - Posted 2005-03-04 03:17:49 »

What's wrong with Overloading?

It's time to prove to your friends that your worth a damn. Sometimes that means dying; sometimes that means killing a whole lotta people.

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

numerical (66 views)
2017-02-21 07:32:16

numerical (67 views)
2017-02-21 07:31:46

theagentd (172 views)
2017-02-18 13:42:33

theagentd (175 views)
2017-02-18 13:35:16

h.pernpeintner (1338 views)
2017-01-24 22:39:11

h.pernpeintner (1326 views)
2017-01-24 22:38:32

Galdo (1887 views)
2017-01-12 13:44:09

Archive (1976 views)
2017-01-02 05:31:41

0AndrewShepherd0 (2515 views)
2016-12-16 03:58:39

0AndrewShepherd0 (2309 views)
2016-12-15 21:50:57
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21 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‑
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!