Posts

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