Java and C# and java script call by value and call by Reference +Sample Code

 

        We have two simple method one method in java one method in c# ,both of this two method get one parameter and return parameter+1 after it,I change this sample for test a object input not primitive value.

.c#         

image00

Java :

image02

and java script:

image10

image03

also,you see our parameter not change after call function sum.

we change param type from integer to a class with a one property as integer .

C# :

image08

Java :

image05

    Javascript:

image04

image07

now we change the code and create new instance in sum method of input object:

C#:

image06

Java :

image09

now I change Call by value to reference :

C#:

image01

Java :

image11

so we have Error here , You simply can’t do that in Java.)

since Java doesn’t support pointers …

but in javascript :

  1. Javascript is always pass by value, but when a variable refers to an object (including arrays), the “value” is a reference to the object.
  2. Changing the value of a variable never changes the underlying primitive or object, it just points the variable to a new primitive or object.
  3. However, changing a property of an object referenced by a variable does change the underlying object.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s