Java Programming Questions | Java sample coding problems

This is a post dedicated to Java programming challenges that can be useful to refresh and validate your knowledge of Java.

Question 1

Write the output of the following code?

nt x = 1, y = 1, z = 0;
if (x == y | x < ++y) {
z = x + y;
} else {
z = 1;
}
System.out.println(z);

Question 2

Write the output of the following code?

nt x = 1, y = 1, z = 0;
if (x == y | x < ++y) {
z = x + y;
} else {
z = 1;
}
System.out.println(z);

Not Sure why the out of questions 1 and 2 are different read about Java short circuit and full evaluation.

Question 3

Write the output of the following code?

int period = 1, amt = 10;
float rate = 0.5f;
switch (period) {
case 4:
	amt += amt * rate;
case 3:
	amt += amt * rate;
case 2:
	amt += amt * rate;
case 1:
	amt += amt * rate;

	System.out.println("final amt : " + amt);
}

Also, try to run the above code after modifying case 4 as below

case 4:
    amt += amt * rate;
    break;

Question 4

Write the output of the following code?

public static void main(String... strings) {
	int localVal = 5;
	calculate(localVal);
	System.out.println(localVal);
}

public static void calculate(int calcVal) {
    calcVal = calcVal * 100;
}

Question 5

Write the output of the following code?

String str = ""; 
str  = 1 + 1 + "u";
System.out.println(str);
str  = "u" + 1 + 1;
System.out.println(str);
str  = "u" + (1 + 1);
System.out.println(str);

Happy Learning !!

Java Short-Circuit Evaluation | Java Operators

There are so many operators available in Java. Here we will discuss Java Short circuit operators and how they evaluate the given expression. As a Java developer, you must know these basics and how to use them effectively.

OperatorEvaluation
&& and ||Short-Circuit
& and |Full

Operator && and || are known as short-circuit operators and how they evaluate an expression is known as short circuit evaluation.

What exactly Short-circuit evaluation is?

The short circuit evaluation enables you to not evaluate the right-hand side AND or OR expression when the overall result of the expression can be predicted from the left-hand side value.

So for AND (&&), both parts must be true for the overall results to be true. For OR (||), one part must be true, either one of these. The other may be false, it doesn’t matter. The overall result will be true. And for exclusive OR (^), either part has to be true, and the other part has to be false for the overall result to become true.

Now, instead of the double ampersand (&&) and double vertical line (||), there are also single ampersand and single vertical line operators available, and they’re known as full evaluation versus the double ampersand and double vertical line, which are known as short circuit evaluation.

Refer to the below picture and see how the evaluation of these operators happens.

Java Short-Circuit Evaluation

Example

Now let’s understand this using the below code snippet.

 
int a = 3, b = 2;
boolean c = false;

c = (a > b && ++b == 3); 
System.out.println("value of c is "+c + " and b is "+b);

c = (a > b && ++b == 3);
System.out.println("value of c is "+c + " and b is "+b);

c = (a > b || ++b == 3); 
System.out.println("value of c is "+c + " and b is "+b);

c = (a < b || ++b == 3); 
System.out.println("value of c is "+c + " and b is "+b);

c = (a < b | ++b == 3); 
System.out.println("value of c is "+c + " and b is "+b);

c = (a > b & ++b == 3);
System.out.println("value of c is "+c + " and b is "+b);

c = (a < b ^ ++b == 3);
System.out.println("value of c is "+c + " and b is "+b);

c = (a > b ^ ++b == 3);
System.out.println("value of c is "+c + " and b is "+b);

//Output
value of c is true and b is 3
value of c is false and b is 3
value of c is false and b is 4
value of c is true and b is 4
value of c is true and b is 5
value of c is false and b is 6
value of c is true and b is 7
value of c is false and b is 8

Note: You don’t have to put round brackets around Booleans, but that may make your code a little bit more readable, just sort of from a visual point of view so the Boolean expression will stand out.

Output Explantion

Let’s understand the output of each line.

 c = (a > b && ++b == 3); 

For AND condition, both parts of the expression need to be true for the overall results to become true. So we have to evaluate both parts to tell that.

If you look at a Boolean expression, the double ampersand is AND, the first part evaluates to true and then ++b increment the b to 3 and it also evaluates to true. That’s why c is true and b is 3.

See the next line of code which is a copy of the first line but its output is different from the first line. In this case, the first condition evaluates to false as b is incremented to 3 in the first line. Hence the second part of the expression will not be evaluated.

 c = (a > b || ++b == 3);  

OR condition, here one part of the expression needs to be true for the overall results to become true. So we have to evaluate both parts to tell that.

If you look at a Boolean expression, the first part evaluates to false and then ++b increment the b to 4 and it also evaluates to false. That’s why c is false and b is 4.

The next line also used the OR operator but changed the first part of the condition to a < b.

 c = (a < b || ++b == 3);  

In this case the first part evaluates to true hence the second part of the expression is omitted from the evaluation. That’s why c is true and b is still 4.

The next line also used the OR operator but not the short circuit one. This time we used the full evaluation of OR (|) means a single vertical line. The difference in full evaluation is even if the first condition of the OR is true the second part will still be evaluated and it doesn’t impact the overall result of the expression.

 c = (a < b | ++b == 3);  

Hence, in the output of the above line value of c is true and b is incremented to 5.

