Why character array is better than String for storing a password in Java was a recent question asked to one of my friends in a java interview. he was interviewing for a Technical lead position and has over 6 years of experience. Both Character array and String can be used to store text data but choosing one over the other is a difficult question if you haven't faced the situation already. But as my friend said any question related to String must have a clue on a special property of Strings like immutability and he used that to convince the interviewer. here we will explore some reasons why should you use char[] for storing passwords than String.
Thursday, June 12, 2025
Difference between Stack and Heap memory in Java? Example
The difference between stack and heap memory is a common programming question asked by beginners learning Java or any other programming language. Stack and heap memory are two terms programmers start hearing once they started programming but without any clear and definite explanation. Lack of knowledge of what is a heap in Java and what is stack memory in Java results in misconceptions related to stack and heap. To add to this confusion, a stack is also a data structure that is used to store elements in LIFO(Last In First Out) order and is available in Java API as java.util.Stack.
Labels:
core java
,
core java interview question
,
JVM Internals
What is load-on-startup servlet element in web.xml with Example?
load-on-startup is an element that appears inside <servlet> tag in web.xml.4 years back load-on-startup was a very popular servlet interview question because not many Java J2EE developer was familiar with this element and how load-on-startup works inside a servlet container like tomcat or WebSphere. In this J2EE Tutorial, we will see what is a load on startup, how to use the load-on-startup element, and what are different values we can configure for load-On-Startup inside web.xml.
Difference between SendRedirect() and Forward() in JSP Servlet? Example
The difference between SendRedirect and forward is one of the classical interview questions asked during a java web developer interview. This is not just applicable for servlet but also for JSP in which we can use forward action or call sendRedirect() method from scriptlet. Before examining the difference between forward and SendRedirect let’s see what send Redirect method and the forward method does.
SendRedirect ():
This method is declared in HttpServletResponse Interface.
Signature: void sendRedirect(String url)
Difference between throw and throws in Exception handling - Java Example
Difference between throw and throws keyword on Exception handling in Java is a popular core java interview question. Exception handling being an important part of Java programming language, complete knowledge of all keywords related to exception handling e.g. try, catch, finally, throw and throws is important. Main difference between throw and throws is in there usage and functionality. where throws is used in method signature to declare Exception possibly thrown by any method, throw is actually used to throw Exception in Java code, here is an example of both throw and throws keyword which makes it easy to understand difference between them.
Labels:
core java
,
core java interview question
Difference between Struts 1 and Struts 2 Web Development Framework
I had worked previously on Struts 1 but never touched Struts 2, especially
since Spring MVC was there to take the leading role. Recently one of my friends
ask me to help with Struts2, which leads me to look on the Struts2 framework from
start. First thing I wanted to find out differences between Struts 1 and Struts
2 framework, because in my experience, if you have worked in the previous version
looking differences between two versions of Struts can quickly help to find,
what changes and What are the new features, concepts and improvement is offered
by Struts 2. Also the difference between
Struts 1 and Struts 2 is a good candidate to include in my list of Struts interview questions
for quick revision.
Labels:
core java interview question
,
J2EE
,
jsp-servlet
,
servlet interview questions
,
struts
10 points on finalize method in Java – Tutorial Example
finalize method in java is a special method much like the main method in java. finalize() is called before the Garbage collector reclaims the Object, its last chance for any object to perform cleanup activity i.e. releasing any system resources held, closing the connection if open etc. The main issue with finalize() method in Java is it's not guaranteed by JLS that it will be called by Garbage collector or exactly when it will be called, for example, an object may wait indefinitely after becoming eligible for garbage collection and before its finalize() method gets called. similarly even after finalize gets called it's not guaranteed it will be immediately collected.
Labels:
core java
,
core java interview question
What is Static and Dynamic binding in Java with Example
Static and dynamic binding in Java is two important concepts that Java programmer should be aware of. this is directly related to the execution of code. If you have more than one method of the same name (method overriding) or two variables of the same name in the same class hierarchy it gets tricky to find out which one is used during runtime as a result of their reference in code. This problem is resolved using static and dynamic binding in Java. For those who are not familiar with the binding operation, its process is used to a link which method or variable to be called as a result of their reference in code.
Labels:
core java
,
core java interview question
Wednesday, June 11, 2025
3 Examples to Concatenate String in Java
String concatenation is the process of joining two or more small String to create a big String. For example, you can create a full name by concatenating first and last name of a person. Java provides multiple ways to concatenate String, but the easiest of them is by using + operator. It is one of the magical operators in Java, though Java doesn't support operator overloading, it has made an exception in the case of String and + operators. Plus operator is a binary operator and primarily used to add two numbers if both operands are integer but it can also used to concatenate String if either both or first operand is String. For example, "Java" + "Programming" will produce "JavaProgramming".
Labels:
core java
,
data structure and algorithm
,
String
3 Ways to Convert an Array to ArrayList in Java? Example
How to convert an Array to ArrayList in Java
Have you encountered any situation where you quickly wanted to convert your array to ArrayList or ArrayList to array in Java? I have faced many such situations that motivate me to write these quick Java tips about converting an array to ArrayList and ArrayList to array in Java. Both array and ArrayList are quite common and every Java developer is familiar with this. Former is used to store objects and primitive types while later can only hold objects. The array is part of standard Java fundamental data structure while ArrayList is part of the collection framework in Java.
Have you encountered any situation where you quickly wanted to convert your array to ArrayList or ArrayList to array in Java? I have faced many such situations that motivate me to write these quick Java tips about converting an array to ArrayList and ArrayList to array in Java. Both array and ArrayList are quite common and every Java developer is familiar with this. Former is used to store objects and primitive types while later can only hold objects. The array is part of standard Java fundamental data structure while ArrayList is part of the collection framework in Java.
How to Replace Line Breaks , New Lines From String in Java - Windows, Mac or Linux Example
We often need to replace line terminator characters, also known as line breaks e.g. new line \n and carriage return \r with different characters. One of the common case is to replace all line breaks with empty space in order to concatenate multiple lines into one long line of String. In order to replace line breaks, you must know line terminator or new line characters for that platform. For example, In Windows operating system e.g. Windows 7 and 8, lines are terminated by \n\r also known as CR+LF, while in Unix environment e.g. Linux or Solaris line terminator is \n, known as line feed LF. You can also get line terminator pragmatically by using Java system property line.terminator.
Labels:
coding
,
core java
,
data structure and algorithm
,
programming
,
String
How to Convert Byte Array to InputStream and OutputStream in Java? Example
Are you stuck with your coding because you have a byte array and the next method in the chain needs an InputStream? don't worry Java has a solution for that, You can use ByteArrayInputStream to convert byte array to InputStream in Java. This class takes a byte array as the source and since it's a subclass of InputStream, you can easily pass this to any method, which accepts InputStream as a parameter. Though most of the API like JDBC and File API allows you to read directly from InputStream because this allows you to process an arbitrary content with limited heap space. You should take advantage of this and directly read from InputStream instead of getting byte array and then converting them back to InputStream.
Labels:
Array
,
core java
,
data structure and algorithm
,
java IO tutorial
,
programming
How to Convert InputStream to Byte Array in Java - 2 Examples
Sometimes we need to convert InputStream to byte array in Java, or you can say reading InputStream as a byte array, In order to pass output to a method that accepts byte array rather than InputStream. One popular example of this, I have seen is an older version of Apache commons-codec, while converting byte array to the hex string. Though, a later version of the same library does provide an overloaded method, to accept InputStream. Java File API provides excellent support to read files like image, text as InputStream in Java program, but as I said, sometimes you need String or byte array, instead of InputStream .
Labels:
Array
,
core java
,
data structure and algorithm
,
java IO tutorial
,
programming
How to Reverse Array in Place in Java? Solution With Explanation
Reversing an array sounds pretty easy, isn't it? It does sound like that, because all you need to do is create an array of the same size, iterate through the original array from end to start, and populate your new array. Boom!!, you have got an array that has elements in reverse order of the original array, but the problem is you have used an additional array here, which makes space complexity of your solution O(n). You cannot use this solution if the array is big e.g. an array of 10 million orders and you don't have enough heap space available.
Labels:
Array
,
data structure and algorithm
How to Convert List of Integers to Int Array in Java - Example
So, you have a List of Integers and you want to convert them into int array? Yes, you read it write, not on Integer array but int array. Though in most practical purpose, Integer array can be used in place of int[] because of autoboxing in Java, you still need an int[] if your method accepts it. In Java, you can not typecast an Integer array into an int array. Many Java programmer think about toArray() method from java.util.List to convert a List into Array, but unfortunately, toArray() is useless most of the time. It doesn't allow you to convert List into primitive arrays.
Labels:
Array
,
coding
,
core java
,
data structure and algorithm
,
java collection tutorial
,
programming
Subscribe to:
Posts
(
Atom
)