JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label core java. Show all posts
Showing posts with label core java. Show all posts

difference and Similarities between Hashtable and HashMap in Java

4:02 AM
Similarities between Hashtable and HashMap in Java 1) Both Hashtable and HashMap implements java.util.Map interface. 2) Hashtable and HashMap both are a hash based collection and works on the principle of hashing. 3) Hashtable and HashMap both provide constant time performance for put and get method if objects are distributed uniformly across bucket. 4) From JDK 4 both Hashtable and HashMap are part of Java collection framework. Difference between Hashtable and HashMap in Java 1) First and most significantly different between Hashtable and HashMap are that HashMap is not thread-safe  while Hashtable is a thread-safe collection. 2) The second important difference between Hashtable and HashMap is performance since HashMap is not synchronized it perform better than Hashtable. 3) The third difference on Hashtable vs HashMap is that Hashtable is obsolete class and you should be using ConcurrentHashMap in place of Hashtable in Java.

Difference between HashSet and HashMap in Java

4:00 AM
Similarities on HashMap and HashSet in Java 1) Both HashMap and HashSet are a hash based collection in Java. 2) Both HashMap and HashSet are not synchronized and can not be shared between multiple threads. 3) Iterator returned by HashMap's keySet() and HashSet are fail-fast and they throw ConcurrentModificationException if they detect any structural change in Collection. 4) Both HashMap and HashSet provided constant time performance for basic operations like put(), get() etc. 5) Both HashSet and HashMap allows null values. Differences between HashSet and HashMap in Java 1) The first and most significant difference between HashMap and HashSet is that HashMap is an implementation of Map interface while HashSet is an implementation of Set interface, which means HashMap is a key value based data-structure and HashSet guarantees uniqueness by not allowing duplicates.In reality, HashSet is a wrapper around HashMap in Java, if you look at the code of add(E e) method

What is difference between Overloading and Overriding in Java

3:48 AM
Overloading vs Overriding in Java 1) First and major difference between Overloading and Overriding is that former occur during compile time while later occur during runtime. 2) Second difference between Overloading and Overriding is that, you can overload method in same class but you can only override method in sub class. 3) Third difference is that you can overload static method in Java but you can not override static method in Java. In fact when you declare same method in Sub Class it's known as method hiding because it hide super class method instead of overriding it. 4) Overloaded methods are bonded using static binding and Type of reference variable is used, while Overridden method are bonded using dynamic bonding based upon actual Object. 5) Rules of Overloading and Overriding is different in Java. In order to overload a method you need to change its method signature but that is not required for overriding any method in Java. 6) Another difference between meth

ArrayList vs Vector in Java

3:44 AM
Common property of Vector and ArrayList in Java 1) Bother Vector and ArrayList are derived from AbstractList and implements List interface, which means both of them are ordered collection and allows duplicates. 2) Another similarity between Vector vs ArrayList is that both are index based Collection and you can use get(index) method to retrieve objects from Vector and ArrayList. Vector vs ArrayList in Java 1) First and most common difference between Vector vs ArrayList is that Vector is synchronized and thread-safe while ArrayList is neither Synchronized nor thread-safe. Now, What does that mean? It means if multiple thread try to access Vector same time they can do that without compromising Vector's internal state. Same is not true in case of ArrayList as methods like add(), remove() or get() is not synchronized. 2) Second major difference on Vector vs ArrayList is Speed, which is directly related to previous difference. Since Vector is synchronized, its slow and Arra

Difference between PreparedStatement and Statement in Java

3:40 AM
JDBC API provides three types of Statement for wrapping an SQL query and sending for execution to the database, they are aptly named as Statement, PreparedStatement, and CallableStatement. First one, Statement is used to execute normal SQL queries e.g. select count(*) from Courses. You can also use it to execute DDL, DML and DCL SQL statements. The second one, PreparedStatement is specialized to execute parameterized queries e.g. select * from Courses where courseId=?, you can execute this SQL multiple times by just changing the course parameters. They are compiled and cached at database end, hence quite fast for repeated execution. The third member of this family is CallableStatement, which is there to execute or call stored procedures stored in the database. Difference between Statement vs PreparedStatement Following are some key differences between these two classes, they are based upon syntax, purpose, performance, security, and capabilities.   Purpose Pre