The next line is having the full evaluation AND operator. These means don’t matter the first condition evaluates to true or false the second condition will be evaluated which is not the case when we use short circuit AND (&&) operator.

 c = (a > b & ++b == 3);  

In the above line the first condition is evaluated to false but still, the second condition is evaluated which is not the case when we use &&. Hence The value of c is false in the output of the above line but the b is still incremented to 6.

The next two lines of the code are using XOR (^) operator. This is also used for a full evaluation of both the expression. This will return true if one condition is true and another is false.

 c = (a < b ^ ++b == 3);  

Here the first condition is evaluated to true then it increments b to 7 and the second condition evaluates to false.

 c = (a > b ^ ++b == 3);  

The last line output value of c is false and b is 8. Here if you see the first condition is evaluated to false then it incremented b to 8 and that condition is false.

Conclusion

Here we discussed the &&, ||, &, | and ^ operators and how they evaluate a given condition. While using full evaluation the second part of the condition is evaluated doesn’t matter the first condition is evaluated to true or false.

Happy Learning !!

Read more

Refrence

How to use Java 8 Function and BiFunction Interface?

With all the other enhancements in Java 8 a new package java.util.function is also added to the Java library. In this post, we will discuss how to use the Function and BiFunction interface with examples. Both the interface are functional interface.

Function Interface Syntax

@FunctionalInterface
public interface Function<T, R> {
    R apply(T t);

    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

    static <T> Function<T, T> identity() {
        return t -> t;
    }
}

Salient Points of Function interface

  • It has been defined with the generic types T & R, where T is the type of the input and R is the output type.
  • Method apply() is the functional method (only abstract method in a functional interface). It takes an input t of type T and returns an output of type R.
  • It has two default method.
  • The first default method compose()  Returns a composed function that first applies the function to its input, and then applies this function to the result. If the evaluation of either function throws an exception, it is relayed to the caller of the composed function. In simple words, a composed function that first applies the before function and then applies its own function to the result of before.
  • The second default method is andThen() is inverse of the compose function. It is a composed function that first applies its own function to the input and then applies the after function to the result.
  • Function interface has one static method identity() which returns a function that always returns its input argument.

BiFunction Interface

@FunctionalInterface
public interface BiFunction<T, U, R> {
    R apply(T t, U u);

    default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t, U u) -> after.apply(apply(t, u));
    }
}

Salient Points of BiFunction interface

  • It has been defined with the generic types T, U & R, where T and U are the types of the first and second input and R is the output type.
  • Method apply() is the functional method that applies this function to the given input T and U and returns the output of Type R.
  • The default method andThen() is a composed function that first applies its own function to the input and then applies the after function to result.

Example

In this example, we will use a class Product to demonstrate the Function interface.

class Product{
	private int id;
	private String name;
	private String desc;
	
	public Product(int id, String name, String desc) {
		super();
		this.id = id;
		this.name = name;
		this.desc = desc;
	}

	@Override
	public String toString() {
		return "Product [id=" + id + ", name=" + name + ", desc=" + desc + "]";
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getDesc() {
		return desc;
	}

	public void setName(String name) {
		this.name = name;
	}
	
} 

Function Interface apply method

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class FunctionDemo {
	static List<Product> products = new ArrayList<>();	
	
	static Function<Product, String> getProductNameFunction = Product::getName;
		
	public static void main(String[] args) {
		prepareData();
		List<String> productNames = new ArrayList<>(); 
		products.forEach(p ->{
			productNames.add(getProductNameFunction.apply(p));
		});
		System.out.println("productNames => "+productNames);		
	}

	static void prepareData() {
		for(int i =1 ; i<= 10; i++) {
			products.add(new Product(i, "product name "+i, "product desc "+i));
		}
	}
}

In the above code snippet, we have created a list of Products and a Function getProductNameFunction which will convert the list of Products into a list of String containing product names.

Function Interface compose method

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.function.Function;

public class FunctionDemo {
	static List<Product> products = new ArrayList<>();	
	
	static Function<Product, String> getProductNameFunction = Product::getName;
	
	static Function<Product, Product> composeBeforeFunction = (Product p)->{
		String name = p.getName().replace(" ", " "+new Random().nextInt());
		p.setName(name);
		return p;
	};
		
	public static void main(String[] args) {
		prepareData();
		
		List<String> productNames = new ArrayList<>(); 
		products.forEach(p ->{
			productNames.add(getProductNameFunction.compose(composeBeforeFunction).apply(p));
		});
		System.out.println("productNames => "+productNames);		
	}

