Sign in to follow this  
Followers 0
Xer0botXer0

How to differentiate between objects | do objects have ids

16 posts in this topic

Hi guys,

 

From what I've searched up on google people are creating libraries to find differences between objects, I just want to be able to identify objects. Or rather find out if an object has an id.

 

The reason being, I'm trying to store an object inside of an array declared as an integer, so far I'm getting an error that's telling me the objects don't have IDs. which leads me to the question, how do I differentiate between objects ? one of the solutions I've looked at is by storing a value inside of a variable and basically creating your own ID for each object. Which kind of makes sense.  But coming from GML each instance(java object) of an object(java class) has an id which is used for reference. 

0

Share this post


Link to post
Share on other sites

Posted (edited)

Why are you trying to store an object inside an array of integers? If you want to store an object into an array, the array needs to be declared with the same type as the object (or a supertype/interface of).

Also, what is the exact error message? If you are trying to store an object in an int array you should get an error saying that cannot assign object type to int or something similar, not that the object doesn't have an ID.

EDIT: also, related to the topic, you might find interesting to read about identity and equality in Java.

Edited by Avalander
1

Share this post


Link to post
Share on other sites

You are posting a lot of threads, most of them on the same topic. Maybe it's time to slow down a little and work with tutorials and/or reading, instead of posting 6 threads in a single day?

2

Share this post


Link to post
Share on other sites

Posted (edited)

From what I've searched up on google people are creating libraries to find differences between objects, I just want to be able to identify objects. Or rather find out if an object has an id.

An object has a memory start address which is unique for each object. That is being used for identity checking (ie ==). The default implementation of "equals" also uses that (except for some standard objects like integers, reals, and strings, and such). By overriding "equals" you can have your own equality notion between objects. (However, given the kind of questions you ask, this is currently a little too far for now, so something to keep in mind for later.)

 

Edit: Sorry, I was talking total nonsense about equals: primitive types (int, double/float, and boolean) have no equals, and use == as equals. class-types (ie everything else than primitive types) have both == and equals, which may or may not check the same thing.

Edited by Alberth
0

Share this post


Link to post
Share on other sites

Here's my current code

 

package myPackage;


public class Server {
public static void main(String[] args) {


int Arr_Players_DB[] = new int [5]; //array object initialization


for (int i = 0; i < Arr_Players_DB.length ; i ++)
{
Arr_Players_DB[i] = -1; //Initialize array
}




for (int i = 0; i < Arr_Players_DB.length; i ++) // //populate array
{
Players_DB player = new Players_DB();
if (Arr_Players_DB[i] == -1)
{


Arr_Players_DB[i] = player;
}}}}
 

Why are you trying to store an object inside an array of integers? If you want to store an object into an array, the array needs to be declared with the same type as the object (or a supertype/interface of).

Also, what is the exact error message? If you are trying to store an object in an int array you should get an error saying that cannot assign object type to int or something similar, not that the object doesn't have an ID.

EDIT: also, related to the topic, you might find interesting to read about identity and equality in Java.

Error message is "Type mismatch: cannot convert from Players_DB to int"

 

Because I want a list, each list ID must be a reference to each object, each object holds variables which will equal to properties that I've assigned for each object. These will be for "Players" in the game in a server side application. So it makes sense that I should try and reference each object in an array. 

 

The reason I've moved from using a 2D array for this is because Multidimensional Arrays in java require that each dimension be the same data type, which is a problem when I want to store "username String" and "password Integer"(for example).

 

I had a look at Identity and equality, all it's showing me is that objects can be referenced by creating a new object type and storing another object in that. Referencing yes..

 

So the idea then is when I create a new array

int Arr_Players_DB[] = new int [5]; //array object initialization

That the data type be that of an object, but that would work would it ? 

 

 

You are posting a lot of threads, most of them on the same topic. Maybe it's time to slow down a little and work with tutorials and/or reading, instead of posting 6 threads in a single day?

 

They're all regarding java, and can be boiled down to reveal many similarities, how ever they are not the same. If you are not going to contribute to the topic then don't post. You are trying to be helpful, how ever let me help you understand that I am browsing the web while creating threads because it's things I've found on the web that don't make sense, specifically mentioned above regarding multidimensional arrays requiring the same data type which is when someone in that thread (not my thread) recommended referencing objects in arrays instead. So I'm not being lazy, or trying to make your day a little more difficult by scrolling by an additional 400 pixels to find a post worthy of your assistance. I'm trying to learn Java. So far this works for me. 

 

 

