devxlogo

January 30, 2020

Understandng Objects.deepEquals

We understand how equals() method works. There is a more elaborate method deepEquals() which compares in depth details during comparison.Basic usage is described below. Let us explore more using these as examples. import java.util.Objects; public class DeepEquals{   public static void main(String args[])   {      DeepEquals deepEquals = new DeepEquals();      deepEquals.proceed();   }      private void proceed()   {      System.out.println(“Objects.deepEquals(1,1): ” + Objects.deepEquals(1,1));      System.out.println(“Objects.deepEquals(1,2): ” + Objects.deepEquals(1,2));      System.out.println(“Objects.deepEquals(“abc”,”abc”): ” + Objects.deepEquals(“abc”,”abc”));      System.out.println(“Objects.deepEquals(“aa”,”ab”): ” + Objects.deepEquals(“aa”,”ab”));   }} /* Expected output: [root@mypc]# java DeepEqualsGetting handle of runtime ConsoleGot handle of runtime ConsoleYou can now use runtimeConsole object to perform actions of your choice on java.io.Console */

Getting console of the current runtime environment.

The Runtime class provides mechanism to get the console of the current runtime environment. Using this, we can perform needed actions on the console. import java.io.*; public class SystemConsole{   public static void main(String args[])   {      SystemConsole systemConsole = new SystemConsole();      systemConsole.proceed();   }      private void proceed()   {      System.out.println(“Getting handle of runtime Console”);      Console runtimeConsole = System.console();      System.out.println(“Got handle of runtime Console”);      System.out.println(“You can now use runtimeConsole object to perform actions of your choice on java.io.Console”);   }} /* Expected output: [root@mypc]# java SystemConsoleObjects.deepEquals(1,1): trueObjects.deepEquals(1,2): falseObjects.deepEquals(“abc”,”abc”): trueObjects.deepEquals(“aa”,”ab”): false */

Unit Test Non-Public Methods in C# Unit Tests

To expose non-public methods to the test project, you need to mark the assembly with an attribute called InternalsVisibleTo in the asemblyinfo.cs file?? For example: [assembly: InternalsVisibleTo(“testProjectName”)] You need to

Converting Base 10 numbers to Binary numbers

Programming needs vary. You may have a requirement to convert a Base 10 value to binary as part of a complex logic. Java has easier mechanism to achieve the same. public class Base10ToBinary{   public static void main(String args[])   {      Base10ToBinary base10ToBinary = new Base10ToBinary();      base10ToBinary.proceed();   }      private void proceed()   {      int num = 10;       String binaryNum = Integer.toString(num, 2);         System.out.println(“Binary value of ” + num + ” : ” + binaryNum);    }} /* Expected output: [root@mypc]# java Base10ToBinaryBinary value of 10 : 1010 */

Updating a JAR file

At times, requirement to update an already delivered JAR file will need to be handled. Of course, JAR file is a way of packaging and delivering Java class file and related metadata files in a package. But having an easier way is always welcome by the developer group. The following command when executed on a command line will achieve updating a JAR file with 1 or more files as needed jar uf jar-file input-file-name(s) where uf          :    indicates update and filejar-file    :   the jar file that needs to be updatedinput-file-name(s)   :   the name of file(s) that needs to be updated in the jar file.

Finding out the Java version.

This is generally useful, specifically if you want to perform some operations based on Java version and so on. public class JavaVersion{   public static void main(String args[])   {      JavaVersion javaVersion = new JavaVersion();      javaVersion.proceed();   }      private void proceed()   {      //This works in Java 8 and prior      //For Java 9, there is a new api available in Runtime class      String javaVersion = System.getProperty(“java.version”);        System.out.println(“Java Version: ” + javaVersion);    }} /* Expected output: [root@mypc]# java JavaVersionJava Version: 1.8.0_221 */

How to use JPA, @MappedSuperclass

First, define an abstract class and annotate it with @MappedSuperclass. This is not an entity: @MappedSuperclasspublic abstract class User implements Serializable {   …} Second, each entity should extend the User class. For example, Student and Teacher entitites: @Entitypublic class Student extends User implements Serializable {   …} @Entitypublic class Teacher extends User implements Serializable {   …}

How to define an auto-incremented identifier via JPA annotations

For defining an auto-incremented identifier in an entity we need the @Id annotation and the IDENTITY generator as follows: @Entitypublic class User implements Serializable {     @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Long id;    …}