	static void prepareData() {
		for(int i =1 ; i<= 10; i++) {
			products.add(new Product(i, "product "+i, "product desc "+i));
		}
	}
}

Here we have created a list of products and a new Function composeBeforeFunction and we also use getProductNameFunction from apply example. Here when we run this code snippet as per the description of compose() method composeBeforeFunction will apply and it will add a random string to the product name and then the getProductNameFunction will be applied which will return a list of the product name.

Function Interface andThen method

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class FunctionDemo {
	static List<Product> products = new ArrayList<>();	
	
	static Function<Product, String> getProductNameFunction = Product::getName;
	
	static Function<String, String> andThenFunction = (String s)-> s.substring(s.length()-1,s.length());
	
	public static void main(String[] args) {
		prepareData();
		
		List<String> productNames = new ArrayList<>(); 
		
		products.forEach(p ->
			productNames.add(getProductNameFunction.andThen(andThenFunction).apply(p))
		);
		
		System.out.println("productNames => "+productNames);		
	}

	static void prepareData() {
		for(int i =1 ; i<= 10; i++) {
			products.add(new Product(i, "product "+i, "product desc "+i));
		}
	}
}

In this example, we have created a list of products and a new Function andThenFunction also we have used getProductNameFunction from apply example. Here when we run this code snippet as per the description of andThen() method getProductNameFunction will apply first and then the andThenFunction will be applied which will return a list of String only having the last char of the product name.

Function Interface identity method

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class FunctionDemo {
	static List<Product> products = new ArrayList<>();	
	
	public static void main(String[] args) {
		prepareData();
		
		Map<Integer, Product> productmap = products.stream()
				.collect(Collectors.toMap(Product::getId, Function.identity()));
		
		System.out.println("productmap => "+productmap);		
	}

	static void prepareData() {
		for(int i =1 ; i<= 10; i++) {
			products.add(new Product(i, "product "+i, "product desc "+i));
		}
	}
}

In this example, we have converted the list of products into a Map of using the identity() method.

If we haven’t used the identity() method to create a Map we have to use the other way like below.

Map<Integer, Product> productmapWithForeach = new HashMap<>();
		
products.forEach(product -> productmapWithForeach.put(product.getId(),
 product));

BiFunction apply method

public class FunctionDemo {	
		
	public static void main(String[] args) {
				
		 BiFunction<Integer, Integer, Integer> addFunction = (t, u) -> t + u;
		 
		 int sum = addFunction.apply(10, 20);
	 
		 System.out.println("sum is => +sum");

		
	}
}

Conclusion

In this post, we have discussed Function and BiFunction Interface and how can we use these functions to write more concise and clean code.

Recommended Read

Java 8 Method Reference
Java 8 Stream API
Java 8 comparison using Lambda

Happy Learning !!

Understanding and preventing memory leaks in Java

For any developer, memory should be one of the most precious resources to think of while writing a program. A program can be said to be memory efficient when it consumes as little memory as possible when in operation but still doing what it was designed to do. So, before write code think of memory uses, do you know What happens when a Java object is created?

What is a Memory Leak?

In simple words, memory leak means unused memory that the garbage collectors can not reclaim.

A memory leak occurs when objects are no longer being used by the application, but the garbage collector is unable to clear them from working memory. This is a serious problem because these objects hold the memory that could otherwise be used by other parts of the program. With time, this piles up and leads to a degradation in system performance over time.

Garbage Collection in Java

One of the coolest features of Java is its automatic memory management. The Garbage Collection is a program that implicitly takes care of memory allocation and deallocation. It’s an effective program and can handle the majority of memory leaks that are likely to occur. However, it’s not infallible. Memory leaks can still sneak up and start taking up precious resources and, in worst cases, result in the java.lang.OutOfMemoryError.

It’s worth mentioning that an OutOfMemoryError is not always because of memory leaks. At times, it could be poor code practices like loading large files in memory.

The three common causes of memory leaks?

  • Misused static fields
  • Un closed streams
  • Un closed connections

Misused static fields

Static fields live in the memory as long as the class that owns them is loaded in the JVM, which means there are no instances of the class in the JVM, at this point, the class will be unloaded and the static field will be marked for garbage collection. But the catch here is, the static classes can live in the memory forever.

Consider the following code:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class StaticDemo {
public static List&lt;Integer&gt; list = new ArrayList&lt;&gt;();

public void populateList() {
for (int i = 0; i &lt; 10000000; i++) {
   list.add(new Random().nextInt());
}
}
public static void main(String[] args) {
  new StaticDemo().populateList();
}
}

This is a simple and small code snippet, we have a static List and we added values into it. But If we see the declaration of the list we have used static in front of it.  That’s the problem, the memory occupied by this list will be in use until the program finishes its execution even though the operations on the list are already completed.

To avoid this mistake, Minimize the use of static fields in your application.

Un closed streams

Till now we have understood memory leak is when code holds a reference to an object and the garbage collector can’t claim that object. But, a closed stream object is not memory unless it holds a reference of an unclosed stream.

Usually, the operating system limits how many open files (while using FileInputStream) an application can have. So, if such streams are not closed and the garbage collector may take some to claim these objects, it is also a case of the leak but not particularly a memory leak.

Refer to the following code snippet:

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

public class OpenStreamDemo {

