Sunday, March 31, 2024

How to enable or disable form elements using jQuery? Examples

In last couple of tutorials you have learned how to get all selected checkbox and radio buttons, and in this tutorial, you will learn how to enable/disable form elements using jQuery. A form element e.g. a textfield, radio buttons and checkbox, all represented using input tag can be enabled or disabled by adding an attribute called "disabled". It's similar to "checked" attribute we have seen for checkbox and radio buttons. If this attribute is not present then element is enable, but if this attribute is present then you can use disabled=true to disable the form element and disabled=false to enable the form elements as shown in our example. Like previous example of checking/unchecking checkbox, you can use prop() jQuery function to enable/disable form element if you are running on jQuery 1.6 or higher version and attr() function if you are running on jQuery 1.5 or lower version. 

How to shuffle a List in Java? Collections.shuffle() Example

Hello guys, if you have a List of numbers and you want to shuff it but don't know how then you have come to the right place. Shuffling is an important technique which is used quite a lot on coding games like number guessing games and card games., The java.util.Collections class provides shuffle() method which can be used to randomize objects stored in a List in Java. Since List is an ordered collection and maintains the order on which objects are inserted into it, you may need to randomize elements if you need them in a different order. Collections.shuffle() method uses default randomness to randomize elements but you also have an overloaded version of shuffle() to provide an instance of the java.util.Random object, which can be used to randomize elements.

Saturday, March 30, 2024

How to Compare Two Dates in Java? Check If They Are Equal, Later or Earlier - Examples

There are multiple ways to compare two dates in Java, it also depends on what exactly comparison means. If you are looking to compare two dates to find out whether they are equal or not, then you can simply use the equals() method of java.util.Date class. This method will return true if both dates are equal, precisely both have the same millisecond value. If you are looking to find out whether a date comes before or after another date then you have 3 choices, you can use the compareTo() method of java.util.Date class, or you can use before() and after() method of Date class, or you can use before() and after() method of Calendar class. I suggest leveraging the before() and after() method of the Date class, they are more readable and easy to use.

Friday, March 29, 2024

What is transient variable in Java? Serialization Example

What is a transient variable in Java?
transient variable in Java is a variable whose value is not serialized during Serialization and which is initialized by its default value during deserialization, for example for object transient variable it would be null. This behavior can be customized by using a custom Serialized form or by using the Externalizable interface. A transient variable is used to prevent any object from being serialized and you can make any variable transient by using the transient keyword. You cannot make a local variable transient through and it's only the member variables which can be transient. 

Wednesday, March 27, 2024

Difference between HashSet and HashMap in Java? Answered

HashSet and HashMap in Java
Hello friends, if you have given Java developer interview then there is good chance that you may have come across questions like Difference between HashSet vs HashMap or HashSet vs TreeSet etc. In this article, we are going to discuss differnece between HashMap and HashSet, two of the popular Collection classes from JDK. The HashSet vs HashMap is a classical Java Collection interview question that focuses on What are differences between HashSet and HashMap in terms of features, usage, and performance. If you are in Java programming even for a year or so, you are likely to be familiar with What is HashSet in Java and What is HashMap in Java, these two are the most popular collection classes. 

Tuesday, March 26, 2024

What is PATH and CLASSPATH in Java? Path vs ClassPath Example

What is PATH and CLASSPATH in Java
The PATH and CLASSPATH are the two most important environment variables of the Java environment which are used to find the JDK binaries used to compile and run Java in windows and Linux and class files which are compiled Java bytecodes. From my personal experience I can say that PATH and CLASSPATH are the two most problematic things for beginners in Java programming language due to two reasons; first because in most Java courses nobody tells details of what is a PATH and CLASSPATH, What do PATH and CLASSPATH do, What is meaning of setting PATH and CLASSPATH, What happens if we do not set them, Difference between PATH vs CLASSPATH in Java or simply How Classpath works in Java, etc. 

Monday, March 25, 2024

Why is main method public, static, and void in Java? Answer

What is the main method in Java?
The main() method in Java is a special method that is used by JVM to start the execution of any Java program. The main method is also referred to as the entry point of Java application which is true in the case of core Java applications because execution starts from the main() method, but in the case of container-managed environments like Servlet, EJB, or MIDlet this is not true as these Java programs have their own life-cycle methods like init(), service() or destroy() for Servlet's which is used by the container. The main method in Java is run by the main thread which is a non-daemon thread and the Java program runs until the main method finishes or any other user thread is running. 

Sunday, March 24, 2024

How to convert JSON String to Java Object using Gson? JSON Deserialization Example

Hello Java Programmers, In the last article, you have learned how to convert a Java object to JSON String and in today's article, you will learn the opposite, i.e. converting a JSON String to a Java object. The first example was known as JSON serialization example and this one is known as JSON deserialization because we are creating a Java object from a String. The idea is very similar to classical Serialization in Java where you convert a Java object to another binary format that can be transported over the network or can be saved in the disk for further usage.

Difference between var, val, and def in Scala? Examples

Hello guys, if you are new to Scala or preparing for Scala interview then you may have come across the question, what is difference between var, val, and def keywords in Scala? This is one of the frequently asked questions from Scala interviews. Even though both var and val keyword is used to declare variables in Scala there is a subtle difference between them. A var is a variable. It’s a mutable reference to a value. Since it’s mutable, its value may change throughout the program's lifetime. On the other hand, the val keyword represents a value. It’s an immutable reference, meaning that its value never changes. Once assigned it will always keep the same value.

3 ways to convert String to JSON object in Java? Examples

It's very common nowadays to receive JSON String from a Java web service instead of XML, but unfortunately, JDK doesn't yet support conversion between JSON String to JSON object. Keeping JSON as String always is not a good option because you cannot operate on it easily, you need to convert it into a JSON object before you do anything else e.g. retrieve any field or set different values. Fortunately, there are many open-source libraries which allows you to create JSON object from JSON formatted String like Gson from Google, Jackson, and json-simple. In this tutorial, you will learn how to use these 3 main libraries to do this conversion with step-by-step examples.

Saturday, March 23, 2024

Difference between Correlated and Non-Correlated Subquery in SQL? Examples

Hello guys, the difference between correlated and non-correlated subquery is one of the popular SQL interview question and an important concept for any programmer to learn. The correlated subquery is one of the tricky concepts of SQL. It's similar to recursion in programming which many programmers struggle to understand, but like recursion, it also offers the unique capability to solve many SQL query-based problems like the second-highest salary problem where you need to compare one row of the table to another row. It gives you a different kind of power. The main difference between a regular, non-correlated, and correlated subquery in SQL is in their working, a regular subquery just runs once and returns a value or a set of values that is used by the outer query.

How to sort ArrayList in Java? Examples

Hello Java programmers, sorting ArrayList in Java is not difficult; there are multiple ways to sort a given ArrayList in Java. For example you can use  the Collections.sort() method to  sort ArrayList in ascending and descending order in Java. The  Collections.sort() the method optionally accepts a Comparator. If provided, it uses Comparators' compare() method to compare Objects stored in Collection to compare with each other; in case of no explicit Comparator, Comparable interfaces' compareTo() method is used to compare objects from each other. If objects stored in ArrayList don't implement Comparable, they can not be sorted using the Collections.sort() the method in Java.

Thursday, March 21, 2024

What is final modifier in Java? fina class, method, and variable example

Hello Java Programmers, the final variable is a very important concept in Java. It's a modifier that you can apply on variables, methods, and classes and when you apply the final modifier it can make variables immutable, prevent the method from overriding in subclasses, means no polymorphism, and when you make a class final in Java it cannot be extended anymore, taking out Inheritance from the picture. There are both pros and cons of using the final modifier in Java and that's why it's very very important for a Java developer to have an in-depth knowledge of the final modifier in Java and that's what you will learn in this article. 

Wednesday, March 20, 2024

Best way to Convert Integer to String in Java with Example

Integer to String conversion in Java
There are many ways to convert an Integer to String in Java e.g. by using Integer.toString(int) or by using String.valueOf(int), or by using new Integer(int).toString(), or by using String.format() method, or by using DecimalFormat, String concatenation, or by using StringBuilder and StringBuffer, etc but which one is the best? Some of them we have already seen on my earlier posts e.g. how to convert int to String and converting double to String.  Well, we are going to find the best way to convert Integer to String in Java in this post. 

How to create a ZIP File in Java? ZipEntry and ZipOutputStream Compression Example