From what I've searched up on google people are creating libraries to find differences between objects, I just want to be able to identify objects. Or rather find out if an object has an id.

An object has a memory start address which is unique for each object. That is being used for identity checking (ie ==). The default implementation of "equals" also uses that (except for some standard objects like integers, reals, and strings, and such). By overriding "equals" you can have your own equality notion between objects. (However, given the kind of questions you ask, this is currently a little too far for now, so something to keep in mind for later.)

 

Edit: Sorry, I was talking total nonsense about equals: primitive types (int, double/float, and boolean) have no equals, and use == as equals. class-types (ie everything else than primitive types) have both == and equals, which may or may not check the same thing.

 

This is similar to the link provided by Avalandor, there's an image which shows that each object holds a space within memory. What's new is that you're saying they have unique start addresses, I don't think It's suppose to get that complicated to find these addresses just to make a list of objects that can be referenced. 

-2

Share this post


Link to post
Share on other sites

Posted (edited)

int Arr_Players_DB[] = new int [5]; //array object initialization

So, what you are doing here is create an array that holds Integer values,
you then go on to try an assign a non-integer value to it.

 

Players_DB Arr_Players_DB[] = new Players_DB [5]; //array object initialization

Is how it should be written to create an array that holds the class you are trying to put into it.


So far though, you seem to be jumping into things pretty randomly (having read and posted some replies in your other topics) They are all fairly starter topics but seemingly jumping around a bit.

I would suggest if you want to learn java pick up something like https://www.amazon.co.uk/Beginning-Java-Programming-Oriented-Approach/dp/1118739493 this book, as it would provide a much more structured approach to learning the language / programming in general.

 

Leaving out the many glaring holes and problems in the code that wont work by changing the array :D but yeah.. the code above really was just terribly written

Edited by GibbonThatCodes
1

Share this post


Link to post
Share on other sites

Posted (edited)

Yes the problem is that I don't understand the syntax for the 'new' keyword which is what I'm currently looking up.

What is happening in the code you've provided ? Is it allocating 5(6) slots within the Arr_Players_DB[] Array, creating a new Players_DB object then adding it to the array ? 

I see you're not initializing the array size before hand.

There are two ways to approach learning, There is the learn everything about something and then there's learning only what is relevant to the applications it's needed for. The one makes you more capable with a larger area of understanding, while takes longer and you may learn things that you'll never use, how ever may learn things that you may use, it's a gamble. The other is setting a goal, and over coming barriers on the way to that goal. 

The way I would suggest my approach to someone over their approach is by establishing pros and cons with both manners, the reason I say suggest is because it's not always the case that you understand the other persons method. I'm going to stick with learning what is relevant, yes I am all over the place at the moment, but eventually the gap will be closed the more sense I can make of these relevant points. 

Thank you for the information so far, there's something new at every avenue, right now I'm stumped with how I want to reference objects in an array, in your code it looks like you're creating the objects at the same time as assigning them to an array. which looks like the way I want to go about it. 

Edited by Xer0botXer0
0

Share this post


Link to post
Share on other sites

So what the code provided does is it create a new array and allocates enough memory to hold 5 objects of Players_DB class.

it doesnt populate any of the members at all, so right now the array has no instances of Players_DB in it, you would have to add those by doing a  

Players_DB_Arr[0] = new Players_DB();

or something along those lines for each of the indexes in the Array. And then obviously if you want to access those objects later you can by using their index again.

1

Share this post


Link to post
Share on other sites

I'm having a hard time working out what it is you're trying to achieve. But one thing I can offer you is to suggest that you use Java's collections framework instead of plain old arrays. ArrayList seems like a good fit:

// create an empty list of players
List<Player> players = new ArrayList<Player>();

// add some players to it
players.add( new Player() );
players.add( new Player() );
players.add( new Player() );
players.add( new Player() );
players.add( new Player() );


// how many players?
int numPlayers = players.size();

// who is the 3rd player? (remember 0-based indexing: 0, 1, 2)
Player player3 = players.get(2);
System.out.println(player3.getUserName()); 
1

Share this post


Link to post
Share on other sites

Posted (edited)

I'm trying to reference objects, so far I have this, I have moved onto lists.
 
package myPackage;
import java.util.*;


