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
  • Share This:  
Newer Post Older Post Home

0 comments:

Post a Comment

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

Popular Posts

  • 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 ...
  • Implement caching in a Spring Boot microservice using Redis
    In this article we will explore how to use Redis as a data cache for a Spring Boot microservice using PostgreSQL as the database. Idea is to...
  • 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...
  • Spring Boot with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Getting started with Kafka in Python
    This article will provide an overview of Kafka and how to get started with Kafka in Python with a simple example. What is Kafka? ...
  • Getting started in GraphQL with Spring Boot
    In this article we will explore basic concepts on GraphQL and look at how to develop a microservice in Spring Boot with GraphQL support. ...

Copyright © StackStalk