Tuesday 24 October 2017

2. Installing gradle

1. Installing gradle
  1. Before we get and install Gradle, we must make sure we have a Java Development Kit (JDK) installed on our computer.
  2. Gradle requires JDK 5 or higher. Gradle will use the JDK found at the path set on our computer.
  3. Although Gradle uses Groovy, we don't have to install Groovy ourselves,  Gradle bundles the Groovy libraries
  4. We can install in 2 ways
    1. Install manually : Gradle is available on the Gradle website, at http://www.gradle.org/downloads. From this page we can download the latest release of Gradle.
    2. Install with a package manager : Using various package managers
      1. SDKMAN - is a tool for managing parallel versions of multiple Software Development Kits on most Unix-based systems.
      2. Homebrew - is “the missing package manager for macOS”
      3. Scoop - is a command-line installer for Windows inspired by Homebrew
      4. MacPorts - is a system for managing tools on macOS
  5. Command using package manager
    1. $ sdk install gradle 4.2.1
    2. $ brew update && brew install gradle
    3. $ scoop install gradle
    4. $ sudo port install gradle
  6. Manually install procedure
    1. Download the latest Gradle distribution
    2. Unpack the distribution
    3. Configure your system environment
      1. $ export PATH=$PATH:/opt/gradle/gradle-4.2.1/bin
    4. Verify your installation
      1. gradle -v
2. Run First gradle script
  1. It is time to create our first Gradle build script, Gradle uses the concept of projects to define a related set of tasks.
  2. A Gradle build can have one or more projects.
  3. A project has one or more tasks.
  4. Tasks are a unit of work that need to be executed by the build, examples of tasks are compiling source code, packaging class files into a JAR file, running tests, or deploying the application.
  5. We use the gradle command to run a build. Gradle will look for a file named build.gradle in the current directory.
  6. No need to create a project, just create a file with name ‘build.gradle’ and write the below code in that file.
task helloWorld << {
println 'Hello world.'
}
  1. In terminal or cmd and go to the ‘build.gradle’ file path and un the following command
    gradle helloWorld
  2. It will print the Hello world. in terminal console.(It will print task name also)
  3. Along with result we will get so many message, if we want to suppress the unwanted messages we need to use below command.
                      gradle  --quiet helloWorld
                      gradle  --q helloWorld
  1. Above command will give results and any error messages if any.

10. Introduction to lambda expressions

1. What is Lambda expression?
  1. One issue with anonymous classes is that if the implementation of your anonymous class is very simple, such as an interface that contains only one method, then the syntax of anonymous classes may seem unwieldy and unclear.
  2. If interface contains only one method, then also we need to mention the method name in anonymous class declaration.
interface Runnable {
       public void run();
}



Runnable runnable = new Runnable() {
           public void run() {
               System.out.println(“i am a new thread”);
           }
 };


  1. In above example, i am creating an implementation class (anonymous class) of Runnable interface, but still i need to mention method name ‘run()’.
  2. In this case by using lambda expression, we can directly pass functionality as an argument to another method to treat functionality as method argument, or code as data.
interface Runnable {
       public void run();
}



Runnable runnable = () ->  System.out.println(“i am a new thread”);


  1. We can use lambda expression, if the interface contains only one method.
  2. A lambda expression can be passed around as if it was an object and executed on demand.


2. Syntaxes of Lambda expressions
  1. Lambda expressions supports interfaces only, Creating an instance to implementation class of a functional interface (interface contains only one method).
  2. Suppose java.lang.Runnable interface is a functional interface, it contains only one method called ‘run()’.
  3. We can assign a block of code or method without a name to variable of type interface directly by using lambda expressions.
          Runnable runnable = () -> {
               System.out.println(“i am a new thread”);
           };
  1. Block of code contains syntax like this  :       
    (arguments list)  ->  { block of code };
  2. In lambda expression, no need to mention the name of the block.
  3. We can pass the list of arguments with or without data type mentioning.
  1. But recommended way is don’t mention data type, java compiler will detect the data type of the argument by looking into the functional interface.
  2. Suppose if we mention wrong data type (other than data type declared in interface method) it will give compile time error.
  3. It will also not consider inheritance relationship also, we need to mention exact type, not child type or not parent type, otherwise don’t mention any type it will add type.
  1. Java compiler automatically detects argument types and return type of the lambda expression.
  2. No need to mention the return statement also, by default it returns last statement.
  3. If you write last statement as improper, it gives compile time error.
  1. If you want we can also mention return statement, but it is optional to mention, but if you mention return statement need to write ‘{ }‘ code only.

  1. If block contains only one line of code than optional to use ‘{‘.


Monday 23 October 2017

1. Introducing Gradle

1. Why Gradle ?
  1. Gradle is a tool for build automation.
  2. We can automate the compiling, testing, packaging, and deployment of our software or other types of projects.
  3. Gradle uses a Domain Specific Language (DSL) based on Groovy to declare builds.
  4. Groovy is a language for the Java Virtual Machine (JVM), such as Java and Scala.
  5. Gradle is an open source build automation system that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven for declaring the project configuration.
  6. Gradle provides support for Java, Groovy, Scala, Web, and OSGi projects.
  7. We can write gradle build scripts in java, groovy, scala.


2.  Advantages of gradle

  1. Incremental builds :
    1. This means tasks in a build are only executed if necessary.
    2. For example, a task to compile source code will first check whether the sources since the last execution of the task have changed. If the sources have changed, the task is executed, but if the sources haven't changed, the execution of the task is skipped and the task is marked as being up to date.
  2. Multi-project builds :
    1. Gradle has great support for multi-project builds.
    2. A project can simply be dependent on other projects. We can define a graph of dependencies between projects, and Gradle can resolve those dependencies for us. We have the flexibility to define our project layout as we want.
    3. Gradle has support for partial builds. This means Gradle will figure out if a project that our project depends on needs to be rebuilt or not. And if the project needs rebuilding, Gradle will do this before building our own project.
  3. Gradle wrapper :
    1. The Gradle wrapper allows us to execute Gradle builds, even though Gradle is not installed on a computer.
    2. This is a great way to distribute source code and provide the build system with it, so that the source code can be built.
  4. Open source :
    1. Gradle is an open source project and it is licensed under the Apache Software License (ASL).

8. Anonymous classes

1. Anonymous classes
  1. Anonymous classes enable you to make your code more concise.
  2. They enable you to declare and instantiate a class at the same time.
  3. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.
interface HelloWorld {
       public void greet();
       public void greetSomeone(String someone);
}

public static void main(String... args) {
HelloWorld frenchGreeting = new HelloWorld() {
   String name = "tout le monde";
   public void greet() {
       greetSomeone("tout le monde");
   }
   public void greetSomeone(String someone) {
       name = someone;
       System.out.println("Salut " + name);
   }
};

frenchGreeting.greetSomeone("Fred");
}
  1. The syntax of an anonymous class expression is like the invocation of a constructor, except that there is a class definition contained in a block of code.
  2. The anonymous class expression consists of the following:
    1. The new operator
    2. The name of an interface to implement or a class to extend.
    3. Parentheses that contain the arguments to a constructor, just like a normal class instance creation expression. Note: When you implement an interface, there is no constructor, so you use an empty pair of parentheses, as in this example.
    4. A body, which is a class declaration body. More specifically, in the body, method declarations are allowed but statements are not.
  3. An anonymous class has access to the members of its enclosing class.
  4. An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final.
  5. You cannot declare static initializers or member interfaces in an anonymous class.
  6. An anonymous class can have static members provided that they are constant variables.
  7. can declare the following in anonymous classes:
    1. Fields
    2. Extra methods (even if they do not implement any methods of the supertype)
    3. Instance initializers
    4. Local classes

3. Java Program to create Binary Tree