Linked Questions

250 votes
7 answers
305k views

Are arrays passed by value or passed by reference in Java? [duplicate]

Arrays are not a primitive type in Java, but they are not objects either, so are they passed by value or by reference? Does it depend on what the array contains, for example references or a primitive ...
Froskoy's user avatar
  • 3,045
55 votes
6 answers
29k views

Java is NEVER pass-by-reference, right?...right? [duplicate]

Possible Duplicate: Is Java “pass-by-reference”? I found an unusual Java method today: private void addShortenedName(ArrayList<String> voiceSetList, String vsName) { if (null == vsName)...
Troy Nichols's user avatar
  • 1,234
46 votes
6 answers
37k views

Is Java really passing objects by value? [duplicate]

Possible Duplicate: Is Java pass by reference? public class myClass{ public static void main(String[] args){ myObject obj = new myObject("myName"); changeName(obj); ...
MBZ's user avatar
  • 479
37 votes
10 answers
36k views

Java: Why does this swap method not work? [duplicate]

I have the following code: public class Main { static void swap (Integer x, Integer y) { Integer t = x; x = y; y = t; } public static void main(String[] args) { ...
witzar's user avatar
  • 770
33 votes
8 answers
38k views

java array pass by reference does not work? [duplicate]

I thought almost all languages, including java, pass array into function as reference (modifiable). But somehow it does not work here, and the testArray is still 1,2,3 with size of 3. Strange enough,...
user avatar
40 votes
4 answers
94k views

Does a List object get passed by reference? [duplicate]

Possible Duplicate: Is Java pass-by-reference? Does a List object get passed by reference? In other words, if I pass an ArrayList (java.util.ArrayList) object to a class, will it be automatically ...
Kirill Kulakov's user avatar
24 votes
11 answers
70k views

Confused, whether java uses call by value or call by reference when an object reference is passed? [duplicate]

public class program1{ public static void main(String args[]){ java.util.Vector vc=new java.util.Vector(); vc.add("111"); vc.add("222"); functioncall(vc); ...
user617597's user avatar
13 votes
9 answers
8k views

Pass-by-value (StringBuilder vs String) [duplicate]

I do not understand why System.out.println(name) outputs Sam without being affected by the method's concat function, while System.out.println(names) outputs Sam4 as a result of the method's append ...
Helenesh's user avatar
  • 4,379
12 votes
7 answers
15k views

Java Incremental operator query (++i and i++) [duplicate]

I have the following code: public class Book { private static int sample1(int i) { return i++; } private static int sample2(int j) { return ++j; } public static ...
misguided's user avatar
  • 3,809
27 votes
5 answers
8k views

Why is an ArrayList parameter modified, but not a String parameter? [duplicate]

public class StackOverFlow { public static void main(String[] args) { ArrayList<String> al = new ArrayList<String>(); al.add("A"); al.add("B"); ...
Naresh's user avatar
  • 313
18 votes
6 answers
23k views

Why do arrays change in method calls? [duplicate]

When I write like this: public class test { void mainx() { int fyeah[] = {2, 3, 4}; smth(fyeah); System.out.println("x"+fyeah[0]); } void smth(int[] fyeah) ...
good_evening's user avatar
  • 21.8k
13 votes
8 answers
38k views

swapping of objects in java [duplicate]

I have studied that java is pass by reference but when I execute following code the strings are not swapped in main method why? static void swap(String s1, String s2){ String temp = s1; s1=s2;...
sabu's user avatar
  • 2,107
14 votes
5 answers
53k views

Java - Object state does not change after method call [duplicate]

Beginner java question, but I cannot understand how call-by-Value ( or Reference ) is working in the example below - How come the String value is not modified after it exits the method while my ...
Anand Hemmige's user avatar
14 votes
4 answers
82k views

Changing the values of variables in methods, Java [duplicate]

I have a question about changing the values of variables in methods in Java. This is my code: public class Test { public static void funk(int a, int[] b) { b[0] = b[0] * 2; a = ...
user42155's user avatar
  • 49.7k
13 votes
4 answers
48k views

Java Pass By Value and Pass By Reference [duplicate]

i have done the following sample with to check my knowledge import java.util.Map; public class HashMap { public static Map<String, String> useDifferentMap(Map<String, String> ...
Joe's user avatar
  • 4,542

15 30 50 per page
1
2 3 4 5
131