pass-by-value

Can we use pass by reference in java? If No How does java.util.Arrays.sort works?

心已入冬 提交于 2019-12-12 11:41:04
问题 I used to think Java supports both pass by value and passby reference but i came accross many discussions like Java is always pass by value, with no exceptions, ever . Java is always pass-by-value Java always passes arguments by value NOT by reference. Java passes references by value.So you can't change the reference that gets passed in. If java only supports pass by value how does java.util.Array.sort() or Collections.sort(unsortList) work? int iArr[] = {2, 1, 9, 6, 4};// sorting array

Copy-on-modify semantic on a vector does not append in a loop. Why?

我的梦境 提交于 2019-12-12 10:35:28
问题 This question sounds to be partially answered here but this is not enough specific to me. I would like to understand better when an object is updated by reference and when it is copied. The simpler example is vector growing. The following code is blazingly inefficient in R because the memory is not allocated before the loop and a copy is made at each iteration. x = runif(10) y = c() for(i in 2:length(x)) y = c(y, x[i] - x[i-1]) Allocating the memory enable to reserve some memory without

swap two numbers using call by reference

混江龙づ霸主 提交于 2019-12-12 08:56:26
问题 Can we swap two numbers in Java using pass by reference or call by reference? Recently when I came across swapping two numbers in Java I wrote class Swap{ int a,b; void getNos(){ System.out.println("input nos"); a = scan.nextInt(); b = scan.nextInt(); // where scan is object of scanner class } void swap(){ int temp; temp = this.a; this.a = thisb; this.b = this.a; } } In the main method I call the above mentioned methods and take two integers a , b and then using the second method I swap the

Pass by value vs pass by reference for a Perl hash

独自空忆成欢 提交于 2019-12-12 08:09:00
问题 I'm using a subroutine to make a few different hash maps. I'm currently passing the hashmap by reference, but this conflicts when doing it multiple times. Should I be passing the hash by value or passing the hash reference? use strict; use warnings; sub fromFile($){ local $/; local our %counts =(); my $string = <$_[0]>; open FILE, $string or die $!; my $contents = <FILE>; close FILE or die $!; my $pa = qr{ ( \pL {2} ) (?{ if(exists $counts{lc($^N)}){ $counts{lc($^N)} = $counts{lc($^N)} + 1; }

Passing a pointer to a function

浪子不回头ぞ 提交于 2019-12-12 05:49:50
问题 I was trying to implement BST using C++ , so i tried this: #include <iostream> #include <stdlib.h> struct node { int value; node* left; node* right; }; void insert(node *cur , int val) { if(!cur) { cur = new node; cur->value = val; cur->left = NULL; cur->right = NULL; return; } if(val <= cur->value) insert(cur->left , val); else insert(cur->right , val); } using namespace std; int main() { node *root = NULL; insert(root , 20); insert(root , 21); cout<<(*root).value; return 0; } but I have a

update actual values using pass by value

你。 提交于 2019-12-12 03:28:38
问题 I want to know if there is any way to update original values by using pass by value. Also i want to know if I can return updated address back to main Here I dont want to use pass by reference to swap original values in main. #include<stdio.h> int a = 100; int f =200; int *p, * q; swap(int, int); main() { printf("%d %d\n", a,f); swap(a, f); printf("%d %d\n",a ,f); //values remain same here how to change them } swap(int a, int f) { int t; p=&a; q=&f; t=*p; *p=*q; *q= t; printf("%d %d\n",a ,f);

when I use linkedlist in java, it seems not to “pass by value” anymore, anyone could explain?

旧街凉风 提交于 2019-12-11 17:28:47
问题 for example: public static void main(String[] args)throws InterruptedException { List<String> l1 = new LinkedList<String>(); String[] s1 = {"aa","bb","cc","dd"}; for(String k : s1) l1.add(k); removeStuff(l1,1,2); printList(l1); } private static void printList(List<String> l) { for(String b : l){ System.out.print(b + ' '); } private static void removeStuff(List<String> l, int i, int j) { l.subList(1, 3).clear(); } it will print aa dd instead of aa bb cc dd. So I am just wondering that, since

Java - Creating a Pseudo Native Type

女生的网名这么多〃 提交于 2019-12-11 17:26:04
问题 In stock trading, quantities are usually integers (e.g. 5x shares, 10x options, etc). With cryptocurrencies, quantities are fractions (e.g. 0.01 bitcoin). In both scenarios, there is typically a minimum unit (e.g. multiples of 100x shares). I would like to wrap this logic in a Quantity class. However: Java native types (e.g. double ) are final , so I cannot extend them Java does not support operator overloading, so arithmetic will be ugly Java does not support typedefs, so I cannot wrap a

What does the marshaler expect from a custom marshaler for a returned value?

左心房为你撑大大i 提交于 2019-12-11 16:46:51
问题 I am trying to implement a custom marshaler for a C# delegate returned value [return: MarshalAs(UnmanagedType.CustomMarshaler,MarshalType = "MyCustomMarshaler")] public delegate MSyntax SyntaxCreator(); In C++ the MSyntax type is a normal C++ class. In C# the MSyntax type is a normal C# class (not a struct) that has an internal pointer (using IntPtr) to the C++ one. So I wrote the method MarshalManagedToNative like this: public IntPtr MarshalManagedToNative(Object ManagedObj) { MSyntax

Some updates are discarded out of a method

给你一囗甜甜゛ 提交于 2019-12-11 14:23:11
问题 I have a set of objects inherits from one BaseObject which have some global primitive properties (indexes etc). Now, I have two objects, one of them (the target one) have values only for the base properties and the other (the source, which is one of a set of objects) have values for all other the properties (retrieved from a 3rd party app), but the base one's. I'm trying to copy all the source object's properties to the target one, but keep the target's base properties' values. In other words