    public static void main(String[] args) throws IOException {
        URL url = new URL("http://ergast.com/api/f1/2004/1/results.json");
        ReadableByteChannel rbc = Channels.newChannel(url.openStream());
        FileOutputStream outputStream = new FileOutputStream("/");
        outputStream.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    }
}

Here the unclosed FileOutputStream and ReadableByteChannel will cause potential issues. The solution to this problem would be closing these stream objects once they are used and not require further. Or use java to try with resources wherever allowed.

rbc.close();
outputStream.close();

Un closed connections

The frequent mistake in Java programming is, developer forgot to close the database connections.  Having an unclosed database connection can give you a tough time in production and they are difficult to replicate in local environments.

Refer to the below code snippet:

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OpenConnectionDemo {

    public static void main(String[] args) {
        try {
            List&lt;String&gt; names = fetchUsersName("foo", "bar");
            names.forEach(System.out::println);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static List&lt;String&gt; fetchUsersName(String username, String password) throws SQLException {
        List&lt;String&gt; names = new ArrayList&lt;&gt;();
        Connection con = DriverManager.getConnection("jdbc:myDriver:devDB",
                username,
                password);

        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT first_name, last_name FROM users");

        String firstName = "";
        String lastName = "";

        while (rs.next()) {
            firstName = rs.getString("first_name");
            lastName = rs.getString("last_name");
            names.add(firstName + " " + lastName);
        }
        return names;
    }
}

Here every resource is leaking. Here we could have use try-catch and finally, and inside the finally block we can close

stmt.close();
con.close();

Here is the complete code for the fetchUsersName method.

public static List&lt;String&gt; fetchUsersName(String username, String password) throws SQLException {
    Statement stmt = null;
    Connection con = null;
    List&lt;String&gt; names = new ArrayList&lt;&gt;();
    try {

        con = DriverManager.getConnection("jdbc:myDriver:devDB",
                username,
                password);

        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT first_name, last_name FROM users");

        String firstName = "";
        String lastName = "";

        while (rs.next()) {
            firstName = rs.getString("first_name");
            lastName = rs.getString("last_name");
            names.add(firstName + " " + lastName);
        }
    }catch (SQLException se){
        se.printStackTrace();
    }finally {
        stmt.close();
        con.close();
    }
    return names;
}

Or we can use try with resources like below.

public static List&lt;String&gt; fetchUsersName(String username, String password) throws SQLException {
    List&lt;String&gt; names = new ArrayList&lt;&gt;();
    try (Connection con = DriverManager.getConnection("jdbc:myDriver:devDB",username,password);
         Statement stmt = con.createStatement();
    ){
        ResultSet rs = stmt.executeQuery("SELECT first_name, last_name FROM users");

        String firstName = "";
        String lastName = "";

        while (rs.next()) {
            firstName = rs.getString("first_name");
            lastName = rs.getString("last_name");
            names.add(firstName + " " + lastName);
        }
    }catch (SQLException se){
        se.printStackTrace();
    }
    return names;
}

We can also use ORM like hibernate or jooq which provides easier and more maintainable resource handling.

So the take away from this post are:

  1. Be careful while using static fields.
  2. Make sure to close the stream and connection or use try with resources.
  3. Use ORM if your application allows.

 

Happy Learning !!!

Parsing JSON With Java 8 Without Dependencies

While working with JSON we often need to parse the JSON into an object, or Map, or List, or to a custom object as well.  To parse the JSON we have to use a dependency to process it easily.

But, starting with JDK 8u60+ the built-in Nashorn engine is capable to convert JSON content into java.util.Map or java.util.List. No external dependencies are required for parsing.

Here is an example with sample JSON. You can follow the below steps.

  1. Copy the below JSON into a file “test.json” and place it inside your java project.
{
	"page": "1",
	"per_page": 10,
	"total": 23,
	"total_pages": 3,
	"data": [
		{
			"competition": "UEFA Champions League",
			"year": 2014,
			"round": "GroupG",
			"team1": "Chelsea",
			"team2": "Schalke 04",
			"team1goals": "1",
			"team2goals": "1"
		},
		{
			"competition": "UEFA Champions League",
			"year": 2014,
			"round": "GroupG",
			"team1": "Chelsea",
			"team2": "NK Maribor",
			"team1goals": "6",
			"team2goals": "0"
		},
		{
			"competition": "UEFA Champions League",
			"year": 2014,
			"round": "GroupG",
			"team1": "Chelsea",
			"team2": "Sporting CP",
			"team1goals": "3",
			"team2goals": "1"
		},
		{
			"competition": "UEFA Champions League",
			"year": 2014,
			"round": "R16",
			"team1": "Chelsea",
			"team2": "Paris Saint-Germain",
			"team1goals": "1",
			"team2goals": "1"
		},
		{
			"competition": "English Premier League",
			"year": 2014,
			"round": "",
			"team1": "Chelsea",
			"team2": "Leicester City",
			"team1goals": "2",
			"team2goals": "0"
		},
		{
			"competition": "English Premier League",
			"year": 2014,
			"round": "",
			"team1": "Chelsea",
			"team2": "Swansea City",
			"team1goals": "4",
			"team2goals": "2"
		},
		{
			"competition": "English Premier League",
			"year": 2014,
			"round": "",
			"team1": "Chelsea",
			"team2": "Aston Villa",
			"team1goals": "3",
			"team2goals": "0"
		},
		{
			"competition": "English Premier League",
			"year": 2014,
			"round": "",
			"team1": "Chelsea",
			"team2": "Arsenal",
			"team1goals": "2",
			"team2goals": "0"
		},
		{
			"competition": "English Premier League",
			"year": 2014,
			"round": "",
			"team1": "Chelsea",
			"team2": "Queens Park Rangers",
			"team1goals": "2",
			"team2goals": "1"
		},
		{
			"competition": "English Premier League",
			"year": 2014,
			"round": "",
			"team1": "Chelsea",
			"team2": "West Bromwich Albion",
			"team1goals": "2",
			"team2goals": "0"
		}
	]
}

2. Create a class “JSONParser.java” and put the below code inside it.

package com.codersdesks.json;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.junit.Before;
import org.junit.Test;

public class JSONParser {
	
	private ScriptEngine scriptEngine;
	private String json;

	@Before
	public void setup() throws IOException {
		ScriptEngineManager sem = new ScriptEngineManager();
		this.scriptEngine = sem.getEngineByName("javascript");
		File file = new File("test.json");
		this.json = new String(Files.readAllBytes(file.toPath()));
	}

	@Test
	public void parse() throws ScriptException {
		String script = "Java.asJSONCompatible(" + this.json + ")";
		Object result = this.scriptEngine.eval(script);
		Map contents = (Map) result;
		
		contents.forEach((k,v) ->{
			System.out.println("Key => "+k +" value =>"+contents.get(k));
		});
		
		List data = (List) contents.get("data");
		data.forEach(d ->{
			Map matchDetail = (Map) d;
			matchDetail.forEach((k,v) ->{
				System.out.println("Key => "+k +" value =>"+matchDetail.get(k));
			});	
		});
	}

}

Run the Junit test and you can see the below output.

Key => page value =>1
Key => per_page value =>10
Key => total value =>23
Key => total_pages value =>3
Key => data value =>[[object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object]]
Key => competition value =>UEFA Champions League
Key => year value =>2014
Key => round value =>GroupG
Key => team1 value =>Chelsea
Key => team2 value =>Schalke 04
Key => team1goals value =>1
Key => team2goals value =>1
Key => competition value =>UEFA Champions League
Key => year value =>2014
Key => round value =>GroupG
Key => team1 value =>Chelsea
Key => team2 value =>NK Maribor
Key => team1goals value =>6
Key => team2goals value =>0
Key => competition value =>UEFA Champions League
Key => year value =>2014
Key => round value =>GroupG
Key => team1 value =>Chelsea
Key => team2 value =>Sporting CP
Key => team1goals value =>3
Key => team2goals value =>1
Key => competition value =>UEFA Champions League
Key => year value =>2014
Key => round value =>R16
Key => team1 value =>Chelsea
Key => team2 value =>Paris Saint-Germain
Key => team1goals value =>1
Key => team2goals value =>1
Key => competition value =>English Premier League
Key => year value =>2014
Key => round value =>
Key => team1 value =>Chelsea
Key => team2 value =>Leicester City
Key => team1goals value =>2
Key => team2goals value =>0
Key => competition value =>English Premier League
Key => year value =>2014
Key => round value =>
Key => team1 value =>Chelsea
Key => team2 value =>Swansea City
Key => team1goals value =>4
Key => team2goals value =>2
Key => competition value =>English Premier League
Key => year value =>2014
Key => round value =>
Key => team1 value =>Chelsea
Key => team2 value =>Aston Villa
Key => team1goals value =>3
Key => team2goals value =>0
Key => competition value =>English Premier League
Key => year value =>2014
Key => round value =>
Key => team1 value =>Chelsea
Key => team2 value =>Arsenal
Key => team1goals value =>2
Key => team2goals value =>0
Key => competition value =>English Premier League
Key => year value =>2014
Key => round value =>
Key => team1 value =>Chelsea
Key => team2 value =>Queens Park Rangers
Key => team1goals value =>2
Key => team2goals value =>1
Key => competition value =>English Premier League
Key => year value =>2014
Key => round value =>
Key => team1 value =>Chelsea
Key => team2 value =>West Bromwich Albion
Key => team1goals value =>2
Key => team2goals value =>0

You can also read about JSON Schema and How to use Jackson databind with JSON

For more information, you can refer to the official Nashorn extensions documentation.

Happy Learning !!

io.lettuce.core.RedisConnectionException: DENIED Redis is running in protected mode

Problem

While trying to connect to Redis running on Linux through my java code getting io.lettuce.core.RedisConnectionException: DENIED Redis is running in protected mode.

The exception stack trace is as below.

io.lettuce.core.RedisConnectionException: DENIED Redis is running in protected mode because the protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode, connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and
then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
        at io.lettuce.core.protocol.CommandHandler.onProtectedMode(CommandHandler.java:667) ~[lettuce-core-5.0.1.RELEASE.jar!/:na]
        at io.lettuce.core.protocol.CommandHandler.channelInactive(CommandHandler.java:305) ~[lettuce-core-5.0.1.RELEASE.jar!/:na]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Fina
l]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Fina
l]
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:224) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]
        at io.netty.channel.ChannelInboundHandlerAdapter.channelInactive(ChannelInboundHandlerAdapter.java:75) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]
        at io.lettuce.core.ChannelGroupListener.channelInactive(ChannelGroupListener.java:46) ~[lettuce-core-5.0.1.RELEASE.jar!/:na]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Fina
