Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array copying or assigning? Are they same?

 
Xiaolong Bao
Greenhorn
Posts: 16
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day guys....

Please can anybody explain me:

what is the essential of this statement: char[] chars = createArray();

The array chars gets an returned array from the createArray() method,
is this an array copying or simply an assignment?
Do the arrays 'chars' and 'lowercaseLetters' point to the same memory location or they are two independent arrays??

Thanks!!!

 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Xiaolong. Welcome to the Ranch!

Arrays in Java are reference types. So the createArray() method is returning a reference to the array created - only one actual array exists in memory.

You can confirm it by looking at the following output, which makes two variables pointing to the array, then modifies it via one and checks to see if it's affected what the other reference sees:
 
Xiaolong Bao
Greenhorn
Posts: 16
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Hi Xiaolong. Welcome to the Ranch!

Arrays in Java are reference types. So the createArray() method is returning a reference to the array created - only one actual array exists in memory.

You can confirm it by looking at the following output, which makes two variables pointing to the array, then modifies it via one and checks to see if it's affected what the other reference sees:


Hi Matthew,

I got the point, really appreciate your reply.

There's so much to learn for me....I just need to keep going...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic