Categories
Java

Wrapper classes in Java with Examples

In Java, which is an Object Oriented Programming Language, everything revolves around Classes and Objects.

Java’s Collection Framework is a collection of rich sets of  data structures and various useful algorithms to manipulate different Data structures in it. Java’s mostly used primary operations are performed on built in data types like int, float double etc…. However, Java Collection Framework works only on Objects. to leverage the advantage of Collection Framework to these primary data types, we have Wrapper classes for each primary data type.

In Java we have 8 primitive types. They are byte, short, int, long, float, double, char and boolean. To leverage the Object Oriented advantages, Java introduced Wrapper classes for these Primitive types.

Below are the list of primitive types and their Wrapper classes

Primitive TypeWrapper Class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
booleanBoolean
charCharacter

So, in short, Wrapper Classes are used to convert primitive data types into objects and vice versa.

Let’s have a quick look with some Java code example on Wrapper classes

// Converting primitive types to Wrapper classes Example
int i = 12;
Integer intWrapper = new Integer(i);

float f = 512.32f;
Float floatWrapper = new Float(f);

char c = 's';
Character charWrapper = new Character(c);
// Converting primitive types to Wrapper classes Example
Integer in=new Integer(3); 
int i = in.intValue();

Character ch=new Character('s'); 
char c = ch.charValue();

Double db = new Double(5.99);
double d = db.doubleValue();

In Above Wrapper classes conversion example, we explicitly converted primitive types to Wrapper classes and Wrapper classes to Primitive Type. Java 5 Introduced New feature called AutoBoxing and Unboxing.

AutoBoxing and UnBoxing

,In Java Automatic Conversion of Primitive types into Wrapper Classes is called AutoBoxing. Here user no need to explicitly code to get the primitive value from corresponding Wrapper class In the same way If Java Compiler converts Wrapper classes to their corresponding primitive types, it is called Unboxing.

Let’s have a simple quick example of AutoBoxing and Unboxing in Java

//Blow AutoBxing, Compiler coverts automatically for us
int a = 12;  
Integer intObject = a;

//Blow UnBoxing, Compiler coverts automatically for us
Integer in=new Integer(3); 
int i = in;

Above is brief introduction of Wrapper classes for beginners. Please let me know In comments If you need anything else to be explained or need more details.

Categories
Linux

How to check the OS version in Linux? Simple and Easy methods

In our day to day life, sometimes it is required that we need to check the version of Operating System we are using. For example, If we need to install a software, we need to check whether our current OS supports that particular version or not.

In Windows It is easy to check, as it is entirely GUI based OS. However, in Operating Systems like Linux/Unix, where we prefer to use Command line instead of the GUI, it is difficult to remember all the commands.

In this post we are going to see How to check the Operating System details we are using in Linux, in 3 Simple methods. There are several ways we can check, but, below are the easy way to check the Linux OS version and OS details easily.

Linux Command to Check the OS Version and details

Using os-release file available in Linux’s etc directory

You can run below command to view the file, which contains Linux Operating System (OS) info

~$ cat /etc/os-release 

output of above

NAME="Ubuntu"
VERSION="18.04.4 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.4 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
Using lsb_release command

We can get the Linux OS info using lsb_release command. LSB Stands for Linux Standard Base

$ lsb_release -a

Output of lsb_release command is

~$ lsb_release -a

Distributor ID: Ubuntu
Description:    Ubuntu 18.04.4 LTS
Release:        18.04
Codename:       bionic
Using version file available in Linux’s proc directory

Same like os-release, we can use below command to view the version file

~$ cat /proc/version

output of above cat command is

~$ cat /proc/version
Linux version 4.15.0-1037-gcp (buildd@lgw01-amd64-031) (gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)) #39-Ubuntu SMP Wed Jul 3 06:28:59 UTC 2019

Above are my preferred methods to get the Linux OS details in simple and easy way.

What is your best command to get the OS details? mention them in comments below.

Categories
Java Software Testing

Java try catch finally block example | Exception Handling in Java

Before we start to know about try Catch Finally, we should know about Exceptions and its handling in Java.

In Java, according to Oracle’s Java documentation, An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.

For example, If a program is trying to read/write a file from a location, and if the file is not found or not accessible then we get FileNotFoundException exception.

Another example where most of the developer face issue in real time is NullPointerException also referred as NPE.One scenario when we get NPE is, when we try to updated an object which is null, then Java will through NPE. That’s why it is recommended to add null checks before updating any Java Object.

These are some of the exceptions which we face in our day to day life. There are many other exceptions available, which are out of the scope for this post. So, the main idea is when a program is being executed and when unintended event happens, Java will through Exception. When the exceptions are thrown, we may get issues like program halts or in Web applications user will see stack trace of an exception, which end users don’r know about.

To avoid these issues, we can handle Exceptions programmatically and if any unexpected event happens, then we can show the meaning full messages to users and log the required details for developer to debug.

This can be done by the trio called “try-Catch-Finally”.

These 3 try, catch and Finally are the building blocks of Exceptions handling. Let us see on by one

Try Block

Try block is the place where we have actual logic implemented. For example, File handling or updating the Objects and so on…

Try block is the place where Exceptions get generated.

When we suspect that particular code may have chance of throwing the Exception we surround that code with try block

Below is the example of try block

Private void printStringLength(String name){
            int n = name.length();
            System.out.println(“String “+name+” length is: “+n);
}

As we see in above example, first line in try block is trying to get the length of a String. Here if String is not empty, then everything works fine. But what if String is null, User will get unexceptced NPE error.

