How to compare two Arrays in Java to check if they are equal - [String & Integer Array Example]

Hello guys, one of the common Programming, the day-to-date task is to compare two arrays in Java and see if they are equal to each other or not. Of course, you can't compare a String array to an int array, which means two arrays are said to be equal if they are of the same type, has the same length, contains the same elements, and in the same order. Now, you can write your own method for checking array equality or take advantage of Java's rich Collection API. Similar to what you have seen while printing array values in Java, java.util.Arrays class provides convenient methods for comparing array values.

How to Run a JAR file from Command Prompt - Windows and UNIX [Example]

So you got a JAR file that contains your favorite Java game and you are wondering how to run a Java program from a JAR  file? Or you may be simply trying your hand after running the Java program from the command prompt and Eclipse IDE. Anyway, it's good to know How to execute Java programs from the JAR file, as the JAR file is the most popular way of shipping Java programs. JAR file usually contains all sources and resources in the form of class files, images, sound files, etc. 

How to create User Defined Exception class in Java? Example Tutorial

Java has very good support for handling Errors and Exceptions, It has a well-defined Exception hierarchy and language level support to throw and catch Exceptions and deal with them. Java Programmers often deal with built-in exceptions from java.lang package and several others which are already defined in JDK API like NullPointerException. If you have read Effective Java, you may remember the advice of Joshua Bloch regarding Exception. According to him, you should try to reuse the Exception classes provided in the JDK, like IndexOutOfBoundException, ArithmeticException, IOException, and java.lang.ArrayIndexOutOfBoundsException , instead of creating new ones for a similar purpose.

Polymorphism and Open Closed Design Principle Example in Java

Java is an object-oriented programming language that allows developers to create complex software applications by organizing code into objects. One of the key features of object-oriented programming is Polymorphism. Polymorphism refers to the ability of objects to take on different forms, depending on the context in which they are used. In Java, polymorphism is achieved through two mechanisms: inheritance and interfaces. In this blog post, I will explain what polymorphism is in Java, how it works, and provide examples to illustrate its usage. We will also discuss the benefits of using polymorphism in your Java programs, as well as some best practices for implementing it effectively.

How to split String by comma in Java - Example Tutorial

In the earlier article, I have shown you how to split String by regular expression and now, you will learn how to split string by comma. Since CSV is a popular format for exporting data, you often need to split a comma-separated String to create an array of individual Strings. Similar to the earlier example, you can use the split() method to accomplish this task. Just pass a "," instead of "\\s+" the regular expression to remove whitespaces, as the regular expression.  The split method will return an array of String, which you can further convert to a list of String. 

How to Read, Write XLSX File in Java - Apache POI Example

No matter how Microsoft is doing in comparison with Google, Microsoft Office is still the most used application in software world. Other alternatives like OpenOffice and LiberOffice have failed to take off to challenge MS Office. What this mean to a Java application developer? Because of huge popularity of MS office products you often need to support Microsoft office format such as word, Excel, PowerPoint and additionally Adobe PDF. If you are using JSP Servlet, display tag library automatically provides Excel, Word and PDF support. Since JDK doesn't provide direct API to read and write Microsoft Excel and Word document, you have to rely on third party library to do your job. Fortunately there are couple of open source library exists to read and write Microsoft Office XLS and XLSX file format, Apache POI is the best one. It is widely used, has strong community support and it is feature rich.

Why is Abstract class Important in Java? [Example]

Abstract class is a special class in Java, it can not be instantiated and that's why can not be used directly. At first concept of abstraction, abstract class and interface all look useless to many developers, because you can not implement any method in an interface, you can not create an object of the abstract class, so why do you need them. Once they face biggest constant of software development, yes that is CHANGE, they understand how abstraction at the top level can help in writing flexible software. A key challenge while writing software (Java Programs, C++ programs) is not just to cater today's requirement but also to ensure that nurture requirement can be handled without any architectural or design change in your code. In short, your software must be flexible enough to support future changes.

How to use Modulo , Modulus, or Remainder Operator in Java? [Example]

Modulo Operator is one of the fundamental operators in Java. It's a binary operator i.e. it requires two operands. In a division operation, the remainder is returned by using the modulo operator. It is denoted by the % (percentage) sign. For example, 5%2 will return 1 because if you divide 5 with 2, the remainder will be 1. For a programmer it's very important to know how to use this operator, they are very important to build logic. For example, in many cases like reversing a number or checking if a number is a palindrome, you can use the modulus operator with 10 to get the last digit, for example, 101%10 will return 1 or 1234%10 will return 4, the last digit. 

