Tuesday, April 23, 2019

JAVA 11 – New String methods


JAVA 11 – New String methods

In this blog, we will discuss about all six new methods which got introduced with JDK 11.

  • isBlank(): returns true if string is empty otherwise false. White spaces code will also be considered as empty string.

// Java 8 Predefine Functional Interface
             // The best way to learn JAVA 8 is to implement it wherever you can...
             BiConsumer<String, String> display = (msg1, msg2) -> System.out.println(msg1 + msg2);

             // local variable type inference
             var msg = "Welcome to waheedtechblog page";
             // String msg = "Welcome to waheedtechblog page"; both are same

             // Will return true if string is empty or any white spaces
             if (msg.isBlank()) {
                    display.accept("msg attribute is emty", msg);
             } else {
                    display.accept("1. Hello, ", msg);
             }

String emptyMsg = "";
display.accept("2. Is msg Empty: ", String.valueOf(emptyMsg.isBlank()));

             String whitespacesMsg = "\n\t";
display.accept("2. Is msg empty: ", String.valueOf(whitespacesMsg.isBlank()));
Output:
            1. Hello, Welcome to waheedtechblog page
2. Is msg Empty: true
3. Is msg empty: true

  • lines(): This method returns a stream of lines extracted from the string, separated by line terminators such as \n, \r etc.

            String lineMsg = "Welcome\nto\nwaheedtechblog\npage";
       display.accept("3. Line Msg: \n", lineMsg);
       // line returns streams of line extracted from the String
       List<String> lines = lineMsg.lines().collect(Collectors.toList());
       System.out.println("4. line as list: " + lines);
Output:
       3. Line Msg:
Welcome
to
waheedtechblog
page
4. line as list: [Welcome, to, waheedtechblog, page]

  • repeat(n): This method returns the concatenated string of original string repeated the number of times in the argument.

var repeatMessage = "Welcome";
// returns a new string whose value is the concatenation of this string repeated
             // ‘n’ times.
             display.accept("9. Repeat msg: ", repeatMessage.repeat(5));
Output:
9. Repeat msg: WelcomeWelcomeWelcomeWelcomeWelcome

  • Strip(), StripLeading(),StripTrailing(): These method are used to strip white spaces from the String

                   var stripMsg = "    Welcome to   \t Waheedtechblog page     ";
             display.accept("5. Normal Print: ", "$" + stripMsg + " $");
             // As the name suggests, Strip() methods are used to strip whitespaces from the
             // string.
             display.accept("6. Using Strip(): ", "$" + stripMsg.strip() + "$");
             display.accept("7. Using StripLeading(): ", "$" + stripMsg.stripLeading() + "$");
display.accept("8. Using StripTrailing: ", "$" + stripMsg.stripTrailing() + "$");

Output:
5. Normal Print: $    Welcome to        Waheedtechblog page      $
6. Using Strip(): $Welcome to    Waheedtechblog page$
7. Using StripLeading(): $Welcome to    Waheedtechblog page     $
8. Using StripTrailing: $    Welcome to        Waheedtechblog page$

You can download the source code from my GitHub repository.

Happy Coding…!!!

Sunday, April 21, 2019

Java 10 - Local Variable Type Inference


Java 10 - Local Variable Type Inference

In one of my previous blog, I have already discussed about Type Inference. If you don’t have the idea about Type Inference then you can check my blog here.

If we want to understand Type Inference in one line then we can say that It is the capability of the compiler to automatically detect the datatype of a variable at the compiler time.

What is Local Variable type inference?

Java 10 added new feature that allows the developer to skip the type declaration associated with local variables (those defined inside method definitions, initialization blocks, for-loops, and other blocks like if-else), and the type is inferred by the JDK. It will, then, be the job of the compiler to figure out the datatype of the variable.
Until Java 9, we had to define the type of the local variable.

E.g.: String message = “Welcome to Waheedtechblog.com”;

The above statement is right as that’s how things have been since the inception of java but if you observe, the type of the object at right side is clearing mentioning the type of data that we have defined at left side which makes the variable redundant.
So with Java 10, We can declare a local variable without defining its data type at left side.

            Var message = “Welcome to Waheedtechblog.com”;

Over here, we don’t have to provide the data type of String to local variable message. Compiler will get the datatype based on right hand side value.

Note:
  • This feature is available only for local variables with the initializer. It cannot be used for member variables, method parameters, return types, etc as the initializer is required as without which compiler won’t be able to infer the type.
  • To support backward compatibility, var is not a java reserved keyword. So, we can create variable with name var as it is allowed.

Let’s see where all we can use var

package com.waheedtechblog.typeinference;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class TypeInferenceExample {

       static {
             // static block variable
             var staticVaribale = "var in static block";
             Consumer<String> displayOnConsole = msg -> {
                    System.out.println(msg);
             };
             displayOnConsole.accept(staticVaribale);

       }

       public static void main(String[] args) {

             TypeInferenceExample inferenceExample = new TypeInferenceExample();

             BiConsumer<String, String> displayOnConsole = (str, msg) -> {
                    System.out.println(str + msg);
             };

             // Before Java 10
             String message = "Welcome to Waheedtechblog.com";
             displayOnConsole.accept("Before JDK 10: ", message);

             // Using JDK 10, Local Variable
             var message10 = "Welcome to Waheedtechblog.com";
             displayOnConsole.accept("With JDK 10: ", message);

             // local variable declaration in enhanced loops
             String[] countryName = { "India", "Japan", "UAE", "USA", "UK" };
             displayOnConsole.accept("", "List of Country using enhanced loop");
             for (var country : countryName) {
                    displayOnConsole.accept("", country);
             }

             // basic for loop
             displayOnConsole.accept("", "List of Country using basic loop");
             for (var i = 0; i < countryName.length; i++) {
                    displayOnConsole.accept("", countryName[i]);
             }

             displayOnConsole.accept("Length of string '" + message10 + "' is: ",
                           String.valueOf(inferenceExample.getLenth(message10)));


             // var is not keyword, so you can create variable with name var as well.
     String var = "var is not keyword";
     displayOnConsole.accept("", var);
       }

       // can return var type as well
       private int getLenth(String msg) {
             var length = msg.length();
             return length;
       }

}

Output:
var in static block
Before JDK 10: Welcome to Waheedtechblog.com
With JDK 10: Welcome to Waheedtechblog.com
List of Country using enhanced loop
India
Japan
UAE
USA
UK
List of Country using basic loop
India
Japan
UAE
USA
UK
Length of string 'Welcome to Waheedtechblog.com' is: 29
var is not keyword

Illegal use of Var:

1.    Can’t use as a class field
Class Test {
            Var msg; // not allowed as it is local variable inference
}
2.    Local variable without initialization
Private void getOTP(){
            Var otp ; // cant use without initializer
}
3.    Not allowed as parameter for any methods
Private void getOTP(var secretKey){
            //Error, cannot use var on method parameter
}
4.    Not permitted in method return type
Private var getOTP(String secretKey){
            //Error, Method return type cannot be var
}
5.    Not permitted with variable initialized with ‘NULL’
Private String getOTP(String secretKey){
            Var msg = null; Error, cant initialize as null
}

You can download the source code from my GitHub repository.

Happy Coding..!!!









How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...