Postingan

Menampilkan postingan dengan label core java interview question

Difference between extends and implements keywords in Java

Though both extends and implements keyword in Java is used to implement Inheritance concept of Object-Oriented programming, there is a subtle difference between them. The extends keyword is mainly used to extend a class i.e. to create a subclass in Java, while implements keyword is used to implement an interface in Java. The extends keyword can also be used by an interface for extending another interface. In order to better understand the difference between extends and implements , you also need to learn and understand the difference between class and interface in Java . Though both are an integral part of application development using object oriented methodology, an interface is more abstract than class hence it is used to define API or contract. Read more �

Top 10 Java Swing Interview Questions Answers asked in Investment banks

The Swing API and toolkit are heavily used in developing trading application front end in Java on both large Investment banks and small broker firms. Doesn�t matter whether its front office or middle office application, you will find Java Swing GUI everywhere. The Swing-based GUI is used to develop Order entry system, Order monitoring GUI and for other tools which trader or operations can use on different trade life cycle in the front office. middle office and back office space. Due to its heavy usage, there are a lot of requirements of Java Swing developer in investment banks like Barclays, Citibank, Deutsche Bank, Nomura, JP Morgan, Morgan Stanley and Goldman Sachs etc. Though in the era of Java 8, Java FX is positioned to take over from Swing but there are still a lot of legacy application which means the requirements for Java Swing developers will not dry out soon. Read more �

Integer vs floating point arithmetic - Java Coding Question

I am starting a new series called Java Coding Quiz, in which I'll show you subtle Java concepts hidden in the code. This is an OCAJP or OCPJP style question but focused on teaching subtle details of Java programming language. In today's puzzle, you will learn about one of the key concepts about how floating point and integer arithmetic works in Java. This is a very important concept for any Java developer because Java behaves differently when the same operation is performed by different types of variable but of the same value. Read more �

How to check if ResultSet is empty in JDBC - Java Example

The JDBC ResultSet doesn't provide any isEmpty() , length() or size() method to check if its empty or not. Hence, when a Java programmer needs to determine if ResultSet is empty or not, it just calls the next() method and if next() return false it means ResultSet is empty. This is correct but the main problem with this approach is if the ResultSet is not empty then you may miss the first row if you follow the standard idiom to iterate over ResultSet and get the rows which involve calling next() method of ResultSet in a while loop. The key thing, which you need to remember is that initially the ResultSet's cursor points to before the first row when you call the next() method it points to the first row and if you don't get this data and calls the next() method again then you will lose the first row. Read more �

Why Timestamp cannot be used in place of Date in Java?

One of the tricky question from Java Interview is, "Can we pass a Timestamp instance to a method expecting java.util.Date? " , it's a tricky question because the answer is both Yes and No . You can, of course, pass a Timestamp object to a method with the argument as Date because first, Timestamp is a subclass of java.util.Date and second it contains both date and time values which are missing in either java.sql.Date and java.sql.Time. So there is more reason to pass a Timestamp value to Date but you should not be doing that. Why? because Timestamp is not exactly Date. It's a composite type of java.util.Date and an additional nanosecond value which is fitted there to confirm database DATETIME data type, which supports nanosecond precision. If you look at the implementation of java.sql.Timestamp class, you will find that the long value supplied by Date is stored separately then this nanosecond value. Read more �

Java Object Oriented Analysis and Design Problem - Vending Machine Part 2

This is the second part of Java tutorial to show how to create Vending Machine in Java. In the first part, we have discussed problem statement and the solution itself, but unit testing and design document was still pending, which we'll see in this article. As I said, there are multiple ways to implement Vending machine in Java e.g. you could have easily used state design pattern to implement a vending machine, in fact, it's one of the best examples of State design pattern. Vending machine behaves differently on different states e.g. return a product if the machine is not empty, otherwise, it just returns coins, so it ideally fits in state design pattern. Though, In our solution, I have not used the state design pattern but have just coded the solution with an if else block. This is easier because of a limited number of state and not many state transition but in more real world scenario, state design pattern is better as it uses Polymorphism and removes the logic we have put ins...

What is difference between Synchronized and Concurrent Collections in Java?

Synchronized vs Concurrent Collections Though both Synchronized and Concurrent Collection classes provide thread-safety, the differences between them comes in performance , scalability and how they achieve thread-safety. Synchronized collections like synchronized HashMap , Hashtable , HashSet , Vector, and synchronized ArrayList are much slower than their concurrent counterparts e.g. ConcurrentHashMap , CopyOnWriteArrayList , and CopyOnWriteHashSet . Main reason for this slowness is locking; synchronized collections locks the whole collection e.g. whole Map or List while concurrent collection never locks the whole Map or List. They achieve thread safety by using advanced and sophisticated techniques like lock stripping. For example, the ConcurrentHashMap divides the whole map into several segments and locks only the relevant segments, which allows multiple threads to access other segments of same ConcurrentHashMap without locking. Read more �

Difference between notify and notifyAll in Java - When and How to use

notify vs notifyAll in Java What is the difference between notify and notifyAll method is one of th e tricky Java questions , w hich is easy to answer but once Interviewer asks follow-up questions, you either got confused or not able to provide clear-cut and to the point answers? The main difference between notify and notifyAll is that notify method will only notify o ne Thread a nd notifyAll method will notify all Threads which are waiting on that monitor or lock. By the way, this is something you have been reading in all over places and to be frank, this statement despite being correct is not complete and its very difficult to understand difference between notify vs notifyAll by just reading this statement. Lot of questions comes in mind like Which thread will be notified if I use notify() ? How do I know how many threads are waiting, so that I can use notifyAll() ? How to call notify()? What are these thread waiting for being notified etc? Actually, discussion of notify...

Difference between ExecutorService.submit() and Executor.execute() methods in Java?

What is the difference between Executor.submit() and Executor.execute() method in Java? is one of the good multi-threading questions for experienced Java programmers, mostly asked in Investment Banks like Barclays, Deutsche Bank, or Citibank. A main difference between the submit() and execute() method is that ExecuterService.submit() can return result of computation because it has a return type of Future , but execute() method cannot return anything because it's return type is void . The core interface in Java 1.5's Executor framework is the Executor interface which defines the execute(Runnable task) method, whose primary purpose is to separate the task from its execution. Read more �

Top 10 Servlet Interview Question Answers - J2EE

Gambar
This time its servlet interview questions, I was thinking what to pick for my interview series and then I thought about J2EE and Servlet is my favorite on that space. Servlet is an important part of any J2EE development and serves as Controller on many web MVC frameworks and that�s why it�s quite popular on J2EE interviews. These Servlet questions are based on my experience as well as collected by friends and colleague and they are not only good for interview practice but also shows a new direction of learning for anyone who is not very familiar with servlet technology. As said earlier this interview question article is part of my earlier series java interview questions , UNIX command interview questions and Java threading interview questions . You can find answers to all these questions on google but I have also listed my answers for quick reference. Read more �

Difference between Stack and Heap memory in Java

The difference between stack and heap memory is c ommo n programming question a sked by beginners learning Java or any other programming language. Stack and heap memory are two terms programmers starts hearing once they started programming but without any clear and definite explanation. Lack of knowledge of what is a heap in Java an d 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 which is used to store elements in LIFO(Last In First out) order and available in Java API as java.util.Stack . In general, both stack and heap are part of memory, a program is allocated and used for different purposes. Java program runs on JVM which is launched as a process by "java" command. Java also uses both stack and heap memory for different n eeds. In our last article 10 points on Java heap space , I h ave touched base on Java heap space and in this article we will see the difference between s...

How to Convert and Print Byte array to Hex String in Java

We often need to convert byte arrays to Hex String in Java, In order to print byte array contents in a readable format. Since many cryptographic algorithms e.g. MD5 returns hash value as a byte array, In order to see and compare that byte array, you need to convert byte array to Hex String. As we have seen, while generating MD5 checksum of File , there are multiple ways to convert byte array to Hexadecimal String in Java . You can either write your own method, or you can use open source library e.g. Apache commons codec to create Hex String from a byte array in Java. Commons codec provides a utility class Hex , which contains an encodeHexString() method to create Hex String from a byte array . It's one of the best options to genera te Hex String if you a re already using this library to generate MD5 hash values. In this Java tutorial, we will see what is the issue with printing byte array as normal String, and 2 examples to convert a byte array into Hexadecimal String in Java. Re...

How to check if two String are Anagram in Java - Program Example

Write a Java program to check if two String are anagram of each other, is another good coding question asked at fresher level Java Interviews. This question is on similar level of finding middle element of LinkedList in one pass and swapping two numbers without using temp variable . By the way two String are called anagram, if they contains same characters but on different order e.g. army and mary , stop and pots etc. Anagrams are actually mix-up of characters in String. If you are familiar with String API, i.e. java.lang.String than you can easily solve this problem. In order to check if Strings are anagram, you need to get there character array and see if they are equal or not. Though you can also use indexOf() , substring() and StringBuffer or StringBuilder class to solve this question. In this Java program, we will see 3 ways to solve this interview questions, and check if two String are anagram or not. By the way, if you are preparing for Java interview, it's good to...

Can You Overload or Override Static methods in Java

Can static method be overridden in Java, or can you override and overload static method in Java , is a common Java interview question, mostly asked to 2 years experienced Java programmers. Answer is, No, you can not override static method in Java , though you can declare method with same signature in sub class. It won't be overridden in exact sense, instead that is called method hiding . But at same time, you can overload static methods in Java, there is nothing wrong declaring static methods with same name, but different arguments. Some time interviewer also ask, Why you can not override static methods in Java ? Answer of this question lies on time of resolution. As I said in difference between static and dynamic binding , static method are bonded during compile time using Type of reference variable, and not Object. If you have using IDE like Netbeans and Eclipse, and If you try to access static methods using an object, you will see warnings. As per Java coding convention, static...