public class Server {
public static void main(String[] args) 
{


List list_Players_DB = new ArrayList();


Players_DB Player = new Players_DB();
list_Players_DB.add(Player);
Players_DB Player1 = new Players_DB();
list_Players_DB.add(Player);
list_Players_DB.add("hmm");
for (int i=0; i < list_Players_DB.size();i ++)
{


System.out.println("Players_DB Object REF:" + list_Players_DB.get(i));
}




}
}

I'm getting this result:

 

Players_DB Object REF: myPackage.Players_DB@7852e922
Players_DB Object REF: myPackage.Players_DB@7852e922
Players_DB Object REF: hmm
 
I was expecting that it prints unique values for each of the two objects.
 
I've changed it to this, just to see what's going on, I'm now adding object variables to the list or rather their values and printing that.
 
package myPackage;
import java.util.*;


public class Server {
public static void main(String[] args) 
{


List list_Players_DB = new ArrayList();


Players_DB Player = new Players_DB();
Player.player_id = 0;
list_Players_DB.add(Player.player_id);
Players_DB Player1 = new Players_DB();
Player1.player_id = 1;
list_Players_DB.add(Player1.player_id);
list_Players_DB.add("hmm");
for (int i=0; i < list_Players_DB.size();i ++)
{


System.out.println("Players_DB Object REF: " + list_Players_DB.get(i));
}




}
}

And get this output:

 

Players_DB Object REF: 0
Players_DB Object REF: 1
Players_DB Object REF: hmm
 
 
The whole idea is that I want to reference the objects themselves.. say an object has an ID. And I was using my first code block, I'd expect an output like
 
Players_DB Object REF: 0x0301
Players_DB Object REF: 0x0302
Players_DB Object REF: hmm
 
Right, from there When I want to retrieve one of the variables within these objects i'd do something like
 
for (int i=0; i < list_Players_DB.size();i ++)
{
String obj_ref = list_Players_DB.get(i);
System.out.println(obj_ref.player_id);
}

which should still print out this 

Players_DB Object REF: 0 

or what ever the value of that referenced objects variable is. :/

That's what I'm aiming for. 

 

Okay! So I tried out your method, this is giving me the results I want YES! XD

package myPackage;
import java.util.*;


public class Server {
public static void main(String[] args) 
{
//attempt 1
List list_Players_DB = new ArrayList();

System.out.println("Attempt 1:");
Players_DB Player = new Players_DB();
Player.player_id = 0;
list_Players_DB.add(Player.player_id);
Players_DB Player1 = new Players_DB();
Player1.player_id = 1;
list_Players_DB.add(Player1.player_id);
list_Players_DB.add("hmm");
for (int i=0; i < list_Players_DB.size();i ++)
{


System.out.println("Players_DB Object REF: " + list_Players_DB.get(i));
}




///Attempt 2


List<Players_DB> players = new ArrayList<Players_DB>();


players.add( new Players_DB());
players.add( new Players_DB());


Players_DB what = players.get(0);
Players_DB what1 = players.get(1);
System.out.println("");
System.out.println("");
System.out.println("Attempt 2:");
System.out.println(what);
System.out.println(what1);


}
}

Here's the results:

 

Attempt 1:
Players_DB Object REF: 0
Players_DB Object REF: 1
Players_DB Object REF: hmm
 
 
Attempt 2:
myPackage.Players_DB@7852e922
myPackage.Players_DB@4e25154f
 
As you can see I'm getting two different IDs now!  I noticed that I didn't put the correct variables in at the first attempt which is why I got the same ID twice, I changed that and also get two IDs there. I'm not sure which of the two approaches to take..
 
your method:
List<Players_DB> players = new ArrayList<Players_DB>();
players.add( new Players_DB());
 
mine:
List list_Players_DB = new ArrayList();
Players_DB Player = new Players_DB();
list_Players_DB.add(Player);
 
Hmm, I'm just happy with the results.  Thanks dmatter
Edited by Xer0botXer0
0

Share this post


Link to post
Share on other sites

Yeah it's actually a copy-paste bug in your first code snippet that's thrown you off. Look at this bit:

Players_DB Player = new Players_DB();
list_Players_DB.add(Player);
Players_DB Player1 = new Players_DB();
list_Players_DB.add(Player);
list_Players_DB.add("hmm");

Notice how you add Player twice and Player1 is created but not added?

