Reading Substring in Java

A substring is a String object created from another String object. In Java, you can get substring in multiple ways based on which String class you are using. Classes like String, StringBuffer, StringBuilder have substring and subSequence methods which can return substring. One thing we have to remember is that the index of string start … Read more

Comparing Two String Objects in Java

String class provides equals method for comparing two String objects. Comparing two String variables: Output: However, this may result in NullPointerException if str1 is null. So, it is always better to check if the reference variable is not null, which is calling the equals method. Output: You don’t have to null check for the variable … Read more

Print Prime Numbers in Java

In this post we will see couple of examples on how to print prime numbers in Java. A positive integer is called Prime Number when it is greater than 1 and divisible only with itself and by 1. Example: 2, 3, 5, 7, 11, 13, 17, 19 etc., Print Prime Numbers using custom square root … Read more

Finding Square Root of a Number in Java

In this post we will see an example on finding the Square Root of a Number. Square Root of a given number n is y, such that square of y is n. Below is the program to print square root of a given number. Program: Output:

Command-line arguments in Java

In Java, you can receive command-line arguments through the main function. Command-line arguments are those arguments which will be passed to a program through command-line. Program: Input: Output: