0

I'd two code snippets:

First

class PassByTest{
    public static void main(String... args){
        PassByTest pbt=new PassByTest();
        int x=10;
        System.out.println("x= "+x);
        pbt.incr(x);//x is passed for increment
        System.out.println("x= "+x);//x is unaffected
    }
    public void incr(int x){
        x+=1;
    }
}

In this code the value of x is unaffected.

Second

import java.io.*;
class PassByteTest{
    public static void main(String...args) throws IOException{
        FileInputStream fis=new FileInputStream(args[0]);
        byte[] b=new byte[fis.available()];
        fis.read(b);//how all the content is available in this byte[]?

        for(int i=0;i<b.length;i++){
            System.out.print((char)b[i]+"");
            if(b[i]==32)
                System.out.println();
        }
    }
}

In this all the content of file is available in the byte[] b.
How and Why?

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
  • int's are primitives and byte[]'s are reference types? – Lior Cohen Feb 22 '12 at 01:40
  • 3
    @YuriyZubarev All types, both primitive and reference are passed by value in Java – Hunter McMillen Feb 22 '12 at 01:40
  • 2
    Read the [many](http://stackoverflow.com/questions/498747/java-pass-by-value-reference-variables) [related](http://stackoverflow.com/questions/2027/pass-by-reference-or-pass-by-value) [questions](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) on this topic. – kabuko Feb 22 '12 at 01:41
  • @YuriyZubarev: what do you mean by `doesn't guarantee to read the whole file. You need to loop.`? – Mohammad Faisal Feb 22 '12 at 01:44

2 Answers2

9

Java is always pass-by-value.

In the second case, though, you are passing a reference by-value (an array is an object, and Java objects are always accessed via references). Because the method now has a reference to the array, it is free to modify it.

Community
  • 1
  • 1
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
4

Java is pass by value - always.

Here's a reference that quotes James Gosling, who should be authoritative enough for anyone:

From the authors of Java: "There is exactly one parameter passing mode in Java - pass by value - and that helps keep things simple." The Java Programming Language, 2nd ed. by Ken Arnold and James Gosling, section 2.6.1, page 40, 3rd paragraph.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Its pass by reference if you are using objects, but pass by value when using primitive types. see here http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html – Jono Apr 03 '13 at 20:42
  • Wrong, wrong, wrong.....you're completely, utterly wrong, jonney. You aren't even wrong - Pauli. Are you going to disagree with the words of James Gosling, the guy who invented the language? Hasn't changed since day 1. – duffymo Apr 03 '13 at 20:44
  • Then that article from Sun java oracle is wrong then. Also this article wrong too http://www.javacertificate.net/passbyvalue.htm ? Both says the same thing ie objects in a method parameter are passed as references. if you want objects passed as value, make it immutable – Jono Apr 03 '13 at 21:03
  • No, I'm sure the article is correct - you read it wrong. Objects are not passed. References are passed, and they're passed by value. The passing mechanism doesn't change because an object is immutable or not. You fail to appreciate the difference. It's subtle, but true. – duffymo Apr 03 '13 at 21:37
  • Yea i had to read 10 articles to confirm what you said and the confusen lies with your explanation ie in theory, object are not passed, its the reference that are but those references contains a pointer value hench it is actually passing some kind of value – Jono Apr 04 '13 at 09:04
  • Or you could have just read what I wrote and been done with it. My explanation is ONE SENTENCE: "Java is pass by value - always"! The rest is a quote from the guy that invented the language. – duffymo Apr 04 '13 at 10:02
  • But u never explained WHY its always pass by value and WHY documents still mention "pass by reference" on objects – Jono Apr 04 '13 at 11:03
  • Why? Because too many people simply don't understand how it works or are too lazy to make the distinction. "It's the same thing", they'll say to themselves to justify an incorrect statement. – duffymo Apr 04 '13 at 11:15
  • You wasnt born understanding how it works. like me, had to read and understand it so why are you having a go at me for reading and understanding the concept in the same manner every human being alive does on just about anything in life? You have come across as a arrogant spiteful person. I have not. i simply challenged your initial comment in a mature none aggressive manner and you responded imature i.e "why why why" Serious? This is not the place to interact with such people so this will my last comment and you can reply to your usual immature comments. best of luck – Jono Apr 04 '13 at 13:55
  • You won't last long here or in business being that thin skinned. You made an incorrect comment about a year old answer. This has been rehashed many times, in many places. Learn from it and move on. – duffymo Apr 04 '13 at 14:18