There is hardly any Java Interview, where no questions are asked from String, and Why String is Immutable in Java is I think most popular Java String question. This question is also asked as Why String class is made final in Java or simply, Why String is final. In order to answer these questions, Java programmer must have a solid understanding of how String works, what are special features of this class, internal structure and implementation of String and some key fundamentals. The String class is a God class in Java, It has got special features which is not available to other classes like String literals are stored in string pool.
Learn Java and Programming through articles, code examples, and tutorials for developers of all levels.
Difference between Array vs ArrayList in Java
What is the difference between Array and ArrayList is quite a common question among beginners especially those who started coding in C and C++ and prefer to use Array? Both Array and Array List are used to store elements, which can be either primitive or objects in case of Array and only objects in case of ArrayList in Java. The main difference between Array vs ArrayList in Java is the static nature of the Array and the dynamic nature of ArrayList. Once created you can not change the size of Array but ArrayList can re-size itself when needed.
Difference between HashMap and ConcurrentHashMap in Java? Example
HashMap vs ConcurrentHashMap in Java
What is the difference between an HashMap and ConcurrentHashMap in Java is one of the common interview questions and knowing the answer is not just important for interviews but also for writing robust and high performance Java cocd. ConcurrentHashMap in Java is introduced as an alternative of Hashtable in Java, which is a synchronized collection class, that makes the main difference between HashMap and ConcurrentHashMap which is one is non-synchronized , non-thread safe and not for use in Concurrent multi-threaded environment while ConcurrentHashMap is a thread-safe collection and intended to be used as primary Map implementation especially for multi-threaded and Concurrent environment.
What is the difference between an HashMap and ConcurrentHashMap in Java is one of the common interview questions and knowing the answer is not just important for interviews but also for writing robust and high performance Java cocd. ConcurrentHashMap in Java is introduced as an alternative of Hashtable in Java, which is a synchronized collection class, that makes the main difference between HashMap and ConcurrentHashMap which is one is non-synchronized , non-thread safe and not for use in Concurrent multi-threaded environment while ConcurrentHashMap is a thread-safe collection and intended to be used as primary Map implementation especially for multi-threaded and Concurrent environment.
How to iterate over an Array in Java using foreach loop Example Tutorial
Though there are many ways to loop over Array in Java, including classical for loop, while loop with length counter, nothing matches the elegance of Java 1.5 foreach loop, which makes iteration super easy and super cool. When we need to iterate through each element of an array and have to perform some operation on them e.g. filtering, transformation, or simply printing, the foreach loop comes to its full glory. There is not much difference between traditional for loop and Java 1.5 foreach loop, except that the former has counter and condition, which is checked after each iteration.
Difference between List and Set in Java [Answered]
List vs Set in Java
The main difference between List and Set is that List is an ordered Collection while Set is an unordered collection. Java collection framework offers several collection classes for various needs but all collections can be divided into broadly three categories: List, Set, and Map. All List, Set, and Map are defined as interfaces and then you have several implementations like ArrayList and Vector are the popular implementation of List interface, while HashSet is a popular implementation of the Set interface.
2 Ways to Remove Elements/Objects From ArrayList in Java [Example]
There are two ways to remove objects from ArrayList in Java, first, by using the remove() method, and second by using Iterator. ArrayList provides overloaded remove() method, one accepts the index of the object to be removed i.e. remove(int index), and the other accept objects to be removed, i.e. remove(Object obj). The rule of thumb is, If you know the index of the object, then use the first method which accept index, otherwise use the second method. By the way, you must remember to use ArrayList remove methods, only when you are not iterating over ArrayList if you are iterating then use Iterator.remove() method, failing to do so may result in ConcurrentModificationException in Java.
How to check if File is Empty in Java? Example Tutorial
One of the readers of my blog Javarevisited emailed me yesterday about how to check if a file is empty in Java and that's why I am writing this post to show that using an example. The common approach for checking if a file is empty or not is to first check if the file exists or not and then check if it contains any content, but Java has done that hard work for you. Well, it's pretty easy to check emptiness for a file in Java by using the length() method of the java.io.File class. This method returns zero if the file is empty, but the good thing is it also returns zero if the file doesn't exist. This means you don't need to check if the file exists or not.
How to Convert Stream to ArrayList in Java 8 - Collectors.toCollection() Example
You can use Collectors.toList(), toSet(), and toMap() to get all elements of Stream into any Collection like List, Set or Map, but if you want to get a particular collection e.g. ArrayList, then you need to use Collectors.toCollection(ArrayList::new) method. This method first creates an ArrayList using method reference and then adds all elements of Stream into the ArrayList. It's very useful if you have a long list of String and you want to create a smaller list containing only String starting with the letter "b" like "Bluehost".
3 Examples to Loop over a List in Java - ArrayList, LinkedList or Vector
3 ways to Loop through a List in Java
There are multiple ways to traverse or loop through a List in Java e.g. by using an Iterator, by using an enhanced for loop of Java 5, and not the forEach() method of Java 8. Given a List is an index-based collection if you know the index you can retrieve an object from a List and because of this, you can also use a traditional for loop which keeps count for iterating a List. Now the question is whether should you use the Iterator or enhanced for loop, or the forEach() method of Java 8 for looping over List in Java.
RegularEnumSet vs JumboEnumSet in Java
Hello guys, the difference between RegularEnumSet and JumboEnumSet in Java was asked in a recent Java interview with one of my friends, unfortunately, he hasn't explored this topic well and couldn't answer this question precisely, but he made sure to learn about EnumSet after that. When he discussed this topic with me, I really liked it because, despite the usefulness of EnumSet and its fast implementation, not many developers know about it, despite being mentioned in Java's classic Effective Java. This makes me write this post, where we will mainly discuss a couple of differences between RegularEnumSet and JumboEnumSet in Java, but we will also touch base upon some of the important properties of EnumSet.
What is fail safe and fail fast Iterator in Java?
Java Collections supports two types of Iterator, fail safe and fail fast. The main difference between a fail fast and fail safe Iterator is whether or not the underlying collection can be modified while its begin iteration. Fail safe Iterator allows that while fail fast Iterator doesn't, they throw ConcurrentModificatoinException when something goes wrong during iteration, except certain operations like removing an element using Iterator's remove() method. This behavior is implemented with a variable called modCount which keeps track of modification done on underlying collection.
How to convert an Array to Collection like Set and List in Java? Example Tutorial
Hello Java programmer, if you are wondering how to convert an Array to Collection like Set or List in Java then you have come to the right place. In the past, I have shared how to convert ArrayList to HashMap in Java and In this example, we will learn how to convert a String array to
Collection, Set or List
in Java. You can convert an Array to Collection using helper method Arrays.asList() in Java. This is shortcut to convert array to List , Set or any Collection. This knowledge of
converting an array to Collection
can be really useful to any Java developer, as most legacy code tend to
use an array, which means you either need to pass them your input as array
or they return the result as an array.
How to Iterate over HashMap in Java? Map.entrySet().iterator() Example
What is the best way to Iterate over HashMap in Java? and not just HashMap, but any Map implementation including old Hashtable, TreeMap, LinkedHashMap and relatively newer ConcurrentHashMap, is a frequently asked query from Java Programmers, with some experience under his belt. Well, when it comes to choosing between different ways to iterate over Map in Java, it's you need, which plays an important role. For example, if you just want to iterate over each entry of HashMap, without modifying Map, then iterating over entry set using Java 1.5 foreach loop seems the most elegant solution to me.
How to use Queue Interface in Java? Example Tutorial
The JDK provides an implementation of several data structures in the Java collection framework like ArrayList is a dynamic array, LinkedList represents a linked list, HashMap represents a hash table data structure, and Queue interface represent queue data structure. Btw, If you are not familiar with data structure itself, then you can also see Introduction to Data Structures & Algorithms in Java, an excellent course to start with the essential data structure in Java. The Queue data structure allows you to process elements in the first in first out order, and that's why it is also known as a FIFO data structure.
10 Linux Books Every Programmer Should Read
Computer and Technology related books get outdated so fast. You might be better off getting a Linux magazine subscription. The fact that the basics rarely change would also help keep the texts relevant.
1) Understanding the Linux Kernel (Bovet & Cesati), 3rd Edition
This book talks about very specific data structures and OS algorithm implementations at they pertain to the 2.4 Linux kernel. Not sure what you are getting at. 3rd edition is 2006 ;)
2) Linux kernel development
4) Linux Bible Paperback 8th Edition
by Christopher Negus (Author), Christine Bresnahan (Contributor)
5) UNIX and Linux System Administration Handbook (4th Edition) Paperback – July 24, 2010
UNIX and Linux System Administration Handbook is an absolute must-have for any sysadmin or Linux systems engineer.
6) RHCSA/ RHCE Red Hat Linux Certification: Exams (Ex200 & Ex300) (Certification Press) Paperback – June 17, 2011
by Michael Jang (Author)
1) Understanding the Linux Kernel (Bovet & Cesati), 3rd Edition
This book talks about very specific data structures and OS algorithm implementations at they pertain to the 2.4 Linux kernel. Not sure what you are getting at. 3rd edition is 2006 ;)
2) Linux kernel development
3) Linux in a Nutshell - 1997
Linux, In a Nutshell, is great, see about getting more of the O'Reilly books if you want good reference books.4) Linux Bible Paperback 8th Edition
by Christopher Negus (Author), Christine Bresnahan (Contributor)
5) UNIX and Linux System Administration Handbook (4th Edition) Paperback – July 24, 2010
UNIX and Linux System Administration Handbook is an absolute must-have for any sysadmin or Linux systems engineer.
6) RHCSA/ RHCE Red Hat Linux Certification: Exams (Ex200 & Ex300) (Certification Press) Paperback – June 17, 2011
by Michael Jang (Author)
7) The Linux Programming Interface
The Linux Programming Interface: A Linux and UNIX System Programming Handbook (Michael Kerrisk) I've found The Linux Programming Interface to be one of the best in-depth books I've ever tried to read on Linux. Only issues with it are it's not too hands-on, so it's a bit difficult to retain, and maybe the price, bit would have to say it's still totally worth it.The only problem is its size, 1500+ pages might look intimidating, so use it on a need basis, more like a reference book. Instead of focusing on size, focus on what looks interesting, as this isn't the kind of book you read back-to-back and it's more helpful in using to figure out what you don't know you don't know.
8) Unix Shell Programming (3rd Edition), by Stephen Kochan (old bell labs employee)
9) The Linux Command-Line.
Have a look at The Linux Command-Line. The book focuses on novice users and the extended version is free for download.
10) Bash Guide, which I used to read
Books on bash would be useful as well since bash plays an important role in Linux.
That's all about some of the best book to learn Linux and Shell Programming.
8) Unix Shell Programming (3rd Edition), by Stephen Kochan (old bell labs employee)
9) The Linux Command-Line.
Have a look at The Linux Command-Line. The book focuses on novice users and the extended version is free for download.
10) Bash Guide, which I used to read
Books on bash would be useful as well since bash plays an important role in Linux.
That's all about some of the best book to learn Linux and Shell Programming.
3 Difference between multi-threading and multitasking? [Answered]
Hello guys, what is the difference between multithreading and multitasking is a common Java interview questions. If you are also wondering what it the real difference between them as they sound similar the continue reading this article and you will find the answer. In the programming world, there are two main ways to improve the throughput of a program, by using multi-threading and by using multitasking. Both take advantage of parallelism to efficiently utilize the immense power of the CPU and improve the throughput of your program. Actually, multi-threading is nothing but thread-based multitasking.
What is difference between start and run method of Thread in Java? Answer
Hello guys, what is the difference between calling start() vs run() method in Java multithreading is a popular core Java interview questions and if you are wondering the subtle difference between two then you have come to the right place. In this article, I will answer this question and show you an example as well. If you remember, a Thread is started in Java by calling the start() method of java.lang.Thread class, but if you learn more you will find out that the start() method internally calls the run() method of the Runnable interface to execute the code specified in the run() method in a separate thread.
Difference between Self and Equi Join in SQL - INNER Join example MySQL
The main difference between Self Join and Equi Join is that In Self Join we join one table to itself rather than joining two tables. Both Self Join and Equi Join are types of INNER Join in SQL, but there is a subtle difference between the two. Any INNER Join with equal as join predicate is known as Equi Join. SQL Joins are the fundamental concept of SQL similar to correlated and noncorrelated subqueries or using group by clause and a good understanding of various types of SQL join is a must for any programmer.
3 Examples to convert a Map to List in Java 8 - Example Tutorial
Hello guys, when you convert a Map to List in Java 8 or before, you have three choices like you can get a list of keys from Map, a List of values from Map, or a List of entries from Map, which encapsulates both keys and values. The API doesn't provide these methods because Map doesn't guarantee any order and the List interface guarantees ordering, like insertion order. Hence, JDK API provides an equivalent method to convert a Map to Set, like keySet() will return a set of keys and entrySet() will return a set of entries.
How to Convert a Stream to List, Set, and Map in Java? Example Tutorial
Hello guys, if you are wondering how to convert a Java Stream to Java Collections like List, Set and Map then you have come to the right place. Earlier, I have shared free Java Courses for beginners and in this article, I am going to share examples to convert Stream to ArrayList, LinkedList, HashSet, TreeSet, LinkedHashSet, TreeMap, HashMap, and ConcurrentHashMap in Java. These are easy-to-follow examples and suitable for both beginners and experienced Java programmers. In Java 8, Stream is one of the most important classes as it allows a lot of useful functional operations like filter, map, flatmap, etc on a collection of objects. Hence, going forward converting a Collection to Stream, performing operations, and then converting the result back to different Collection classes like List, Set, and Map will be a common task.
Subscribe to:
Posts (Atom)