l]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Fina
l]
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:224) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]
        at io.netty.channel.ChannelInboundHandlerAdapter.channelInactive(ChannelInboundHandlerAdapter.java:75) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]
        at io.lettuce.core.PlainChannelInitializer$1.channelInactive(PlainChannelInitializer.java:85) ~[lettuce-core-5.0.1.RELEASE.jar!/:na]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Fina
l]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Fina
l]
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:224) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]
        at io.netty.channel.DefaultChannelPipeline$HeadContext.channelInactive(DefaultChannelPipeline.java:1429) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Fina
l]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Fina
l]
        at io.netty.channel.DefaultChannelPipeline.fireChannelInactive(DefaultChannelPipeline.java:947) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]
        at io.netty.channel.AbstractChannel$AbstractUnsafe$8.run(AbstractChannel.java:822) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]
        at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) ~[netty-common-4.1.31.Final.jar!/:4.1.31.Final]
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) ~[netty-common-4.1.31.Final.jar!/:4.1.31.Final]
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:466) ~[netty-transport-4.1.31.Final.jar!/:4.1.31.Final]

Solution

After analyzing the logs noticed following lines which describe the root cause and fix for this.

DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no auth

In order to fix this we need to do one of the following:

  1. Disable protected mode from redis CLI
  2. Disable protected mode from redis config
  3. Restart redis it with the ‘–protected-mode no’ option, only for testing purposes.
  4. Setup a bind address or an auth

