StackStalk
  • Home
  • Java
    • Java Collection
    • Spring Boot Collection
  • Python
    • Python Collection
  • C++
    • C++ Collection
    • Progamming Problems
    • Algorithms
    • Data Structures
    • Design Patterns
  • General
    • Tips and Tricks

Saturday, July 5, 2014

Java is pass by value

 July 05, 2014     Java     No comments   

Is Java pass by value (or) pass by reference is often a topic of confusion. This article clarifies with simple examples.
  1. Java is always pass by value.
  2. Primitive data type arguments like int or double are passed into methods by value. Any changes to the value of the parameters is limited to the scope of the method. When method returns any changes are lost.
  3. When objects are used Java copies and passes the references by value. This is often confused and best explained with examples. In this example below arg1 and arg2 point to the original StringBuilder objects and they hold a reference. In the swap1 function we are actually swapping the reference values between arg1 and arg2 and hence the original contents of the StringBuilder objects in main are not modified. In the swap2 function are actually modifying the content of objects to which arg1 and arg2 point and hence the content of objects in main are modified.
public class PassValueTest {
 
   private static void swap1(StringBuilder arg1, StringBuilder arg2) {
     StringBuilder temp = arg2;
     arg2 = arg1;
     arg1 = temp;  
   }
 
   private static void swap2(StringBuilder arg1, StringBuilder arg2) {
     String temp = arg2.toString();
     arg2.replace(0, arg2.length(), arg1.toString());
     arg1.replace(0, temp.length(), temp);
   }
 
   public static void main(String[] args) {
     StringBuilder str1 = new StringBuilder("Hello");
     StringBuilder str2 = new StringBuilder("World");
     System.out.println("Before swap = " + str1 + " " + str2);
  
     swap1(str1, str2);
     System.out.println("After swap1 = " + str1 + " " + str2);
  
     swap2(str1, str2);
     System.out.println("After swap2 = " + str1 + " " + str2);
   }
}
This example produces the following output.
Before swap = Hello World
After swap1 = Hello World
After swap2 = World Hello
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Follow @StackStalk
Get new posts by email:
Powered by follow.it

Popular Posts

  • Python FastAPI file upload and download
    In this article, we will look at an example of how to implement a file upload and download API in a Python FastAPI microservice. Example bel...
  • Avro Producer and Consumer with Python using Confluent Kafka
    In this article, we will understand Avro a popular data serialization format in streaming data applications and develop a simple Avro Produc...
  • Monitor Spring Boot App with Micrometer and Prometheus
    Modern distributed applications typically have multiple microservices working together. Ability to monitor and manage aspects like health, m...
  • Server-Sent Events with Spring WebFlux
    In this article we will review the concepts of server-sent events and work on an example using WebFlux. Before getting into this article it ...
  • Accessing the Kubernetes API
    In this article, we will explore the steps required to access the Kubernetes API and overcome common challenges. All operations and communic...
  • Python FastAPI microservice with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Scheduling jobs in Python
    When developing applications and microservices we run into scenarios where there is a need to run scheduled tasks. Examples include performi...
  • Using Tekton to deploy KNative services
    Tekton is a popular open-source framework for building continuous delivery pipelines. Tekton provides a declarative way to define pipelines ...

Copyright © StackStalk