Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Can I use pointers in Java?  (Read 1717 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2010-09-22 18:52:38 »

Okay, so if I have
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
Class Main{
Image[10][4] = fillArrayWithImages();
Person p = new Person(Image);
}

Class Person{
    Person(Images[][] i){
        Images[10][4] = i;
    }
}


Does Person have a copy of the Image array? Or is p.Images pointing to the Image array?
Do I have two copies of the Image array in memory or just one?
Offline noblemaster

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #1 - Posted 2010-09-22 19:03:16 »

it's pointers & you have only 1 copy!

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 797
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2010-09-22 19:13:05 »

the code you provided makes little sense (as it won't even compile). you are assigning an Image[][] to an element of an Image[][]


Anyway, if you pass an Image[][] to a method, it will not be copied. If you change something in the array, it will be visible to everything that refers to that Image[][].

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

JGO Kernel


Medals: 378
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2010-09-22 21:21:32 »

And don't call 'em pointers, you'll only get confused with C. They're references.

Cas Smiley

Offline gouessej
« Reply #4 - Posted 2010-09-22 21:40:44 »

And don't call 'em pointers, you'll only get confused with C. They're references.

Cas Smiley
I agree with princec. Pointers are more flexible but more dangerous. Using the same word for 2 very different notions is quite confusing.

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #5 - Posted 2010-09-23 23:12:55 »

Every Object is a reference, every primitive is a value. Arrays and Strings are objects.

Whenever you have an instance of an Object, any fields that "hold" that object are just references to the same Object in memory.

1  
2  
3  
4  
5  
6  
7  
8  
MyObject o = new MyObject();
MyObject o2 = o;
MyObject o3 = o;
MyObject o4 = o3;
MyObject o5 = o;
MyObject o6 = o2;
MyObject o7 = o;
MyObject o8 = o7;


What's being copied when you set those equal is the address of the original MyObject you created, not the object itself. Think of it that you aren't ever creating any new object unless you say "new" or you call a method that returns a new object (like clone()). In the latter case, the method is going to be calling new somewhere anyway.

See my work:
OTC Software
Offline CyanPrime
« Reply #6 - Posted 2010-09-23 23:52:07 »

Every Object is a reference, every primitive is a value. Arrays and Strings are objects.

Whenever you have an instance of an Object, any fields that "hold" that object are just references to the same Object in memory.

1  
2  
3  
4  
5  
6  
7  
8  
MyObject o = new MyObject();
MyObject o2 = o;
MyObject o3 = o;
MyObject o4 = o3;
MyObject o5 = o;
MyObject o6 = o2;
MyObject o7 = o;
MyObject o8 = o7;


What's being copied when you set those equal is the address of the original MyObject you created, not the object itself. Think of it that you aren't ever creating any new object unless you say "new" or you call a method that returns a new object (like clone()). In the latter case, the method is going to be calling new somewhere anyway.
Fantastic answer. Thank you very much for your reply.
Offline Nate

JGO Kernel


Medals: 147
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #7 - Posted 2010-09-24 03:27:37 »

Think of it that you aren't ever creating any new object unless you say "new" or you call a method that returns a new object (like clone()). In the latter case, the method is going to be calling new somewhere anyway.

I'm being a bit nitpicky, but clone happens to be special as it constructs an object without using "new" or even calling a constructor. This is the reason not to use clone (and Serializable, which also uses black demon magic to create new objects). Don't take my word though, try Joshua Bloch and Doug Lea:
http://www.artima.com/intv/bloch13.html

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 797
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2010-09-24 06:25:56 »

I'm being a bit nitpicky, but clone happens to be special as it constructs an object without using "new" or even calling a constructor. This is the reason not to use clone (and Serializable, which also uses black demon magic to create new objects). Don't take my word though, try Joshua Bloch and Doug Lea:
http://www.artima.com/intv/bloch13.html

I'm being even more nitpicky, but you are encouraged to use clone() for array copies.

Don't take my word though, try Joshua Bloch and Doug Lea:
http://www.artima.com/intv/bloch13.html

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

JGO Kernel


Medals: 147
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #9 - Posted 2010-09-24 07:41:15 »

Just saying those cats are reasonably well respected, whereas I gets no respect. Tongue Plus every time I mention the crappyness of Serializable someone replies with, "just use Serializable". Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 797
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2010-09-24 07:46:42 »

Just saying those cats are reasonably well respected, whereas I gets no respect. Tongue Plus every time I mention the crappyness of Serializable someone replies with, "just use Serializable". Smiley

The problem there is that you are biased. You plug your own API (kyro). Regardless of whether it is better, you'll have a harder time convincing people, than somebody else.

</offtopic>

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

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #11 - Posted 2010-09-25 01:23:58 »

I'm being a bit nitpicky, but clone happens to be special as it constructs an object without using "new" or even calling a constructor. This is the reason not to use clone (and Serializable, which also uses black demon magic to create new objects). Don't take my word though, try Joshua Bloch and Doug Lea:
http://www.artima.com/intv/bloch13.html
Cool, good point to be specific. I was sort of trying to keep it simpler but I understand after reading it over that I was just being inaccurate.

As for the Serializable thing, I actually agree with you - one of my favorite things about Java is its overwhelming transparency with how it works. One thing I hate about Objective-C is that almost all of it is a black box. Serializable is one of the rare weird beasts in Java, and I rarely use it, to be honest. In the other thread I was more arguing about its potential uses (because it can indeed be useful).

See my work:
OTC Software
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.

Dwinin (23 views)
2014-09-12 09:08:26

Norakomi (56 views)
2014-09-10 13:57:51

TehJavaDev (69 views)
2014-09-10 06:39:09

Tekkerue (34 views)
2014-09-09 02:24:56

mitcheeb (56 views)
2014-09-08 06:06:29

BurntPizza (40 views)
2014-09-07 01:13:42

Longarmx (25 views)
2014-09-07 01:12:14

Longarmx (31 views)
2014-09-07 01:11:22

Longarmx (31 views)
2014-09-07 01:10:19

mitcheeb (39 views)
2014-09-04 23:08:59
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!