Passing data to methods – Java Concepts

Passing data to methods

Java is pass-by-value language.

A Java Method can have 2 type of arguments –

  •        Primitives
  •        Ref Type

Passing arguments to Method –

Primitives : Xerox copy of variable will be passed as an argument

 Example –

public class Test {

	public static void squareValue(int num) {
		num = num * num;
	}

	public static void main(String[] args) {
		int num = 10;
		System.out.println("num: " + num);

		squareValue(num);
		System.out.println("num: " + num);

	}
}

---------------
OUTPUT –
num: 10
num: 10

 Ref Type : Address of object stored in Ref type will be passed as an argument.

Example –

public class Test {

	public static void uName(String name) {
		name = name.concat(" Done");
	}

	public static void uName(StringBuilder name) {
		name = name.append(" Done");
	}

	public static void main(String[] args) {
		String name = "Neeraj";
		StringBuilder name1 = new StringBuilder("Baliyan");

		System.out.println("name: " + name);
		System.out.println("name1: " + name1);

		uName(name);
		uName(name1);

		System.out.println("name: " + name);
		System.out.println("name1: " + name1);
	}
}


---------------
OUTPUT –
name: Neeraj
name1: Baliyan
name: Neeraj
name1: Baliyan Done

–***>>> Be careful when handling ref type arguments. Operation on ref type may create new object (Immutable e.g. String, Primitive Wrapper Class etc) or operate on existing object (Mutable e.g StringBuilder etc)

Passing data to methods

 If method is updating state of object &  Input Ref type is corresponding to Mutable Object –

It will not create a new object.You can confirm that Ref type passed as argument is still holding original address.


public class MutableInput {
	public static StringBuilder name = new StringBuilder("Neeraj");

	public static void updateName(StringBuilder name) {
		name = name.append(" Kumar");// it will not create new object.Still //name will have original address
	}

	public static void main(String[] args) {
		System.out.println("Before processing " + name); // Neeraj

		updateName(name);

		System.out.println("After processing " + name); // Neeraj Kumar
	}
}

  If method is updating state of object & &  Input Ref type is corresponding to Immutable Object

Method should return updated Ref Type i.e. address of new object and we should assign it to original Ref Type in caller.


public class MutableInput {
	public static StringBuilder name = new StringBuilder("Neeraj");

	public static void updateName(StringBuilder name) {
		name = name.append(" Kumar");// it will not create new object.Still //name will have original address
	}

	public static void main(String[] args) {
		System.out.println("Before processing " + name); // Neeraj

		updateName(name);

		System.out.println("After processing " + name); // Neeraj Kumar
	}
}

For Your kind attention Please –

If you are preparing for Java Certificate exam, You can take a Practice Test by clicking here. Practice Test have real exam questions. Thus it will ensure that you pass your Certificate Exam without any surprise.

  If you want to crack an interview for the Java Developer role, a demo interview will fortify your chances. Thus, Book an interview preparation session.

Workspace in use - Eclipse Error

#ITCertification #AWS #Google #Azure #ITJobs #ITCareer

Leave a Comment

Your email address will not be published.

Shopping Cart