Since compressing and archiving old log files is an essential housekeeping job in any Java application environment, a Java programmer should know how to compress files in .zip format and then how to read them programmatically if required. The JDK provides full support to create and read ZIP files in Java. There is a separate package java.util.zip to hold all classes related to zipping and unzipping files and streams. In this series of articles, you will learn how to use those classes like ZipFile, ZipEntry, ZipInputStream, and ZipOutputStream, etc. 

How to add JAR files in Eclipse Project's Build path? Example

In this Java Eclipse tutorial, I will show you two ways to add external JAR files in Eclipse Java projects. Many times we need to use external JAR files in our Java application for different needs like for general purposes you may use Google Guava or Apache Commons. If you are using Spring or Hibernate framework then you need their JAR files as well. In order to use any third-party library or framework, we have to add their JAR files in the classpath, to compile and run our Java programs. Since Eclipse is the most popular IDE for developing Java applications, it's important to know how you can add external JARs into your Java project's build path. 

Tuesday, March 19, 2024

5 Tips to Fix Exception in thread "main" java.lang.NoClassDefFoundError in Java, Examples

NoClassDefFoundError in Java
The Exception in thread "main" java.lang.NoClassDefFoundError is a common error in Java that occurs if a ClassLoader cannot find a particular class in the classpath while trying to load it. The Exception in thread "main" suggests that this error has occurred in the main thread, the thread which is responsible for running the Java application. This error can occur to any thread, but if it happens in the main thread, then your program will crash because Java program runs only until main() thread is running or any other non-daemon thread is running. As per Javadoc, NoClassDefFoundError can be thrown during the linking or loading of the class file

14 Enum Interview Questions and Answers for 1 to 2 Years Experienced Java Programmers

Hello Java developers, Java Enum is one of the best features of Java Programming language which was first added on the JDK 1.5 Tiger release along with Generics, Autoboxing, and varargs. While the concept of Enum was also present in C Programming language, in Java its much more powerful. Enum in Java represents a fixed number of well-known things like the number of days in a week, the number of months in the calendar year, etc. Unlike C and C++ Enum in Java are much powerful and they are not an integer constant, Instead, Enum is a Type in Java like class or interface which provides compile-time type safety. In this Java Enum tutorial, we will see a couple of important points about Java Enum which help to understand Enum better and get the most out of it. 

How to convert String to char in Java? Example Tutorial

Hello Java Programmers, if you are wondering how to convert a String to char value in Java then you have come to the right place. Earlier, we have seen how to convert a character to String in Java and today we'll learn opposite i.e. converting String object to a character value. You know the difference right? String is an Object while char is a primitive value. So here we are also going to convert a Java object into a primitive value. By the way, this conversion only make sense if String is a single character. For example, you have a String with value "s", how do you convert that to a char 's' in Java (FYI, string literal are quoted inside double quotes in Java, like "c" and character literals are quoted inside single quotes e.g. 'c' in J)? Well, if you know, Java String is made of a character array and java.lang.String class provides a method toCharArray() to retrieve that character array.

How to get current TimeStamp value in Java? Example

Do you know that Java developer Google (now ask ChatGPT) for even the simplest of things? Yes, that's true and I can say it because even I do that :-). Finding the current timestamp value is one of them. In UNIX you can just use the date command to get the current date and time but how do you get that in your Java program? Well, you can get the current timestamp in Java by using the Date and Timestamp class of JDK. Note, there is "s" instead of "S" in Timestamp. Since Date in Java contains both date and time, it can be used as a Timestamp value as well but when you print Date, it shows time in the local timezone

5 Difference between String, StringBuffer, and StringBuilder in Java

Difference between String, StringBuffer, and StringBuilder is probably one of the oldest yet most popular interview question in the history of Java developer interviews. I have personally asked this question almost 4 to 5 times. You will find this question on any list of Java interview questions. I have also included this in my earlier list of core Java questions and Java String questions. If you are preparing for Java interviews, those are handy resources. Now coming back to the answer, although all three classes StringBuffer, StringBuilder, and String are used for representing text data in Java there are some significant differences between them. One of the most notable differences between StringBuilder, StringBuffer, and String in Java is that both StringBuffer and StringBuilder are Mutable classes but String is Immutable in Java. What this means is, you can add, remove or replace characters from StringBuffer and StringBuilder object but any change on the String object like converting uppercase to lowercase or appending a new character using String concatenation will always result in a new String object. 

Monday, March 18, 2024

Difference between include() and forward() methods of RequestDispatcher in Servlert

What is the difference between include and forward methods of RequestDispatcher interface is one of the frequently asked Servlet questions from Java EE interviews and we'll see how you can answer this question on your interview. You get the RequestDispatcher reference either from ServletContext or ServletRequest interface and even though both include() and forward() method allow a Servlet to interact with another servlet, the main difference between include() and forward is that the include() method is used to load the contents of the specified resource (could be a Servlet, JSP, or static resource like HTML files) directly into the Servlet's response, as if it is part of the calling Servlet. 

Base64 Encoding and Decoding Examples in Java 8 and Before

Though, there are a couple of ways to Base64 encode a String in Java e.g. by using Java 6's javax.xml.bind.DatatypeConverter#printBase64Binary(byte[]) or by using Apache Commons Codec's Base64.encodeBase64(byte[) and Base64.decodeBase64(byte[])as shown here, or the infamous Sun's internal base64 encoder and decoder, sun.misc.BASE64Encoder().encode() and sun.misc.BASE64Decoder().decode(), there was no standard way in JDK API itself. That was one of the few missing item (another one is about joining string) which is addressed in Java 8.

How Java achieves platform independence? Answer

Java is a platform-independent language How?
Java a programming language provides platform independence, what does it mean? It means the same Java program can be run on any platform or operating system like Windows, Linux or Solaris without any change. This is a great benefit for someone coming from a platform-dependent programming language like C or C++ whose code needs to be ported for every single platform because they use native libraries, which differ in every platform. Now the question comes how Java achieves platform independence, what makes Java programs running on every platform without any change

10 Difference between Struts 1.x and Struts 2.x framework in Java

What is the difference between Struts 1 and Struts 2 is one of the popular Struts interview questions from Java JEE interviews? If you have mentioned Struts in your CV then you can expect this question on the telephonic or face-to-face round of interviews. Struts 1.x was a very popular MVC framework for Java web applications in the last decade, but when Spring MVC comes it lost a lot of its sheen. Spring framework's dependency injection and inversion of control feature, along with several other template-based features makes it the de-facto framework for developing Java applications. 

Saturday, March 16, 2024

When You should Not use Microservice Architecture? (Answer)

 Hello guys, recently while giving interview in one of the big US based Investment bank I was asked about, what are the scenarios when you shouldn't be using Microservices architecture? When I hear this question I was big surprised not because I didn't know the answer but because most of the people ask about using Microservice architecture and this was the first time I am hearing otherwise. To be honest, it's a good question because most of the candidate will prepare about benefirst about Microservice architecture and they may not know when it's not appropriate to use it. Since I have worked in low latency high frequency application, I knew that multiple process means more latency so clearly a Microservices is a big no if you are working on a high frequency low latency application. In those cases you want to do all your calculations inside a single thread or process without even swapping memory to avoid delay. Hence the first answer to this question was, you shouldn't be using Microservice in a low latency application. 

Friday, March 8, 2024

Difference between static and instance member variables in Java? Answer Example

Hello Java Programmers, In the last article, I had explained some key differences between static and nonstatic methods in Java, and in this part, I'll explain the difference between static and nonstatic member variables in Java. The concept of static remains the same, that doesn't change with method or member variables but there are still some subtle details, which every Java programmer should know and understand. As with static methods, a static member variable belongs to a class and a non-static member variable belongs to an instance. 

Tuesday, March 5, 2024

Top 10 Bash + UNIX Interview Questions and Answers

Hello guys, bash is a wonderful command line tool which can be used to automate trivial task. I have written cleanup scripts, start and stop scripts and even schedule jobs using bash and crontab command. Since bash is super useful for anyone running their application in Linux its also a key skill for developers and IT support. Because of that you will always find a couple of questions related to bash on developer interviews. In the past, I have shared Linux interview questions and today I am going to share a couple of bash interview questions which is asked to me throughout the years. If you have worked in bash shell then you can easily answer these questions but if you find it trouble, maybe it's time to revise your bash concepts.