What is difference between HashMap and HashSet
HashSet : HashSet does not allow duplicate values. It provides add method rather put method. You also use its contain method to check whether the object is already available in HashSet. HashSet can be used where you want to maintain a unique list.
HashMap : It allows null for both key and value. It is unsynchronized. So come up with better performance.
How to get a list of resources from a directory in Java classpath
You can use Reflections library
for doing this. Reflections is a open source Java library. It scans Java classpath and indexes it with metadata. This library allows you to query the classpath at runtime and can be very handy for many run-time reflection code needs.
For binary operands :
If one of the operands is double, the other operand is converted to double
Else If one of the operands is float, the other operand is converted to float
Else If one of the operands is long, the other operand is converted to long
Else both the operands are converted to int
In real applications, how do you know that you have a memory leak
If you profile your application, you can notice a graph like a saw tooth. Here is how you can determine this with the help of jconsole for the above bad key class example. All you have to do is while your MemoryLeak is running, get the Java process id by typing.
C:>jps
5808 Jps
4568 MemoryLeak
3860 Main
Now, open up the jconsole as shown below on a command line
C:>jconsole 4568
Which one will you use Statement or PreparedStatement? Or Which one to use when (Statement/PreparedStatement)? Compare PreparedStatement vs Statement.
By Java API definitions: Statement is a object used for executing a static SQL statement and returning the results it produces. PreparedStatement is a SQL statement which is precompiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times. There are few advantages of using PreparedStatements over Statements
- Since its pre-compiled, Executing the same query multiple times in loop, binding different parameter values each time is faster. (What does pre-compiled statement means? The prepared statement(pre-compiled) concept is not specific to Java, it is a database concept. Statement precompiling means: when you execute a SQL query, database server will prepare a execution plan before executing the actual query, this execution plan will be cached at database server for further execution.)
- In PreparedStatement the setDate()/setString() methods can be used to escape dates and strings properly, in a database-independent way.
- SQL injection attacks on a system are virtually impossible when using PreparedStatements.
How can we include Jar within a Jar in java classpath
There is no easy way to do this in current java versions.
There are 2 alternatives to deal with this problem using third party libraries.
Use Jar class loader library
The “http://www.jdotsoft.com/JarClassLoader.php”JarClassLoader library provides you the feature of loading resources from a top JAR and from JARs inside the top JAR.
Explode and combine into one jar
Instead of trying to bundle jar file inside jar you can explode all required jars and re-bundle them as one jar using following two libraries
- The first is “http://one-jar.sourceforge.net/”One-Jar, which uses a special classloader to allow the nesting of jars.
- The second is “http://classworlds.codehaus.org/uberjar.html”UberJar, (or “http://maven.apache.org/plugins/maven-shade-plugin/”Maven Shade Plugin), which explodes the included libraries and puts all the classes in the top-level jar.
What are volatile variables
It indicates that these variables can be modified asynchronously
Why Java uses Classpath parameter or environment variables?
In a Java class import statements are used to access other classes. You also do a wild card import like org.fromdev.* on your java file.
In such cases, It will become very impractical/slow for the Java Virtual Machine to search for classes in every file/folder on a machine, therefore you can provide the Java Virtual Machine with a list of places to look. This is done by putting folder and jar files on your classpath.
Environment variables in general are a set of dynamic name value pair that can be used by processes to decide the behavior based on a system. These are supported by all modern operating systems.
The CLASSPATH variable is a Java way to tell the possible locations of user classes or jar files for a Java application. Since each user/computer may choose to have classes in different location its best to have custom locations configured in Classpath variable.
In case you have not set the classpath environment variable, the default value is used as the “.” (current directory). That means, the current directory is searched.
How many types of relationship exist in database designing
There are three major relationship models:-
- One-to-one
- One-to-many
- Many-to-many
How to read a file from CLASSPATH in java
This can be done in two simple ways
Using ClassLoader.getResourceAsStream
This method can be used to load any file from CLASSPATH
InputStream in =this.getClass().getClassLoader().getResourceAsStream(“MyFile.txt”);
Using Class.getResourceAsStream
This method can be used to load files using relative path to the package of the class
InputStream in =this.getClass().getResourceAsStream(“SomeTextFile.txt”);
This method can also be used to load any files from CLASSPATH by prefixing a “/”