Posts

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. L...

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 =...

Object Class in Java

Object class is super class of all classes in Java. Every class in Java extends Object class directly or in-directly. There are total 12 methods in Object class. You can call override all non-final, public and protected methods. You can also call all public methods on object of any class(custom or in-built). toString clone equals hashCode notify and notifyAll wait finalize registerNatives getClass

We have 2 overloaded methods, one has Integer and another has String as parameter. You are calling method with null. Which method will be called

Question: You have 2 overloaded methods, doSomething, in below code. One takes Integer as parameter whereas another takes String. If you call it with null, which method will be called? What should be output of below code? public class OverloadDemo { public void doSomething(String str){ System.out.println("do something with String"); } public void doSomething(Integer str){ System.out.println("do something with Integer"); } public static void main(String[] args) { OverloadDemo overloadDemo = new OverloadDemo(); overloadDemo.doSomething(null); } } Answer: Compilation error -  The method doSomething(String) is ambiguous for the type OverloadDemo

equals() method of Object class in Java

Image
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...

How to define Auxiliary Constructor in case class in Scala

We have seen case class previously. We had discussed what is case class, how to create object of case class, list of boilerplate code generated for case class. If you don't know case class, I recommend to understand case class first. Let us revise case class declaration. case class Person(id:Int, name: String) Person class is declared with primary constructor where id and name are parameter. So good so far. The constructor which is not primary constructor, other than primary constructors and overloaded to primary constructor are called auxiliary constructor. How will you declare Auxiliary(other overloaded) constructors. What is syntax of creating auxiliary constructor. Please note that constructors in case class are not really constructor. Actually these are apply methods on companion object. Auxiliary constructor in case class are different than regular class. We can add auxiliary constructors in case class by adding apply() methods in companion object of case class...

What is difference between Runnable and Thread? What is the best way to create thread?

There are 2 ways of creating thread. First way is by extending Thread class and another is implementing Runnable interface. What are the differences between these 2 ways. This is frequently asked question in Java interviews. 1. Multiple Inheritance: Thread is a class. Once you extend Thread class, you can not extend other class because Java does not support multiple inheritance. Whereas Runnable is interface so you can extend other class while implementing Runnable interface. 2. Worker and Job: Thread is worker which works on job(Runnable). It is always a good practice to separate code of worker and job. If we create thread by extending Thread class, It will combine code of worker and job. So it is good practice to implement Runnable interface. Object of this class(Job) will be passed to thread constructor(Worker). The thread instance will call run method on class which implements Runnable. This is how we can separate Worker and its Job. 3. extends means adding new Feature: Whe...