Thursday, December 22, 2016

Actions in Play Framework 2.5

Requests that come to application based on Play usually are processed by thing which is called action.

Action it is just a method that processes parameters of requests and sends result back to web client

Let's look on example below

package controllers;

import play.mvc.*;

 * This controller contains an action to handle HTTP requests
 * to the application's home page.
public class HomeController extends Controller {

     * An action that renders an HTML page with a welcome message.
     * The configuration in the routes file means that
     * this method will be called when the application receives a
     * GET request with a path of /.
    public Result index() {
        return ok("Hello World!");


We see an action index that return Result (HTTP response which we send to web client).

Wednesday, December 21, 2016

Hello World on Play Framework 2.5 on OSX

Today we are going to make Hello World project based on Play Framework 2.5 on OS X

I'm getting back to Play Framework again and I'm going to build simple start project.

1. Checking if Java is installed

Make sure you have java installed.

java -version

If java is installed you will see message like that:

java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

2. Installing Typesafe activator (current version is 1.3.12)

brew install typesafe-activator

3. Let's create a new project

activator new hello-world

Activator will ask you what template you want to use for new project? (I used play-java).

  1) minimal-akka-java-seed
  2) minimal-akka-scala-seed
  3) minimal-java
  4) minimal-scala
  5) play-java
  6) play-scala

That will create a java project for us

Now let's run it. Go into newly create project and run activator

activator run

That will run our project and up server as well. You can access project by localhost:9000

Thursday, December 08, 2016

Programming Pascal's triangle

What is Pascal's triangle?

It is a triangular array which is consists from binomial coefficients (you can see visual representation of it below).

To get more information check article on wiki: Pascal's triangle.

I had a task where I needed to find out a value in a cell and what I only had were coordinates of it. I came with quit simple solution (Scala) which I really like.

The idea is to move up from the initial cell to the borders (left and right, since I know the values there) and once I am there, I move back to my initial cell but now I can bring some values from above.

object Main {
  def main(args: Array[String]) {    
    println(pascal(4, 3))

  def pascal(c: Int, r: Int): Int = {
    if (c == 0 || r == 0 || c == r) 1
    else pascal(c - 1, r - 1) + pascal(c, r - 1)

Tuesday, March 22, 2016

Use equal for string literal, rather than for an object

When you wanna compare String object with string literal, we often disregard what we compare with what, however there is one and safe way to do it.

It's much better to use equals() and equalsIgnoreCase() for a string literal, instead of Object, because it helps to avoid possible NullPointerException.

Here is an example:
 String a = null;  
 System.out.print("123".equals(a));     // false  
 System.out.print(a.equals("123"));     // java.lang.NullPointerException

Friday, January 08, 2016

Typical mistakes with String in Java

Just few typical mistakes developers do when dealing with Strings. It's common stuff and everybody knows that, but for some reasons I still find such things (even written by myself :-)).

1. Checking empty string

In old days (version 1.5 and lower) we used String.equal(""), but 1.6 brought us String.IsEmpty which is better and faster.
 // wrong/slow  
 if (name.equals("")) {  
 // correct/fast
 if (name.isEmpty()) {

2. Concatenation

Method String.concat creates new String object, it's OK to use it when you do operation only once, otherwise use operator + += operators (see below why).
 String s = "Hello ".concat("new").concat(" world!"); // "Hello new world!"  
Using operators + and +=, they do not use String.concat method but StringBuilder and it's important to know what it means to us.
 String s = "Hello " + "new" + " world!"; // "Hello new world!"
What actually happens when you use + operator is StringBuilder used:
 String s = new StringBuilder().append("Hello ").append("new").append(" world!").toString(); // "Hello new world!"
So conclusion - it's ok to use concat if you do one time operation, for any other situation I would recommend to use + or +=.

3. String Formatting

Very often in order to format string people either concatenate the string to achieve result or do replace or invent something else. I do it as well sometimes (what a shame!). Instead we should use String.format method to do that.
 int n = 10;  
 // wrong  
 String s = "I have " + Integer.toString(10) + " books";  
 // wrong  
 String s = "I have #num books".replace("#num", Integer.toString(10));  
 // correct  
 Strig s = String.format("I have %d books", n);  

I would be glad to hear other typical mistakes we do with String object! So please share your experience.