Can you make an abstract class/method final in Java?

3:34 AM
No, you cannot make an abstract class or method final in Java because abstract and final are the mutual exclusive concept . An abstract class is incomplete and can only be instantiated by extending a concrete class and implementing all abstract methods, while a final class is considered as complete and cannot be extended further. This means when you make an abstract class final, it cannot be extended hence it cannot be used and that's why Java compiler throws a compile time error when you try to make an abstract class final in Java Can we make an Abstract method final in Java Now that, you know there is no way you can make an  abstract class final  in Java, let's try to make an abstract method final in Java. As I said above, this time also Java compiler should complain because both  final  and  abstract  are mutual exclusive keywords: abstract class ImAbstract{ public final abstract void anAbstractMethod (); } class ImConcrete extends ImAbstract{ @Overrid

Difference between Heap and Stack Memory in Java JVM ?

3:31 AM
Difference between Heap and Stack Memory in Java JVM Heap memory is shared by all threads of Java application but Stack memory is local to each thread.  Objects are created in heap memory but method frames are stored in Stack memory, and size of heap space is much bigger than the small size of Stack in Java Stack vs Heap in Java As I told, both Stack and Heap space are part of JVM but they are used for different purpose, let's see some more points to understand the difference between stack and heap memory better. 1) Size One of the significant difference between Stack and heap comes from their size. Heap space in Java is much bigger than the Stack memory. This is partly due to the fact that whenever a new thread is created in JVM, a separate stack memory is allocated to them. 2) Resizing JVM allows you to resize both heap and stack in Java, though you need to use different JVM flags for that. You can use -Xms and -Xmx to specify the starting and maximum he

Difference between a List and ArrayList Reference Variable in Java?

3:25 AM
what is the difference between  List  and  ArrayList ?    Why not just store the ArrayList object in the ArrayList variable just like we do for String, int, and other data types.? The main difference between List and ArrayList is that List is an interface while ArrayList is a class it implements the List interface, which also means that ArrayList is a subtype of List interface.In Java or any object oriented language, super type of variable can store an object of subtype. This is known as Polymorphism because any virtual method will be executed from subclass only, even though they were called from super type. Why store ArrayList object on List variable? You might have seen something like this: List<Movie> listOfMovies = new ArrayList<Movie> () here we are using a List as a type of variable to store an object of ArrayList class, created using new() operator. This is known as programming for the interfaces. In fact, everywhere you need to declare a refere

Java - Multithreading

9:58 PM
Java is a  multi-threaded programming language  which means we can develop multi-threaded program using Java. A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs. By definition, multitasking is when multiple processes share common processing resources such as a CPU. Multi-threading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. Each of the threads can run in parallel. The OS divides processing time not only among different applications, but also among each thread within an application. Multi-threading enables you to write in a way where multiple activities can proceed concurrently in the same program. Life Cycle of a Thread A thread goes through various stages in its life cycle. For example, a thread i

java_sending_email

4:01 AM
To send an e-mail using your Java Application is simple enough but to start with you should have  JavaMail API  and  Java Activation Framework (JAF) installed on your machine. You can download latest version of  JavaMail (Version 1.2)  from Java's standard website. You can download latest version of  JAF (Version 1.1.1)  from Java's standard website. Download and unzip these files, in the newly created top level directories you will find a number of jar files for both the applications. You need to add  mail.jar  and  activation.jar  files in your CLASSPATH. Send a Simple E-mail Here is an example to send a simple e-mail from your machine. It is assumed that your  localhost  is connected to the Internet and capable enough to send an e-mail. Example // File Name SendEmail.java import java . util .*; import javax . mail .*; import javax . mail . internet .*; import javax . activation .*; public class SendEmail { public static void main ( St

.