Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
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 1464 times)
0 Members and 1 Guest are viewing this topic.
Offline K.I.L.E.R

Senior Duke




Java games rock!


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

1  
2  
3  
4  
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?

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline Orangy Tang

JGO Kernel


Medals: 56
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'.

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

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


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

Hmmm... What if you do this:
1  
2  
3  
4  
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 Duke




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.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline c_lilian

Senior Duke


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...

Lilian

Offline Micke

Senior Newbie




Yada-yada


« 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 Duke




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.

Blog
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.

theagentd (19 views)
2014-10-25 15:46:29

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (45 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!