Is Java pass by value in case of objects also? -
this question has answer here:
i have searched lot , every found java pass value still not satisfied answers. below code. here if passing object of hashmap
getting updated value while in case of integer not that. difference between these both. how pass value working in both cases-
public static void main(string[] args) { hashmap hm = new hashmap(); system.out.println(hm.size()); //print 0 operation(hm); system.out.println(hm.size()); //print 1 ; why it's updating hashmap. integer c = new integer(3); system.out.println(c); //print 3 getval(c); system.out.println(c); //print 3: why it's not updating integer hashmap. } public static void operation(hashmap h) { h.put(“key”, “java”); } public static void getval(integer x) { x = x + 2; system.out.println(x); }
here if passing object of hashmap
you aren't. you're passing reference hashmap. reference passed value.
Comments
Post a Comment