To avoid this, we need to surround the code where we suspect there is a chance of exception, with try block like below

private void printStringLength(String name){
       try{
            int n = name.length();
            System.out.println(“String “+name+” length is: “+n);
     }
}

catch block

catch block are where we handle the Exception thrown from try block. Code written in catch block are executed only when there is an Exception raised in try block, otherwise catch block is skipped.

In General when we developing application, we don’t want to show the user error messages which are not user friendly like “java.lang.NullPointerException” with some stack trace. Instead we want to show some meaningful information like “Given Name is empty” and then log the stack trace for developer to debug.

Above process of displaying meaningfull message to user/client is handled in catch block.

Below example of catch block

private void printStringLength(String name){
        try{
            int n = name.length();
            System.out.println(“String “+name+” length is: “+n);
        }catch(Exception e){
           System.out.println(“ Exception occurred while getting length“);
        }
}

finally block

Finally block, as the name suggest, is executed after executing try and/or catch blocks.

Usually we use finally if we want to perform some operation irrespective of whether an exception occurred or not.

For example,

We want to close a DB connection after performing some task in try block, but If any exception occurred there is a chance that rest of the code doesn’t execute, in that scenario we want to close connection. To close the connection, we can put the code in finally block.

Finally block make sure whatever code inside is executed 100% whether an exception occurs or not.

private void printStringLength(String name){
        try{
            int n = name.length();
            System.out.println(“String “+name+” length is: “+n);
        }catch(Exception e){
            System.out.println(“Exception occurred while getting length “);
        }finally{
            System.out.println(“In Finally “)
        }
}


Above is just a brief idea on how try catch finally works, still there is a vast amount of information left, which can be found in Oracle java docs and other places.

Categories
Mobile Deals

Realme 3 Pro (Carbon Grey, 64 GB)(4 GB RAM)


Take photography a notch higher with the Realme 3 Pro as it comes with a 25 MP AI Selfie Camera and 16 MP + 5 MP Rear Camera with Sony IMX519 Sensor. The Qualcomm Snapdragon 710 AIE, multi-core AI Engine and Adreno 616 GPU redefine your smartphone experience.
More >>

Price : INR 15999

Categories
Mobile Deals

Realme 3 (Radiant Blue, 64 GB)(4 GB RAM)


The Realme 3 redefines excellent performance and elegant design. Its Helio P70 AI Processor (12nm-class), combined with a powerful 4230-mAh battery, makes the Realme 3 a highly power-efficient smartphone. It has a 3D Unibody Design, that’s complemented by a gradient effect, which adds to your stylish persona. Its 13+2 MP AI Dual Camera, coupled with the Chroma Boost feature and Nightscape Mode, helps capture all the fine details of precious moments, whether it’s day or night.
More >>

Price : INR 12999

Categories
Mobile Deals

Redmi Note 7 (Onyx Black, 64 GB)(4 GB RAM)


Experience power and performance in your palm with the Redmi Note 7. Every picture that you click comes out beautiful, thanks to its (12 MP + 2 MP) dual-rear camera and the 13 MP front camera. While 16-cm (6.3) FHD+ Dot Notch Display revolutionises your viewing experience, features such as AI Scene Detection, Face Unlock and IR Remote Control make your user experience as convenient as possible.
More >>

Price : INR 14999

Categories
Mobile Deals

Realme 3 (Radiant Blue, 32 GB)(3 GB RAM)


The Realme 3 redefines excellent performance and elegant design. Its Helio P70 AI Processor (12nm-class), combined with a powerful 4230-mAh battery, makes the Realme 3 a highly power-efficient smartphone. It has a 3D Unibody Design, that’s complemented by a gradient effect, which adds to your stylish persona. Its 13+2 MP AI Dual Camera, coupled with the Chroma Boost feature and Nightscape Mode, helps capture all the fine details of precious moments, whether it’s day or night.
More >>

Price : INR 10999

Categories
Mobile Deals

Redmi Note 7 (Sapphire Blue, 64 GB)(4 GB RAM)


Experience power and performance in your palm with the Redmi Note 7. Every picture that you click comes out beautiful, thanks to its (12 MP + 2 MP) dual-rear camera and the 13 MP front camera. While 16-cm (6.3) FHD+ Dot Notch Display revolutionises your viewing experience, features such as AI Scene Detection, Face Unlock and IR Remote Control make your user experience as convenient as possible.
More >>

Price : INR 14999

Categories
Mobile Deals

Realme 3 (Dynamic Black, 64 GB)(4 GB RAM)


The Realme 3 redefines excellent performance and elegant design. Its Helio P70 AI Processor (12nm-class), combined with a powerful 4230-mAh battery, makes the Realme 3 a highly power-efficient smartphone. It has a 3D Unibody Design, that’s complemented by a gradient effect, which adds to your stylish persona. Its 13+2 MP AI Dual Camera, coupled with the Chroma Boost feature and Nightscape Mode, helps capture all the fine details of precious moments, whether it’s day or night.
More >>

Price : INR 12999

Categories
Mobile Deals

Redmi Note 7 (Sapphire Blue, 32 GB)(3 GB RAM)


Experience power and performance in your palm with the Redmi Note 7. Every picture that you click comes out beautiful, thanks to its (12 MP + 2 MP) dual-rear camera and the 13 MP front camera. While 16-cm (6.3) FHD+ Dot Notch Display revolutionises your viewing experience, features such as AI Scene Detection, Face Unlock and IR Remote Control make your user experience as convenient as possible.
More >>

Price : INR 12999