• Post Reply Bookmark Topic Watch Topic
  • New Topic

array dereferences, explain this term please  RSS feed

 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just started the Cattle Drive. My nitpick included a comment about how many array dereferences I had created. I understand arrays but have not worked much in C or Java. I don't know -- or understand -- the term array dereferences. I don't understand if having array dereferences is good or bad, or the problem was how many I had. I think once I understand the term the rest will become clear. Thanks.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably best discussed in the Cattle Drive forum. Moving.

I understand this thread has been moved before. All right, moving back. Sorry.
 
Marilyn de Queiroz
Sheriff
Posts: 9082
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have an array, you may reference the contents of that array by using the index (the position of the element in the array) ... like

String[] names = {"myName", "yourName"};

where
names[0] is the first element (i.e. myName)
names[1] is the second element (i.e. yourName)

Each time you get an element using its index, you are dereferencing that array.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked in Wikipedia under "dereference" and couldn't find a specific use for arrays. In C it means to get the contents of a pointer with the * operator, but * isn't used for that purpose in Java.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seeing Marilyn's answer, that might mean you have been using to many for loops and not enough for-each loops.
 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marilyn.
What I am most confused about is why is it called dereferencing the array instead of referencing the array? The word dereferencing makes me feel that when the array is touched with the index reference something happens to the array like it's DEcommissioned or DEstroyed.

Regarding what Campbell said.
Say I have an array String args[]. Instead of reading the same array element args[0] multiple times in a for loop is there a performance advantage to storing the args[0] information into a variable and using the variable in the for loop instead?

 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Margaret:

Simply, no, there isn't really a performance benefit. At this point, you probably should be focusing on getting your code to work, rather than on performance.

John.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a term I hear often about Java; it is often used in C however. Dereferencing means getting the contents of a memory address.
 
paul wheaton
Trailboss
Posts: 22577
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marilyn asked me to pop in here.

Suppose you have an array of 20 strings and each string is 300 characters long.

What you really have is an array of 20 string references.

A "reference" is kinda like a pointer. Each reference uses up 32 bits (there can be quibbling about 32, but I cannot, at the moment, think of an instance it is not 32, so I'll just say 32). 32 bits is 4 bytes. Each string is about 700 bytes.

So the array is 20 references with each reference being 4 bytes. The array has some object overhead, so the total size might be something like 150 bytes.

Suppose the array is called "poems". And I wan't the seventh poem: "poems[6]" - this is sometimes called a "dereference" because I am resolving the reference to get the actual object. Sort of.

Campbell is quite right in that this has a lot more to do with C than java.

The important thing is that If you are gonna mess with a string several times, if you say

if (poems[7].contains"cheese")
{
System.out.println("a cheesy poem: " + poems[7]);
}

then this code contains two "dereferences", and

String poem = poems[7];
if (poem.contains"cheese")
{
System.out.println("a cheesy poem: " + poem);
}

contains one dereference.


 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Paul! You wrote
this is sometimes called a "dereference" because I am resolving the reference to get the actual object.

That makes sense now.

If I am understanding correctly, based on what you said and your examples, it is better to only resolve the array reference once. If the same array value is going to be repeated -- say in a for loop -- then storing the array value to the String and then repeating the String inside the for loop is the best practice.
 
paul wheaton
Trailboss
Posts: 22577
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct.

Each time you ask for poems[6] then the JVM has to do a little computing to get you what you seek.

 
margaret gillon
Ranch Hand
Posts: 335
6
Linux Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everyone for the help.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!