Java pass variable as reference

Integer, Float, String, List passed as reference in JAVA example source code:

// List passed as reference JAVA
List<Integer>list = new ArrayList<Integer>();
 
public void fc (List<Integer>listRef){
listRef.add(7);
listRef.add(5);
}
fc(list); // 7, 5 
 
 
// String passed as reference JAVA
	public void mutate(AtomicReference<Object> ref) { ref.set("Goodbye"); }
 
		AtomicReference<Object> ref = new AtomicReference<Object>("Hello");
		mutate(ref);
		System.out.println(ref.get()); //Goodbye!
             String s = (String) ref.get();
 
 
// Integer passed as reference JAVA
	private static void mutate(AtomicReference<Object> ref) { ref.set(7); }
 
	//public static void main(String[] arg) {
		AtomicReference<Object> ref = new AtomicReference<Object>(5);
		mutate(ref);
		System.out.println(ref.get()); // 7
int n = (Integer) ref.get();
 
 
// Float passed as reference JAVA
	private static void mutate(AtomicReference<Object> ref) { ref.set(14.8f); }
 
	//public static void main(String[] arg) {
		AtomicReference<Object> ref = new AtomicReference<Object>(12.1f);
		mutate(ref);
		float f = (Float) ref.get();
		System.out.println(f); //14.8
This entry was posted in Android Examples Code and tagged , . Bookmark the permalink.


Comments are closed.

3,177 views