As s java developer we need to know some of the basic but important things while coding. I found some of the below in my experience.
1. Divided by zero: Generally if we divide any number by using / operator , we will get arithmatic exception. Similarly, when some one asked me what is the output of 1/0.0, without any hesitation i said, it will give run time exception. But answer is wrong. Actually it will print INFINITY.
package com.sample.java.testing;
public class DividedByDoubleTest{
public static void main(String[] args) {
int i = 10;
System.out.println(i / 0.0);//Infinity
System.out.println(i % 0.0);//nan
System.out.println(i / 2);//quotient
System.out.println(i % 2);//reminder
System.out.println(i % 0);//java.lang.ArithmeticException: / by zero
System.out.println(i / 0);
}
}
Output:
Infinity
NaN
5
0
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.sample.java.testing.PrintTriAngleTest.main(PrintTriAngleTest.java:12)
2. Overriding: To override any method in sub class, we need to have same signature, return type and method name and sub class can not modify the method behavior like it can not throws any exception if super class doe not do any like that.
Coming to Static method overridden it seems like overridden but actually not .Because static method is bind with class name. Hence we can have same static method name both is super class and subclass like separate methods not like Overridden.We can access super class static method with super class and subclass static method with subclass but coming to instance method, we can access the super class methods with subclass. And also we can access the overridden methods of subclass by using the parent class reference if we assign subclass object to parent class reference .This is what run time polymorphism.
We need multiple classes to get overridden feature.
3. Overloading: One class can have different methods with same name but either argument return type or argument count should be different.
4. Static/Instance : We can access the static variables in side instance methods and also we can access static fields/methods using instance of class. Because we are creating object for the class only so variables declared in class can be accessible using object of class which are either static or instance fields.Static filed will store in method area and when JVM loads the class first it loads static blocks/fields. But we can not access instance variable//methods using the class name because instance fields are accessible / memory allocated when we create the object and these are stored in heap memory.
5. Declaration : This is what just we have just write variable declaration etc.
String s;//Declaration
int k = 10;
6. Initiation: When we create object with new Keyword, then memory will allocate and reference for an object is created. So that we can access all the methods or fields using reference.
7. Initialization : Default values will be allocated for the filed into Memory. This is what happen by Constructor when we called. All variables will be initialized with some value in memory here.
ClassTest t;//Declaration
t = new ClassTest(); //initiation and initialization memory and initialize the variable t.
1. Divided by zero: Generally if we divide any number by using / operator , we will get arithmatic exception. Similarly, when some one asked me what is the output of 1/0.0, without any hesitation i said, it will give run time exception. But answer is wrong. Actually it will print INFINITY.
package com.sample.java.testing;
public class DividedByDoubleTest{
public static void main(String[] args) {
int i = 10;
System.out.println(i / 0.0);//Infinity
System.out.println(i % 0.0);//nan
System.out.println(i / 2);//quotient
System.out.println(i % 2);//reminder
System.out.println(i % 0);//java.lang.ArithmeticException: / by zero
System.out.println(i / 0);
}
}
Output:
Infinity
NaN
5
0
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.sample.java.testing.PrintTriAngleTest.main(PrintTriAngleTest.java:12)
2. Overriding: To override any method in sub class, we need to have same signature, return type and method name and sub class can not modify the method behavior like it can not throws any exception if super class doe not do any like that.
Coming to Static method overridden it seems like overridden but actually not .Because static method is bind with class name. Hence we can have same static method name both is super class and subclass like separate methods not like Overridden.We can access super class static method with super class and subclass static method with subclass but coming to instance method, we can access the super class methods with subclass. And also we can access the overridden methods of subclass by using the parent class reference if we assign subclass object to parent class reference .This is what run time polymorphism.
We need multiple classes to get overridden feature.
3. Overloading: One class can have different methods with same name but either argument return type or argument count should be different.
4. Static/Instance : We can access the static variables in side instance methods and also we can access static fields/methods using instance of class. Because we are creating object for the class only so variables declared in class can be accessible using object of class which are either static or instance fields.Static filed will store in method area and when JVM loads the class first it loads static blocks/fields. But we can not access instance variable//methods using the class name because instance fields are accessible / memory allocated when we create the object and these are stored in heap memory.
5. Declaration : This is what just we have just write variable declaration etc.
String s;//Declaration
int k = 10;
6. Initiation: When we create object with new Keyword, then memory will allocate and reference for an object is created. So that we can access all the methods or fields using reference.
7. Initialization : Default values will be allocated for the filed into Memory. This is what happen by Constructor when we called. All variables will be initialized with some value in memory here.
ClassTest t;//Declaration
t = new ClassTest(); //initiation and initialization memory and initialize the variable t.
No comments:
Post a Comment