Java pass by value explained

Hey friends!

pass by value means the actual value is passed on by creating a copy of it.

Pass by reference means that a number (called a memory address) is passed on, this address defines where the actual value is stored.

I will try to explain why java is pass by value by taking an example class Person.

public class Person {
  private String name;
  private int age;
  
  Person(String name, int age){
    this.name = name;
    this.age = age;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  
}

Through out this post I will modify the person object in different scenarios and explain whats happening behind the scenes.

Initial state: Let’s create a person object with name `Tom Cruise` and age 57.

public class ReferenceTest {public static void main(String[] args) {
    Person person = new Person("Tom Cruise", 57);
    System.out.println(person.getName());
  }}

Output: Tom Cruise

Scenario 1:

Now let’s try to change the name of the person object.

  Person person = new Person("Tom Cruise", 57);
  modifyPerson(person);
  System.out.println(person.getName());
  
  private static void modifyPerson(Person person) {
    person.setName("Brad Pitt");
  }

Output: Brad Pitt

No surprises.

Scenario 2:

Let’s now modify the state of person. I will try to nullify the object.

//excluded other code for brevity
private static void modifyPerson(Person person2) {
 person2 = null;
}

Output: Tom Cruise

What ? Yes that’s because java passes a copy of reference (don’t get confused this with physical memory address reference)pointing to the actual object. So what ever you do to change the state of the reference the actual object remains untouched. The image will explain more. person is untouched as person2 is a reference copy of person.

Let’s do one last thing to confirm this.

//excluded other code for brevity
private static void modifyPerson(Person person2) {
 person2 = new Person("Johnny Depp", 56);
}

Output: Tom Cruise

Still no change. Look at the image to see whats happening.

person2 is pointing to a whole new object in the memory.

Same thing applies to primitive data types.

Last but not the least you can pass the reference of a variable by adding an & in front of the method argument, but we can just return the value instead of doing this.

Hope this helps!

When to define default constructor in java

In Object Oriented Programming calling constructor is the first step of initialization an object. There are 2 types of constructors – default and parameterized.

Default – Constructor that take zero argument is called Default Constructor. Every class needs a default constructor. A default (no-argument) constructor is automatically created only when you do not define one yourself.

Parameterized – Constructor that take arguments are called parameterized Constructors.

They can be added as per our requirement.

When you declare a parameterized constructor you must add default constructor as java stops creating one.

How to configure postgres data base in a spring boot application

There are 2 steps involved in configuring postgres sql data base to a spring boot application assuming we already have data base setup.

  1. Add postgres sql dependency to pom.xml.

postgres_pom

2. We need to tell Spring container to look into postgres by changing the application.properties.

application.properties

So the list of possible options for ddl-auto are

  • validate: validate the schema, makes no changes to the database.
  • update: update the schema.
  • create: creates the schema, destroying previous data.
  • create-drop: drop the schema when the SessionFactory is closed explicitly, typically when the application is stopped.

I added spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true as the server is throwing following warning while startup.

java.sql.SQLFeatureNotSupportedException: Method org.postgresql.jdbc42.Jdbc42Connection.createClob() is not yet implemented.

Now restart server and you should see successful db connection logs. When the connection is successful spring container tries to validate the entities in the project to the db tables.

Happy coding!