Total members 11894 |It is currently Thu Nov 21, 2024 2:17 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





You should be able to see the difference between passing the array as parameter in following two cases :
1. Assigning objects to the array of reference before the calling of function: .

Code:
  public static void main(String[] args) {
  byte[] arr=new byte[3];
 byte[] x=     calc(arr);
       // in this case x = arr ( not null);
    }

    public static  byte[] calc (byte[] arr)
    {
        //do something here. 
        return arr; 
        
    
} 

2. Assigning objects to the array of references in the called of function:
Code:

     public static void main
(String[] args) {
        byte[] arr = null;
        byte[] x = calc(arr);
      // in this case arr = null

        System.out.println(x.length);
        System.out.println(arr);
    }

    public static byte[] calc(byte[] arr) {
        arr = new byte[3];
        //do something here.
        return arr;

    }
 


The output of the second code snippet is :
Code:
3
null


You notice that the reference "arr" is still null in the main function



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

updated.


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Passing arrays as function parameter in java
 Passing Enum as Type Parameter to method     -  
 JSP Passing Arrays to Methods     -  
 Passing an Argument to a Function by Value     -  
 Passing Pointers to function example     -  
 passing string value from java to .exe file     -  
 Java script time parameter     -  
 Arrays in java     -  
 Multidimensional arrays in Java     -  
 How to compare two arrays in java     -  
 Comparing Arrays in java     -  



Topic Tags

Java Arrays
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com