Since we knew the solution for this, let’s see how we can actually do it.

  1. To disable the protected mode just open the redis-cli and run ‘CONFIG SET protected-mode no’.
  2. To disable the protected mode stop redis server and edit redis.conf file inside conf folder, add ‘protected-mode no’.  Restart the redis server .
  3. To restart redis server with –protected-mode no’ option, run redis-server  with option –protected-mode. Like redis-server –protected-mode no
  4. To bind an IP address stop redis server, edit redis config and add bind *.*.*.* to bind a specific IP to access the redis server. i.e. bind 127.0.0.1. Restart redis server.

Note: If none of the above options are performed you will not be able to access redis server.

If you know of any other fixes to this issue please let us know through comments.

Read more about Java

What happens when a Java object is created?

Java 8 Stream API with examples

Happy Learning !!

Amazon EC2 – disk full /dev/nvme0n1p1 full how to fix

Problem: While running my Jenkins build, build failed with following error.

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.1.RELEASE:repackage (repackage) on project <project name>: No space left on device -> [Help 1]

To investigate, when I run df -h on my Amazon EC2 server, this is the output:

[root@ip-172-*-*-3 builds]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 516K 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/nvme0n1p1 20G 20G 76K 100% /
tmpfs 389M 0 389M 0% /run/user/1000

For some reason my space is almost full and I really can’t find the big files or directory is easily.

To get the files/directory eating up my space I used the following command to find all the files greater than 10 MB:

sudo find / -type f -size +10M -exec ls -lh {} \;

This command return you list of files with complete path, which helps me to identify which ones are causing the trouble and deal with them accordingly.

After analyzing the result form the above command, I found that Jenkins keeps archiving all the previous builds which are eating up most of my memory.

Once I have the list of files I deleted all the large files using rm -r commands and removed all the files inside directory using rm -rf *.

Once I have deleted the large files, I ran the Jenkins build again and it work perfectly.

So, while using Jenkins I think we should also think about the clean up process for the archives, if not the entire one we can delete the significantly old archives and have enough space for builds to execute successfully.

Read more about Java here.

 

IntelliJ: Error:java: error: release version 5 not supported

Problem: While creating a new maven project using IntelliJ IDEA with Java 11 started getting the following error:

Error:java: error: release version 5 not supported

After exploring the internet for a while I found what is causing this error.

Reason: Generally Maven sets the default Java version to 1.5. Hence you need to set the correct language level and release version in pom.xml.

Before searching for a solution you can check your current Java version at, File => Settings => Build, Execution, Deployment => Compiler => Java Compiler and change it if required.

IntelliJ_Java_compiler_version

Even after changing the java version, if it didn’t work, you can apply the following solutions.

Solution 1:

Set the source /target versions in pom.xml like below:

<properties>
     <maven.compiler.source>1.11</maven.compiler.source>
     <maven.compiler.target>1.11</maven.compiler.target>
</properties>

You will also need to include the maven-compiler-plugin if you haven’t already like:

plugins>
     <plugin>
	 <groupId>org.apache.maven.plugins</groupId>
	      <artifactId>maven-compiler-plugin</artifactId>
	      <version>3.6.0</version>
	      <configuration>
	        <source>1.11</source>
                <target>1.11</target>
	      </configuration>
	</plugin>
</plugins>

Solution 2: Specific to SPring boot, If you are using spring boot as a parent, you have to set the java version.

<properties>
   <java.version>11</java.version>
</properties>

This will automatically set the correct versions.

If The above-mentioned option didn’t work for you, you can move one step further by changing the “language level” in every module at, File => Project Structure => Modules

Here we discussed how we can change the Java compiler version in IntelliJ and solution to common error while creating a maven project using IntelliJ.

You are free to share if there are other ways to fix this error.

If you are new to Java you may find these articles helpful.

What happens when a Java object is created?

Java bitwise operators with example

Java Exception in detail with example

How to create Java Custom Exception?

Java try with resources statement

Java Generic, Generic with super and wildcard

Happy Learning !!

What happens when a Java object is created?

As a software developer how many times we create an object in Java, almost for everything there is an object of a class to do the given task. But do you know what all happens behind the scene when we create an object?

In this post, we will discuss several things that happen in an order to ensure the object is constructed properly.

Introduction

Java object

Before creating a Java object, the class byte code (.class file) must be loaded from the file system to memory. Locating the byte code for a given class name and converting that code into a Java class instance is known as class loading. There is one class created for each type of Java class.

Objects in Java are created on heap memory. An object is created based on its class. A class is a template that defines states and behavior defines how an object is created.