HelloWorld Program in Java with Example

First of all, welcome to the exciting world of Java programming. If you are ready to write your first Java program i.e. HelloWorld in Java, it means you already crossed major hurdles to start Java programming, in terms of installing JDK and setting PATH for Java. If you haven't done so then you can follow those tutorials to install JDK in Windows 7 and 8 and setting PATH for Java. Before we start writing HelloWorld in Java, few notes about editors. Many Java beginners insist or try to use Eclipse or Netbeans IDE from the very start of the first program, which is not a good idea. 

How to Override Equals, HashCode and CompareTo method in Java

Though modern IDE like Eclipse, IntelliJ or Netbeans allows you to generate equals, hashCode and compareTo methods for your value classes, it's equally important, you know how to do that by hand. By overriding equals and hashcode method by yourself, you know how they work, what kind of errors you can get, and most importantly, it's expected form you, as a Java programmer in any core Java interview. More often, you would see a coding question in Java, which ask you to override equals(), hashcode(), compare() and compareTo() methods for a value class. 

Difference between Comparator and Comparable in Java - Interview Question

Comparator and Comparable are two interfaces in Java API, which is used to compare two objects in Java. Though both are used for comparison there are some differences between them, a major difference between Comparable and Comparator is that the former is used to define the natural ordering of objects e.g. lexicographic order for java.lang.String, while later is used to define any alternative ordering for an object.  The main usage of java.lang.Comparable and java.util.Comparator interface is for sorting a list of objects in Java.

How to Split String in Java using Regular Expression

String class provides split() method to split String in Java, based upon any delimiter, e.g. comma, colon, space or any arbitrary method. split() method splits the string based on delimiter provided, and return a String array, which contains individual Strings. Actually, split() method takes a regular expression, which in simplest case can be a single word. split() is also overloaded method in java.lang.String class and its overloaded version take a limit parameter which is used to control how many times the pattern will be applied during the splitting process. if this limit is positive n, then the pattern will be applied at most n-1 times, if it's negative or zero then split operation is applied any number of times.

How to Convert java.util.Date to java.sql.Date - Example

Hello guys, you may not know but there are two date classes in Java, one in java.util package and other in the java.sql package. Though both are known as Date class, there is some difference between java.util.Date and java.sql.Date e.g. Former is used whenever a Date is required in Java application while later is used to read and store DATE SQL type from the database. There is one more important difference is, java.util.Date stores both date and time values, while java.sql.date only stores date information, without any time part. As per Javadoc, java.sql.date is a thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated. See SQL date vs Util date for few more differences.

[Solved] How to convert Decimal to Binary Number in Java? Example Tutorial

Hello guys, how are you doing? I have been sharing a lot of programming interview questions and coding problems for practice in this blog. Earlier, I have shared 21 String programing questions, 100+ data structure questions, and 20 System design questions and this week's programming exercise is to write a program to convert a decimal number to binary in Java. It's a simple exercise for beginners who have just started learning the Java programming language. Decimal numbers are base 10 numbers, which means there are exactly 10 digits to represent a number, starting from 0 to 9, on the other hand, the binary number system has just two digits 0 and 1, which is known as bits. 

How to convert String to int or Integer data type in Java? Example Tutorial

Hello guys, if you are wondering how to convert a String variable to int or Integer variable in Java then you have come to the right place. Earlier, I have showed how to convert Integer to String in Java and today, you will learn about how to convert String to int or Integer variable in Java. There are 3 main ways to convert String to int in Java, first, by using the constructor of Integer class, second, by using parseInt() method of java.lang.Integer, and third, by using Integer.valueOf() method. Though all those methods return an instance of java.lang.Integer, which is a wrapper class for primitive int value, it's easy to convert Integer to int in Java. 

Can You Run Java Program Without a Main Method? [Interview Question Answer]

Hello guys, the first thing Java programmers learn is that they need a main method to run, but when they go to any Interview or college viva and ask can run a Java program without a main method, they are surprised like hell. Well, there are actually different types of execution models available in Java; for example, Applets, which run on the browser don't have the main method. Instead, they have life-cycle methods like init(), start() and stop(), which controls their execution. Since Applet is a Java program, you can answer this question is Yes. Similarly, we have Servlet, which runs in a Servlet container, comes as bundled in a web server like Tomcat, or Jetty.