JShell in Java 9 - Part 3

 JShell default package: 

Now we will discuss the package related concept, by default how many packages are available in jshell.

Let's see a few examples:

EX.1:-

jshell> Math.sqrt(4)
$4 ==> 2.0 
| created scratch variable $4 : double

EX.2:-

jshell> Math.max(10,20) 
$5 ==> 20
| created scratch variable $5 : int 

EX.3:-

jshell> Math.random() 
$6 ==> 0.6956946870985563
| created scratch variable $6 : double 

  • Here you can see we didn't include any package in the jshell still Math class is working because of the java.lang package which is by default available to jshell.
Let's check the whether this code will work in jshell or not?


jshell> ArrayList<String> l=new ArrayList<String>();
l ==> []

As ArrayList, is available in java.util package. So how it is working even we are not importing java.util package in jshell.
By seeing the output it is clear that java.util package is also available to jshell, that's why it has created the empty ArrayList obj.

We can do multiple operations on ArrayList obj like add, remove, getting the size of obj.

Refer the below operation:



There is one question that :

Q1. How many packages are imported by default to jshell?

There are exactly 10 packages available to jshell. How to check which packages are being imported by default to jshell.

To check we need to use /imports command.
Refer the below image:












So, this is all about packages.


Jshell Compiler

Internally jshell having Java compiler which is responsible to check syntax. If any violation in syntax then we will get Compile time error which is exactly the same as normal compile time errors.

Below is the example of compile-time error:

EX.1:




In the Above example, we will get the compile-time error as we didn't declare X and Y so jshell compiler will give compile time error.

EX.2














In this case, jshell compiler will give the compile-time error because we have typed (Sytsem) instead of System and it is not recognized by jshell compiler hence it throws compile time error.



Let's Watch the difference between jshell compiler and java normal compiler:-

In our program, if there is any chance of getting checked exceptions compulsory we required to handle either by try-catch or by throws keyword. Otherwise, we will get a Compile time error.

But in the case of Jshell, jshell itself will take care of these and we are not required to use try-catch or throws. Thanks to Jshell. 

import Java.io.*;
class Test 
 {
    public static void main(String[] args) 
 {
   PrintWriter pw=new PrintWriter("abc.txt"); 
    pw.println("Hello"); 
  }
}

When we run this java program using normal java compiler, then we will get the compile-time error.
but if run the same program in jshell we will not get the compile-time error.
below is the code for jshell


jshell> PrintWriter pw=new PrintWriter("abc.txt");pw.println("Hello");pw.flush(); 
pw ==> Java.io.PrintWriter@e25b2fe 


Conclusions:


  1.  From the jshell we can execute any expression, any Java statement.
  2.  Most of the packages are not required to import to the Jshell because by default already available to the jshell.
  3.  Internally jshell use Java compiler to check syntaxes
  4.  If we are not handling any checked exceptions we won’t get any compile time errors, because jshell will takes care.


JShell in Java 9 - Part 2

Getting Started with JShell: 

Q1. How to Start and Stop JShell?

Open the Jshell from the command prompt in verbose mode. Using this command jshell -v Or jshell.

We will discuss further the difference b/w  jshell and jshell -v command.

Refer the below Image:
















Q2.How to exit JShell?

Just type this command /exit.

Refer the below Image:














Now its time to discuss the difference b/w with -v and without -v.


-v stands for verbose mode. It means in verbose mode we will get the extra information from the Jshell.

Let's see the example in verbose mode and non-verbose mode.

  • Without -v

















  • With -v



Note: If any information displaying on the jshell starts with '|' pipe symbol, it is the information to the programmer from the jshell.

EX:

Refer the below example:

 jshell> System.out.println("Hello World")
 Hello World














  In the above example, we observe that the output does not start with '|' pipe symbol because it is not information from the Jshell.

Note: Terminating semicolons are automatically added at the end of complete snippet by JShell if not entered.





JShell In Java 9 - Part 1

Introduction to the JShell:

This Feature was introduced by Java from Java 9.

Q1.What is Jshell? 
   
Jshell is Java's Shell. It is also known as an Interactive console. 
Jshell is Java's own REPL tool.
REPL  stands for  Read Execute Print Loop.
Refer this image.
By using this tool we can execute Java code snippets and we can get immediate results. 

Using Jshell we can test and execute Java expressions, statements, methods, classes etc. After testing small code we can plug that code in our project.

Before Java 9 we cannot test a small piece of Java code without any Java class. But from Java 9 onwards we can execute the small piece of without having complete class structure.

Its a new thing for java but this kind of tool is already available in the various language like Python, Scala, Swift.

Limitation of Jshell:
  •  JShell is not meant for Main Coding. We can use it to test small coding snippets, which can be used in our Main Coding. 
  •  JShell is not a replacement of Regular Java IDEs like Eclipse, NetBeans etc.





Java 8 and 9 Enhancement in Interface Part -1

Java 8 and 9 Enhancement in Interface Part -1
Hello Friends,

In this post, we will take a look at Java 8 and Java 9 Enhancement with respect to Interface.

As we already know in the interface every method is always public and abstract whether we are declaring or not. We can't put the normal method and this is the limitation of interface prior to Java 8 version.

Java 8 version onwards give you the ability to put default method and static method as well.

Now the question comes to mind why we need default method and static method in Interface.
To give you an answer to this question lets take an example.

Step :

Assume we have an interface with the name School and that has one method syllabus.

public interface School {
public void syllabus();
}

Now assume we have ten different class that implement School interface and provide his own implementation for syllabus method.

public class Class1 implements School {

@Override
public void syllabus() {
System.out.println("Class First");
}
}


public class Class2 implements School {

@Override
public void syllabus() {
System.out.println("Class Second");
}
}

and up to class10.

Assume our programming requirement is we have to extend the functionality of this
interface by adding a new method ExtraSub.

public interface School {
public void syllabus();
        public void extraSub(); 
}


Once we added new method extraSub to the interface then all the implementation classes will be
affected and won't be compiled, because every implementation class should implement all
methods of interface.

Hence to overcome this problem in Java 8 default method concept came. which are also known as Defender methods or Virtual Extension Methods.


How to Declare Default Methods inside interfaces :

public interface School {

        default void extraSub(){
System.out.println("Additional subject");
}
public void syllabus();
}

So now Interface default methods are by default available to all implementation classes. Based
on requirement implementation class can ignore these methods or use these default methods directly or can override.

So we can say a default method gives you the ability to add an additional feature without affecting implementation class.

Now assume we have added multiple default method in the interface and some have common functionality so there may be duplicate code to overcome this type of problem Java 9 has private method concept in the interface.

Example

public interface School {

         default void extraSub() {
System.out.println("Only for Class 9 and 10");
common();
}

default void extraSub1() {
System.out.println("Only for Class 9 and 10");
common();
}

default void extraSub2() {
System.out.println("Only for Class 9 and 10");
common();
}

default void extraSub3() {
System.out.println("Only for Class 9 and 10");
common();
}

private void common(){
System.out.println("Common code for all the default method");
}
public void syllabus();
}

So in the above example, we have added separately the common code into a private method and call that private method from each default method which required that functionality.