When a Java object is created following steps will happen one by one –

    • JVM allocates 8 bytes of memory for the reference variable & assigns a default value as null.
    • JVM will verify whether class loading is done or not, if the class is already loaded then it will ignore or else it will perform class loading.
    • At the time of class loading, if any static variable is there then memory will be allocated for them.
    • At the time of class loading, if it contains any String literals a new String object will be created to represent that literal. This may not happen if the same String is previously been interned.
    • By using the new operator, object memory will be created inside heap memory.
    • At the time of object creation, if any instance variables are there then those will allocate memory inside object Memory.
    • It will assign object memory address to the reference variable which is created first.
    • If there is not sufficient space available to allocate memory for the object, then the creation of the class instance abrupt with an OutOfMemoryError. Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values.

When an object is created, memory is allocated to hold the object properties. An object reference pointing to that memory location is also created. To use the object in the future, that object reference must be stored as a local variable or as an object member variable.

There is no limit on how many objects from the same class can be created. Code and static variables are stored only once, no matter how many objects are created. Memory is allocated for the object member variables when the object is created. Thus, the size of an object is determined not by its code’s size but by the memory it needs for its member variables to be stored.

We have discussed what happens when a Java object is created, let’s check the ways to create an object in Java.  Following are some ways in which you can create objects in Java –

Creating an object with the new keyword

In most cases, the new objects are created using the new keyword. This is the most basic way to create an object by calling a constructor (default or parameterized constructor). In the below example, we create an object of AppTest when the program starts running.

public class AppTest{
    public static void main(String[] args) {
        AppTest test = new AppTest();
    }
}

Creating an object with the newInstance()

If the name of the class is known & it has a public default constructor we can create an object using – Class.forName. We can use it to create the Object of a Class. Class.forName actually loads the Class in Java but doesn’t create any Object. To create an Object of the Class you have to use the new Instance Method of the Class.

public class AppTest{

