Why convert from a list to a map? Maps allow you to store and access data by a “KEY”. Lists only allow you to access data by either index or by looping through the whole list to find a value that you are interested in. … Read more ›...
Why convert from a list to a map? Maps allow you to store and access data by a “KEY”. Lists only allow you to access data by either index or by looping through the whole list to find a value that you are interested in. … Read more ›...
Current Balance = Initial Amounts + Credits – Debits OOP approach using the builder design pattern Creating a constructor to take three BigDecimal values is NOT only readable, but also prone to errors as you can inadvertently supply the debit value instead of the credit value, … Read more ›...
Q1. How much memory space does a primitive type int occupy in Java? A1. 4 bytes. Q2. Java objects get stored in the heap memory space, but how about the primitive variables? A2. … Read more ›...
Q18. Given code:
|
1 2 3 |
List<String> list = new ArrayList<>(); list.add("Java"); list.add("JEE"); |
A18. Firstly, let’s take abstraction and encapsulation as the difference is subtle. Abstraction is often not possible without encapsulation because if a class exposes its internal state, it can’t change its inner workings.
…
Mistake #1: Using floating point data types like float or double for monetary calculations. This can lead to rounding issues.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.monetary; import java.math.BigDecimal; public class MonetaryCalc { public static void main(String[] args) { System.out.println(1.05 - 0.42); //1: $0.6300000000000001 //2: $0.63 System.out.println(new BigDecimal("1.05") .subtract(new BigDecimal("0.42"))); //3: $0.630000000000000059952043329758453182876110076904296875 System.out.println(new BigDecimal(1.05) .subtract(new BigDecimal(0.42))); //4: $0.63 System.out.println(BigDecimal.valueOf(1.05) .subtract(BigDecimal.valueOf(0.42))); System.out.println(105 - 42); //5: 63 cents } } |
In the above code, 2, 4, and 5 are correct and 1 and 3 are incorrect usage leading to rounding issues.
…
Q1. The “java.util.function” package only have “Function<T,R“, which takes an input and returns an output, and “BiFunction<T,U,R>“, which takes two inputs and returns an output. How would you then use a function with 3 or more input arguments? A1. … Read more ›...
Sorting objects in Java interview Q&As especially understanding Comparable & Comparator interfaces in Java for natural & custom sorting respectively. Q1. If I mention the interface names Comparable or Comparator, what does come to your mind? … Read more ›...
This post extends Transforming your thinking from OOP to FP. In Big-data, functional programming is prevalent when working with data sets. For example, writing a Spark job to work with RDDs (Resilient Distributed Data sets). In Imperative (E.g. OOP, procedural programming, etc) programming you can say
|
1 2 3 |
x = x + 5 y = x + y |
where you are...
Java multithreading Q&As on concepts like blocking, and debugging blocked threads. How do you sequence threads? what do you understand by the term preemptive scheduling? Q1. What are the different ways a Java thread gets blocked or suspended? How will you debug Java threading issues? A1. … Read more ›...
Requirements gathering
Analysis
…
Q1. What is wrong with old Java.util.Date & java.util.Calendar/java.sql.* APIs and why should joda-time & Java 8 libraries favored? A1. Old Java.util.Date & java.util.Calendar/java.sql.* -> Joda-Time (Pre Java 8) -> Java 8 Date/Time java.util.Date, … Read more ›...
int result = input / divideBy will throw an unchecked exception if divideBy = 0. OOP error handling by throwing exceptions In OOP errors are handled by throwing & catching exceptions. Here is a very simple OOP example. Step 1: Define an interface. … Read more ›...
In an earlier post we looked at Understanding Overriding, Hiding, and Overloading in Java?. We also discussed how “method overriding” gives polymorphism. In this post, let’s see how a “generic method” can replace a number of overloaded methods. Then see under the hood as to what happens to the compiled...
I have a good news, I got job offer. I like to share with you from my bottom of my heart feeling thankful to you.