How to static import math class in Java

Here we will see how can we import math class in Java. As we already know to import any class from any package we use the following notation import packagename.classname; example import java.util.Scanner; Here java is package util is subpackage and Scanner is a class. By importing the Scanner class we can use all its … Read more

How to Convert Java Object to/from JSON using GSON

Gson is Java JSON library that is used to convert Java object to/from JSON GSON is developed by google. GSON Maven dependency is as below com.google.code.gson gson 2.8.5 You can also download Gson Jar from here Gson jar contains API to work with JSON GOal of GSON is provide simple methods to convert and receive … Read more

Java Finding a sub string in string

indexOf(String str) return int value that is the index value of str if found in string other wise it returns -1.The index value start from 0. contains(CharSequence str) returns boolean value if str is present in given string it will return true else false.

for, while, do while and for each loops in Java

In a programming language we have to repeat set of instruction many time as per programmer’s requirements. To repeat instructions java provides looping statements. Looping is also known as Iterative statementFollowing are types of loop in java.1. For loop statement2. While loop statement3. Do while loop statement4. For each loop (enhanced for loop) Work of … Read more

Leap Year Program in Java

A Leap Year is a year that has 366 days. This year February is of 29 days. Leap years occur in every four years. A leap year has the following constraints. We can check Leap Year Program in Java as below A year is divisible by 400 then it is a leap year A year … Read more

Exception Handling in Java: Hierarchy Example and Types

One of the important topic Exception handling in java Here we will discuss basics of exceptions, Why Exception handling and its types. What is Exception in Java A unwanted event that disrupts the program execution. In Java all Exceptions are class. Why Exception Occurs Due to wrong user input Incorrect programming logic Example of Exceptions … Read more

Prime number program in java using for loop

Prime number program in java using for loop is very simple. we already discussed the prime number example with the while loop. Prime number program in java using for loop import java.util.Scanner; public class PrimeNumberChecker { public static void main(String[] args) { System.out.println(“Enter a number to check Prime or Not”); Scanner scanner = new Scanner(System.in); … Read more

Contains list java contains() in Java ArrayList

In Java, the term “contains list java” is used to check whether a particular object or element is present in a list or collection. The ArrayList method contains() is used to check specified elements in List. Contains Syntax public boolean contains​(Object o) Parameters: The method takes a single parameter, which is the object or element that you want … Read more

Default value of string in Java

In Java, the default value for a String variable is null. When a String the variable is declared but not assigned a value, it automatically takes the null value. Here’s an example: public class Main { static String str; // Declaring a String variable without assigning a value public static void main(String[] args) { System.out.println(str); … Read more

Sending mail using java mail API

Java Mail API is a platform and protocol independent framework to send mail. It supports JDK 1.4 and above. It supports IMAP, POP3 and SMTP protocol. To use Java Mail API we have to include javax.mail.jar in your class library. <dependencies> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency> </dependencies> import java.util.*; import javax.mail.*; import javax.mail.internet.*; public class … Read more