Posts

equals() method of Object class in Java

When you compare two object references using == operator, it evaluate to true only when both references refer to same object. public class Person { private Integer id; private String name; public Person(Integer id, String name) { this.id = id; this.name = name; } public static void main(String[] args) { Person p1 = new Person(1, "Prithvi"); Person p2 = p1; Person p3= new Person(1, "Prithvi"); System.out.println(p1==p2); System.out.println(p1==p3); } } Output: true false We have created 3 references of Person class. Object references p1 and p2 both refer to same object so comparison evaluate to true. Comparison of p1 and p3 evaluate to false, even though p3 has same contents as p1 but refer to different object so comparison returns false. Below image explains this - Objects p3 and p1 are meaningfully equal. We must have some way to see their content are same or not. But the question is, How will you compare two different object

Software design interview question

  Microservice design patterns. Singleton design pattern in Golang.

Golang Interview Questions

What is the new function? What is vendor in the go module? Write a producer-consumer program using goroutine. Write a two routine: even and old. The even goroutine should only print even numbers and the odd goroutine will only print odd numbers. The max number will be given as an input parameter. What is the closure function in Golang? How to create it? How to access the shared variable in Golang when multiple goroutines are trying to access it? How to handle race condition? What is the difference between Golang's concurrency with other languages? What is the difference between concurrency and parellism? Which one Golang uses and how? How Golang concurrency is fast.

Create your own LinkedList in Java

As a Java developer, we need to frequently use the collection framework. Have you ever tried to create your own any of the collection classes in Java? Here we go, we will see how to create our own LinkedList in Java. We will create a very simple class with add and get methods. Also, the LinkedList class of collection framework is doubly LinkedList but we will singly LinkedList. Line 36 to 42, we have created an inner class i.e Node with a generic type T. This class has 2 fields: item field is a generic type that will hold an actual value of LinkedList node. next field is a Node type that holds the address of the next node of the LinkedList. We have also defined a constructor to set value for the item field. In lines 4 and 5, we have created an instance variable of type Node in MyLinkedList class i.e. first and last. The field first will always hold the first node of the LinkedList and the field last will always hold the node of LinkedList. Now the question is, why

Identifiers in GO

Image
Introduction: Identifiers in any language are name of variable, function, array, struct etc. The same applicable for GO. There are certain rule and convention for identifier in GO. Rules: 1. Identifier should start with letters or underscore. 2. It can contain numbers but it should not be at the start. Number can place in middle or end of identifier. 3. Identifier are case sensitive. It means variables result and Result are two different variables. 4. Go keywords can not be used as identifier. Go has 25 reserved keywords: 5. Go has some predefined identifier. These are not reserved. You can use them in your program. It sounds weird but all basic datatypes are also predefined datatypes, you can use them as variable name in-fact as all identifiers. Below are the list of predefined identifiers: package main import ("fmt") //import "time" func main() { var int int =10 fmt.Println(int+2) } Even though it is allowed, I personally recommend that

divide by zero in Java

You might be thinking,  divide by zero will throw ArithmeticException. But this is not true for floating point numbers. Divide by zero throws ArithmeticException only when char or integral value are divided by 0. Below two operation throws exception 10/0 'A'/0 If floating point number is divided by 0, output will be Infinity . 10.2/0 -10.3/0 Output: Infinity -Infinity

Data types and Literals in Java

Image
Data types Data types category:   Data types summary: Literals A constant value which can be assigned to variable is called "Literal". Integral Literals All possible value of Integral data type(byte, short, int and long) are called Integral Literals. Default Value: By default every Integral literal is int type. When you assign a value in long data byte and that value is more than 2147483647(Maximum value for int) then compiler will give you error. Ex: long a = 3000000000; Error: The literal 3000000000 of type int is out of range  Even though we have used long data type and value is also within range of long but still compiler gives error because every integral literal is int type. We can resolve this error by specifying long type explicitly. We can specify long type by suffixing l or L . Ex: long a = 3000000000L; Number System: Integral Literal can be represented as Decimal, Octal and Hexadecimal number systems. Let's

Identifiers in Java

Image
Any name in Java program is called Identifier. It could be a class name, variable name or method name. Question: Find out list of identifiers in below program- public class App { public static void main(String[] args) { int a = 10; } } Answer:  1. Test 2. main 3. args 4. a Rule: 1. The only allowed characters in Java identifier are:     2. Identifiers can't start with digit. 3.  Java identifiers are case sensitive. Below are 3 different variables- int Number =10; int NumbeR =10; int NUBber =10; 4. There is not length limit for Java identifiers but it is not commanded to take more than 15 length. 5.  Reserved words can't be used as identifier. 6. All predefined Java classes name and interface names can be used as identifier. It is legal but not recommended. We have declared variable names as String and Runnable in below program. It is valid. public class App { public static void main(String[] args) { int String = 10; int Runna