It should have been this:

Players_DB Player = new Players_DB();
list_Players_DB.add(Player);
Players_DB Player1 = new Players_DB();
list_Players_DB.add(Player1);
list_Players_DB.add("hmm");

And you would get the result you expected!

0

Share this post


Link to post
Share on other sites

I'm not sure which of the two approaches to take..
 
your method:

List<Players_DB> players = new ArrayList<Players_DB>();
players.add( new Players_DB());
 
mine:
List list_Players_DB = new ArrayList();
Players_DB Player = new Players_DB();
list_Players_DB.add(Player);

 
They basically the same approach except that you create player as a local variable whereas I didn't bother because it's only going into the list. They're equivalent for this purpose. If you want to further utilise the player variable in ways other than just adding it to a list then stick with your approach.

You really should be using the <angle brackets> from my version however. These are called generic parameters (fairly advanced concept, learn about it later), but for now you just have to know that they're an important part of writing modern Java code and will save you from bugs and help keep your code clean from unnecessary "type casting".

0

Share this post


Link to post
Share on other sites

Posted (edited)

They're all regarding java, and can be boiled down to reveal many similarities, how ever they are not the same. If you are not going to contribute to the topic then don't post. You are trying to be helpful, how ever let me help you understand that I am browsing the web while creating threads because it's things I've found on the web that don't make sense, specifically mentioned above regarding multidimensional arrays requiring the same data type which is when someone in that thread (not my thread) recommended referencing objects in arrays instead. So I'm not being lazy, or trying to make your day a little more difficult by scrolling by an additional 400 pixels to find a post worthy of your assistance. I'm trying to learn Java. So far this works for me. 

 

Instead of getting all defensive and condescending, you might want to consider the possibility that my post was a suggestion worth taking to heart.

Putting aside for the moment that some of the threads could easily have been consolidated/merged (2 threads with questions regarding constructors, one of which is named "Constructor questions" seems fairly redundant, to name a quick example), the main point is something different entirely.

You are trying to learn a programming language. While doing so, you are posting many threads on subjects -- some fairly similar, some quite different -- in a very short time period. This, to me, is a strong indication that you are trying to progress too quickly; that you are moving onto new topics without having an adequate understanding of the stuff you're moving away from. There's nothing wrong with asking questions or creating threads, but with your current post frequency it doesn't seem like you're giving yourself enough time to digest and comprehend the things you are studying.

I think your time would be better invested by slowing down a bit and allowing yourself a greater opportunity to fully understand the subjects you are reading.

 

It might also be worth pointing you towards the chat room for questions which you might feel aren't big/substantial enough for them to warrant their own thread. It can also be quite helpful if you need clarifying something quickly, or help spotting a bug in a piece of code.

 

Edit: Or, you know, act like you know better even when asking for help, and down-vote in spite. That will also work.

Edited by Lactose
1

Share this post


Link to post
Share on other sites

Apologies for helping derail this topic but: I find it odd you are derailing a topic with a huge wall of text like that. There is nothing wrong with what he is doing, he is clearly learning in a very practical manner and you shouldn't dictate how people learn. A forum is also easier to reference than chat logs. GDnet doesn't(I hope) have an issue with 6 threads per day being created per account.

I down-voted you not because I disagree (It would be nice if he had the fundamentals down) but because you are essentially telling someone not to use the forum to seek help.

 

-2

Share this post


Link to post
Share on other sites

People are welcome to use the forum to seek help, but posting multiple threads at the same time on overlapping topics is the wrong way to do things. It means that anyone who wants to help has to try and follow each of those threads to understand what is being attempted. I appreciate that it can be frustrating when you're keen to make progress on your program and you have a lot of things that you want answers for, but the best approach is to post one clear thread, wait for the responses, act on them, and repeat.

-1

Share this post


Link to post
Share on other sites
3 hours ago, CRYP7IK said:

you shouldn't dictate how people learn.

What I posted was a suggestion that he should consider doing things differently. I think my reasons for suggesting it was made clear in my previous post (which was a "huge wall of text" in order to clarify the much shorter post which just got a dismissive and condescending reply).

I did not forbid him from creating 6+ threads per day, nor did I tell him to swap to only using the chat instead of creating threads. I said that he might want to reconsider his approach (with, I think, good reasons why), and I mentioned the chat existing because it can be helpful in ways the forum is not, due to its more direct nature.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0