Advertisement

How to differentiate between objects | do objects have ids

Started by June 12, 2017 12:53 PM
15 comments, last by Lactose 7 years, 5 months ago

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.

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.

Advertisement

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?

Hello to all my stalkers.

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.

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.


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

Advertisement

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.

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.

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()); 
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

This topic is closed to new replies.

Advertisement