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

Multithreading in Java

9:54 PM
Multithreading in Java Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such program is called a thread. So, threads are light-weight processes within a process. Threads can be created by using two mechanisms : 1. Extending the Thread class 2. Implementing the Runnable Interface Thread creation by extending the Thread class We create a class that extends the java.lang.Thread class. This class overrides the run() method available in the Thread class. A thread begins its life inside run() method. We create an object of our new class and call start() method to start the execution of a thread. Start() invokes the run() method on the Thread object. // Java code for thread creation by extending  // the Thread class  class MultithreadingDemo extends Thread  {      public void run()      {          try ...

BufferedReader vs Scanner in Java

4:27 AM
BufferedReader vs Scanner in Java Here is the 5 key differences between the Scanner and BufferedReader class of Java API: 1. Scanner is a much more powerful utility than BufferedReader. It can parse the user input and read int, short, byte, float, long and double apart from String. On the other hand  BufferedReader  can only  read String in Java . 2. BuffredReader has significantly large buffer (8KB) than Scanner (1KB), which means if you are reading long String from file, you should use BufferedReader but for short input and input other than String, you can use Scanner class. 3. BufferedReader is older than Scanner. It's present in Java from JDK 1.1 onward but Scanner is only introduced in JDK 1.5 release. 4. Scanner uses  regular expression  to read and parse text input. It can accept custom delimiter and parse text into primitive data type e.g. int, long, short, float or double using  nextInt() ,  nextLong() ,  nextShort() ,  nextFloat(...

Difference between final vs finally and finalize in Java?

4:26 AM
Difference between final vs finally vs finalize in Java The difference given in first paragraph about final, finally and finalize() method is sufficient from an interview point of view but it's better to know some more differences if you want to impress the interviewer or want to prepare better for follow-up questions. Let's see couple of more points to learn this concept better. 1) First and foremost, even though their name sounds similar, final, finally and finalize() are completely different from each other. There is no similarity in their function, whatever is, its only on their names. 2) The final keyword can be used with a class, method or variable in Java. A final class is not extensible in Java, a final method cannot be overridden, and a final variable cannot be changed. You can make a class Immutable in Java by using the final keyword. Similarly you can prevent overriding by using the final keyword with method and you can declare static final method to represen...

Difference between early (static) binding vs late (dynamic) binding in Java

4:24 AM
In  order to understand the difference between static and dynamic binding in Java, it's important to first learn what is binding? Binding means the link between reference and actual code e.g. when you refer a variable it's bonded to the code where it is defined, similarly when you call a method, it's linked to the code where a method is defined. There are two types of method binding in Java, static binding and dynamic binding. When a method is called in Java it's bonded to the actual code either at compile time or runtime, when the program is actually started and objects are created. As the name suggest, static binding is more of static nature hence it occurs at compile time i.e. your code knows which method to call once you compiled your Java source file into a class file. Since it happens early in program's life cycle it is also known as early binding in Java. On the other hand, dynamic binding occurs at runtime, when JVM starts your program. This time which me...

How to sort ArrayList ascending descending order

4:07 AM
How to sort ArrayList ascending descending order  Sorting ArrayList in Java is not difficult, by using Collections.sort() method you can sort ArrayList in ascending and descending order in Java. Collections.sort() method optionally accept a Comparator and if provided it uses Comparator's compare method to compare Objects stored in Collection to compare with each other, in case of no explicit Comparator, Comparable interface's compareTo() method is used to compare objects from each other. If object's stored in ArrayList doesn't implements Comparable than they can not be sorted using Collections.sort() method in Java. Sorting ArrayList in Java – Code Example ArrayList Sorting Example in Java - Ascending Descending Order Code Here is a complete code example of How to sort ArrayList in Java, In this Sorting we have used Comparable method of String for sorting String on there natural order, You can also use Comparator in place of Comparable to sort String on any o...

.