    public static void main(String[] args) {
        try {
            Class clazz = Class.forName("com.coddersdesks.AppTest");
            AppTest test =  (AppTest) clazz.newInstance();
            test.print("hello this is a test with newInstance()");
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public void print(String message){
        System.out.println("message "+message);
    }
}
message hello this is a test with newInstance()

Creating an object using the clone() method

Whenever we call the clone() method on any java object, the JVM creates a new object and copies all content of the old object into it, read more about shallow copy vs deep copy. Creating an object using the clone method does not invoke any constructor. To use the clone() method on an object we need to implement Cloneable and define the clone() method in it.

Cloning is not automatic, there is some help though, as all Java objects inherit the protected Object clone() method from the Object class. This base method would allocate the memory and do the bit by bit copying of the object’s states.

public class Student implements Cloneable{

	 int rollno;
	
	 String name;
	
	 Course course;

	public Student(int rollno, String name, Course course) {
		super();
		this.rollno = rollno;
		this.name = name;
		this.course = course;
	}

	@Override
	public String toString() {
		return "Student [rollno=" + rollno + ", name=" + name + ", course=" + course + "]";
	}
	
	protected Object clone() throws CloneNotSupportedException{
		return super.clone();
	}
}
  1. Here we are creating the clone from an existing Object and not any new Object.
  2. To support the cloning Class need to implement Cloneable Interface otherwise it will throw CloneNotSupportedException.

The clone() method copies the whole object’s memory in one operation and this is much faster than using the new keyword and copying each variable so if you need to create lots of objects with the same type, performance will be better if you create one object and clone new ones from it.

Re-creating an object using deserialization

I hope you are aware of serialization and deserialization in Java. If you want to recall it you can read Serialization in Java using Serializable Interface. Do you know there is another way as well? Serialization with Java Externalizable Interface.

So, whenever there is a need to manage the object states (properties), we can use serialization. The term Object Serialization refers to the act of converting the object to a byte stream. The common uses of serialization are sending an object over the network, persisting the object into the database using ORM such as hibernate or writing the object to the file system.

serialization in Java

During deserialization, the object can be re-created from that stream of bytes. The only requirement is that the same class has to be available both times when the object is serialized and also when the object is re-created. If that happens on different application servers, then the same class must be available on both servers. The same class means that the same version of the class must be available, otherwise, the object won’t be able to be re-created.

When a class is modified, there could be a problem re-creating those objects that were serialized using an earlier version of the class.

Creating Object using newInstance() method of Constructor

The newInstance() method of java.lang.reflect.Constructor is similar to newInstance() of Class, which can be used to create objects. Using this we can call the parameterized constructor and private constructor as well.

Both newInstance() methods are known as reflective ways to create objects. In fact newInstance() method of Class internally uses newInstance() method of Constructor class. Refer to the below code snippet for detail.

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class AppTest{

    private String testName;

    public AppTest(){

    }

    public AppTest(String testName){
        this.testName = testName;
    }

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, 
      InvocationTargetException, InstantiationException {

        Constructor<AppTest> constructor = AppTest.class.getDeclaredConstructor(); // default constructor
        AppTest test = constructor.newInstance();
        Constructor<AppTest> constructor1 = AppTest.class.getDeclaredConstructor(String.class); // parameterized constructor

        test.print("called using constructor newInstance() and default constructor");
        AppTest test1 = constructor1.newInstance("test name passed to constructor");
        test1.print("called using constructor newInstance() and parameterized constructor");
    }

    public void print(String message){
        System.out.println("message "+message);
        if(this.testName != null){
            System.out.println(this.testName);
        }
    }
}

When you run the above code it will print the following.

message called using constructor newInstance() and default constructor
message called using constructor newInstance() and parameterized constructor
test name passed to the constructor

Here we have discussed what happened behind the scene when we create a new Java object and various ways to create an object.

Happy Learning !!

How to use synchronized and reentrant lock in Java

In this post, we will discuss the uses of synchronized and reentrant lock in Java. Below are the points we are gonna cover.

The problem

Before jumping into the use of a synchronized or reentrant lock, first, let see what kind of problem it’s gonna solve. Here is a simple requirement we have, we have to write a method which increments the value of an int ‘a’. Refer the below code snippet.

static int a = 0;
static void add() {
   for (int i = 0; i < 10000; i++)
     a++;
}

But when we call the same method using two threads we are not getting the expected value of ‘a’. Here is the complete java source.

public class SynchronizedAndReentrantLockExample {
    static int a = 0;

    static void add() {
        for (int i = 0; i < 10000; i++)
            a++;
    }

    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(() ->{
            add();
        });

        Thread t2 = new Thread(() ->{
            add();
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println("Final value of a is "+a);
    }
}

The expected value of a is 20000 but after executing this it didn’t return that.

So the problem here is that at the same time when we call the add() method using two threads, one thread overwrites the value incremented by the other thread, and the reason behind that is the method add() can be executed by two threads at the same time.

Using synchronized to fix

We can fix the above problem using the synchronized keyword. Let’s add the synchronized keyword to the add() method. As soon as we make the add () method synchronized it starts working as expected.

static synchronized void add() {
  for (int i = 0; i < 10000; i++)
     a++;
}

So, once we make this method synchronized only one thread can execute this method at a time, hence it solves the problem we have. But is this is the only way can use synchronized? The answer is a big NO.

Instead of making the entire method synchronized, we can use a synchronized block. Refer to the updated version of the add() method.

static void add() {
   synchronized (SynchronizedAndReentrantLockExample.class) {
       for (int i = 0; i < 10000; i++)
	 a++;
   }
}

The difference between synchronized block and method is when we use the synchronized method the entire method is blocked, but by using a synchronized keyword if the method has code that is not inside the synchronized block it can be executed by other threads. That could be useful in some cases.

By using these two ways we can solve the problem, but it has some limitations as well. The major one is in both cases the lock will be acquired on the class, which means if two threads are running at the same time and the first thread that acquires the lock will block the second thread even if it is calling another synchronized method.

public class SynchronizedAndReentrantLockExample {
    static int a = 0;

    static synchronized void add() {
        System.out.println("incrementing values");
        for (int i = 0; i < 10000; i++)
            a++;
        System.out.println("method add done, going to sleep now ");
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    static synchronized void print() {
        System.out.println("the value of a is "+a);
    }


    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(() ->{
            add();
        });

        Thread t2 = new Thread(() ->{
            print();
            //add();
        });

        t1.start();
        t2.start();
    }
}
incrementing values
method add done, going to sleep now 
the value of a is 10000

Problem with the synchronized method and block

Here you can see the thread t1 call the add () method which increments the values and then sleeps for 4000 ms. But the thread t2 calls another synchronized method print () which has nothing to do with the add () method. But as thread t1 acquires the lock on the class it blocks thread t2 which is entirely independent of t1. This is the problem of using a class lock.

Remember: Every problem has a solution. So let’s fix the class lock problem. It can be fixed in a couple of ways. Let’s see how we can do that.

1. Using object lock.
2. By Using ReentrantLock.

Let’s modify the add () method and introduce an object lock.

final static Object lock = new Object();

static void add() {
 synchronized (lock) {
  for (int i = 0; i < 10000; i++)
    a++;
  }
}

By using lock objects, in the case of the class lock, the lock object should be static whereas in the case of object lock it will be non-static. The benefit of using a lock object is, it allows the other threads to execute the code which is independent of this method as opposed to the class lock which does acquire the lock in the entire class and block any other thread from executing even another synchronized method.

Using ReentrantLock

The same implementation as using object lock can be achieved using ReentrantLock, the inbuilt class in java.util.concurrency package which implements Lock and serializable interface.

public class ReentrantLock implements Lock, Serializable

Here how we can rewrite the add () method using ReentrantLock. We will create a Lock object by initiating ReentrantLock.

static Lock lock = new ReentrantLock();
static void add() {
  lock.lock();
    for (int i = 0; i < 10000; i++)
      a++;
  lock.unlock();
}

The benefit of using ReentrantLock is that we don’t have to handle the low-level problem of synchronization and all.

The important point while using a reentrant lock, the lock acquisitions should be wrapped in a try/finally block, to prevent the deadlock in case of an exception.

Difference between the synchronized and reentrant lock

A reentrant mutual exclusion Lock with the same basic behavior and semantics as the implicit monitor lock accessed using synchronized methods and statements, but with extended capabilities.

The ability of ReentrantLock to make the lock fair. Synchronized blocks are unfair. We can create the lock object by passing the value of fair as true/false in the constructor.

Lock lock = new ReentrantLock(true);

The fair locks favor granting access to the longest-waiting thread.

Recommended Read

An introduction to Multithreading

Happy Learning !!