Top 15 Data Structures and Algorithm Interview Questions for Java programmer - Answers

Data structures and algorithm questions are an important part of any programming job interview, be it a Java interview, C++ interview or any other programming language. Since data structures are core programming concept, it's mandatory for all programmers, to know basic data structures like stack, linked list, queue, array, tree, and graph. Though tree and graph are on the tough side, I still see programmers get familiar will all these. Any lis t of programming job interview questions is incomplete without questions from data structures and algorithms. Similarly, while going on questions from data structure you may get some programming exercise as well e . g. swapping numbers without temp variable. The li nked list and array are favorite topics in any data structure interview, questions like reversing linked list, traversing linked list or deleting nodes from linked list, which involves algorithm and data structures are quite common. Read more �

Bitwise and BitShift Operators in Java - AND, OR, XOR, Signed Left and Right shift Operator Examples

Bitwise and Bit Shift operators in Java are powerful set of operators which allows you to manipulate bits on integral types like int, long, short, bytes and boolean data types in Java. Bitwise and Bit shift operator are among the fastest operator in Java but still many Java programmers doesn't familiar with bitwise and bitshift operations, especially those who doesn't come from C programming backgrounds. If you have already learned C or C++ before starting with Java then understanding bitwise and bitshift operators are quite easy in Java, because its similar to bitwise operation in C. Some of th e tricky programming interview questions e .g. checking if a number is power of tw o or swapping two numbers without temporary variable or, can be easily solved using bitwise operators. This Java programming tutorial is quick recap of different bitwise operator available in Java and how to use them. This tutorial also discusses bit shift operator, both signed and unsigned with example...

Difference between Singleton Pattern vs Static Class in Java

Singleton pattern vs Static Class (a class, having all static methods) is another interesting questions, which I missed while blogging about Interview questions on Singleton pattern in Java . Si nce both Singleton pattern and static class provides good accessibility, and they share some similarities e.g. both can be used without creating object and both provide only one instance, at very high level it looks that they both are intended for same task. Because of high level similarities, interviewer normally ask questions like, Why you use Singleton instead of Static Methods, or Can you replace Singleton with static class, and what are differences betwee n Singleton pattern and static in Java . In order to answer these question, it�s important to remember fundamental difference between Singleton pattern and static class, former gives you an Object , while later just provide static methods. Since an object is always much more capable than a method, it can guide you when to use Sing...

ReentrantLock Example in Java, Difference between synchronized vs ReentrantLock

ReentrantLock in Java is added on java.util.concurrent package in Java 1.5 along with other concurrent utilities like CountDownLatch , Executors and CyclicBarrier . ReentrantLock is one of the most useful addition in Java concurrency package and several of concurrent collection classes from java.util.concurrent package is written using ReentrantLock , including ConcurrentHashMap , see How ConcurrentHashMap works in Java fo r more details. Two key feature of ReentrantLock, which provides more control on lock acquisition is trying to get a lock with ability to interrupt, and a timeout on waiting for lock, these are key for writing responsive and scalable systems in Java. In short, ReentrantLock extends functionality of synchronized keyword in Java and open path for more controlled locking in Java. In this Java concurrency tutorial we will learn : What is ReentrantLock in Java ? Difference between ReentrantLock and synchronized keyword in Java? Benefits of using Reentrant lock in J...

How to create Immutable Class and Object in Java - Tutorial Example

Writing or creating immutable classes in Java is becoming popular day by day, because of concurrency and multithreading advantage provided by immutable objects. Immutable objects offers several benefits over conventional mutable object, especially while creating concurrent Java application. Immutable object not only guarantees safe publication of object�s state, but also can be shared among other threads without any externa l synchronization . In fact JDK itself contains several immutable classes l ike String , Integer an d other wrapper classes. For those, who doesn�t know what is immutable class or object, Immutable objects are those, whose state can not be changed once created e.g. java.lang.String , once created can not be modified e.g. trim, uppercase, lowercase. All modification in String result in new object, see why String is immutable in Java for mor e details. In this Java programming tutorial, we will learn, how to write immutable class in Java or how to make a class immu...

How to get Key from Value in Hashtable, HashMap or Map in Java

It's not easy to get key from value in Hashtable or HashMap, as compared to getting value from key because HashMap or Hashtable doesn't enforce one to one mapping between key and value inside Map in Java. in fact, Map allows same value to be mapped to multiple keys insid e HashMap , Hashtable or any other Map implementation. What you have in your kitty is Hashtable.containsValue(String value) or Hashtable.containsKey(String key) to check whether key or value exists in Hashtable or not, but sometimes we want to retrieve a value from Map corresponding to any key and there is no API method to do in Map. We can still do this, but it highly depends data in your Map because Hashtable and HashMap both allows duplicate value s mapped to the different key. In this Java tutorial, we will see an example of how to get key from value in Hashtable in Java. Read more �