Yes. it can.
abstract class means it cannot be instantiated into objects. However, invoking a static method doesn't depends on any instance of a class. So even abstract class it is able to have static main method.
Sunday, 26 June 2016
Friday, 24 June 2016
Guessing what is the output?
try - catch -finally maybe not that simple.
principle:
1) finally block will be operated regardless of return clause. Even it returns in the inner catch block, but outer finally block will still be operated no matter what.
2)
3) return will quite from current position and return control back to the main method.
4) finally block will be operated regardless if there is or not an exception caught.
5) FileNotFoundException is a checked exception.
6) IndexOutOfBoundsException is a un-checked (Runtime) exception, I explicitly catch it here.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
/**
*
* @author YNZ
*/
public class TasteEx {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
try {
FileReader fr = new FileReader(new File("test.txt"));
} catch (FileNotFoundException ex) {
System.out.println("File not found");
return;
} finally {
System.out.println("inner final");
}
System.out.println("try outer");
int[] ary = new int[2];
ary[0] = 10;
ary[5] = 20;
} catch (IndexOutOfBoundsException ex) {
System.err.println("Index out of bounds");
} finally {
System.out.println("outer final");
}
System.out.println("next task");
}
}
How about moving return also in the finally block?
this gives a compiling error, for //3 cannot be reached.
public static void main(String[] args) {
try {
try {
FileReader fr = new FileReader(new File("test.txt"));
} catch (FileNotFoundException ex) {
System.out.println("File not found");
return; //1
} finally {
System.out.println("inner final");
return; //2
}
System.out.println("try outer"); //3
int[] ary = new int[2];
ary[0] = 10;
ary[5] = 20;
} catch (IndexOutOfBoundsException ex) {
System.err.println("Index out of bounds");
} finally {
System.out.println("outer final");
}
System.out.println("next task");
}
}
Sunday, 19 June 2016
super() implicitly added in sub-class constructor
If sub-class constructor does not explicitly invoke any super-class constructor, a super() will be automatically added in the first line of sub-class constructor.
class Father {
public Father() {
System.out.println("in father!");
}
public Father(String wife) {
System.out.println("in father wife!");
}
}
class Kid extends Father {
public Kid() {
System.out.println("in kid!");
}
public Kid(String mother) {
System.out.println("in kid mother!");
}
}
/**
*
* @author YNZ
*/
public class TestSuper {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Kid kid = new Kid();
System.out.println("----");
Kid kid1 = new Kid("mother");
}
}
output
run: in father! in kid! in father! in kid mother!
so when sub-class doesn't explicitly invoke any super-class constructor, and mean while super class has no non-argument constructor defined, it will cause an compiling error.
Implicit and explicit casting
Implicit casting, moving from a small container to a big container.
OR:
Explicit casting, moving from a big container to small container.
byte (8) short(16) int(32) long(64) float(32) double(64)
excluding char(16) un-singed
/**
*
* @author YNZ
*/
public class ExplicitCasting {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
byte b = 127;
short s = b;
int i = s;
long l = i;
float f = l;
double d = f;
//byte short int long are integer types, but not char
//char is not integer and decimal(floating)
char c = 10000;
c = (char) b;
c = (char) s;
c = (char) i;
c = (char) l;
c = (char) f;
c = (char) d;
i = c; //int is big enough to hold char
l = c; //long is big enough
s = (short) c; //no
b = (byte) c; //no
f = c;
d = c;
}
}
Can new an Interface or Abstract class?
Both interface and abstract class cannot not be instantiated, for they are pre-defined abstractions.
However in one situation, you may use 'new' followed by the interface type or abstraction class type.
When declaring an array, you may declare an interface type and abstract class type array.
import java.util.ArrayList;
import java.util.List;
/**
*
* @author YNZ
*/
interface myInterface{
void doSth();
}
class myDevice extends myAbstract implements myInterface{
@Override
public void doSth() {
System.out.println("my device do something!");
}
}
abstract class myAbstract{
abstract public void doSth();
}
public class InterfaceArray {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
myInterface[] is = new myInterface[3];
is[0] = new myDevice();
is[1] = new myDevice();
is[2] = new myDevice();
List[] ls = new List[4];
ls[1] = new ArrayList();
ls[4] = new ArrayList();
myAbstract[] as = new myAbstract[2];
as[0] = new myDevice();
as[1] = new myDevice();
}
}
Saturday, 18 June 2016
OOAD: Overloading
The purpose of overloading is to re-use the method signature.
Overloading methods cannot be validated by differing return types.
In the following case, imagine what will the output?
Overload Methods
Overloading is distinguished and validated only by input augment lists.
- The change in the number of parameters that are accepted.
- The change in the type of method parameters that are accepted.
- The change in the positions of the parameters that are accepted
public void method(float m1, int m2){}
public void method(int m2, float m1){}
The sequence of parameters can create different argument lists, so it is valid overloading.
Overloading methods cannot be validated by differing return types.
double calcAverage(int m1, int m2){}
int calcAverage(int m1, int m2){}
the above is invalid overloading and leading to a compiling error.
Overloading methods cannot be validated by differing access modifiers
Overloading method cannot be validated by differing non-access modifiers
When the class don't share an inheritance relationship, there isn't any confusion with the version of the method that will be called.
Class Base{}
Class A extends Base{}
Class B extends Base{}
A and B they both in a different branch of the inheritance tree, they are not a confusion;
However, A and Base are on the same inheritance branch; they may create confusion.
A is a Base; while Base is also Base; These two types may confusing the overloading.
Class Reception{
public void accept(Base var){} //1
public void accept(A var){}//2
}
If invoking accept(new A()); Which overloading method will be called?
here the A instance is created, so //2 should be invoked.
The overloaded methods are bound at the compiling time but not runtime.
In another way:
Base a = new A();
accept(a)
On the compiling time, a is considered as a Base class type because of the reference variable type, the //1 is therefore invoked.
Overloaded Constructors
- Overloaded Constructor must be defined using a different argument list.
- Overloaded Constructors cannot be validated by differing only the access modifiers.
Using this key to refer to other overloaded constructors in a constructor; it is a common mistake to use a Class name to refer to other overloaded constructors in a constructor.
Using non-access modifiers with constructors is illegal. The code won't compile.
Using non-access modifiers with constructors is illegal. The code won't compile.
In the following case, imagine what will the output?
/**
*
* @author YNZ
*/
class TestClass {
void probe(int... x) {
System.out.println("In ...");
} //1
void probe(Integer x) {
System.out.println("In Integer");
} //2
void probe(long x) {
System.out.println("In long");
} //3
void probe(Long x) {
System.out.println("In LONG");
} //4
void probe(Object x) {
System.out.println("In Object");
} //5
void probe(int x) {
System.out.println("In int");
} //6
public static void main(String[] args) {
Integer a = 4;
new TestClass().probe(a); //5
Long b = 4L;
new TestClass().probe(b); //6
byte c = 100;
new TestClass().probe(c);
char d = 'a';
new TestClass().probe(d);
int e = 10;
new TestClass().probe(e);
String str ="aha";
new TestClass().probe(str);
int[] array = new int[3];
new TestClass().probe(array);
}
}
static and instance init. block
Init. block is located in the class body, could be anywhere.
Static initialize block: used for init. static variables; it will be invoked as class loaded. One class could have several static init. blocks. they will be invoked by the sequence in the body.
static {
//whatever code here
}
Instance initialize block: used for init. instance variables, and shared by the overloaded constructors. On compiling time, the instance init. block will be copied into each overloaded constructors. So static init. block is always invoked earlier than instance init. block.
{
//whatever code here
}
Static initialize block: used for init. static variables; it will be invoked as class loaded. One class could have several static init. blocks. they will be invoked by the sequence in the body.
static {
//whatever code here
}
Instance initialize block: used for init. instance variables, and shared by the overloaded constructors. On compiling time, the instance init. block will be copied into each overloaded constructors. So static init. block is always invoked earlier than instance init. block.
{
//whatever code here
}
/**
*
* @author YNZ
*/
public class InitBlocks {
static float sp;
final static Integer[][] matrix = new Integer[2][3];
Float[][] instMatrix = new Float[4][6];
//static init block is a place to init satic field,
//as it cannot be done in one line.
static {
Random r = new Random();
for (Integer[] matrix1 : matrix) {
for (int j = 0; j < matrix1.length; j++) {
matrix1[j] = r.nextInt(100);
}
}
}
{
Random r = new Random();
for (Float[] row : instMatrix) {
for (int i = 0; i < row.length; i++) {
row[i] = r.nextFloat();
}
}
}
InitBlocks() {
//if static field can be modified in the instance.
sp = 20;
}
static void printMatrix(T[][] mat){
for(T[] row: mat){
System.out.println(Arrays.toString(row));
}
}
public static void main(String[] args) {
printMatrix(InitBlocks.matrix);
InitBlocks ib = new InitBlocks();
printMatrix(ib.instMatrix);
}
}
Subscribe to:
Posts (Atom)
Can Jackson Deserialize Java Time ZonedDateTime
Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...
-
The strictfp keyword is used to force the precision of floating point calculations (float or double) in Java conform to IEEE’s 754 standard...
-
In construction. Spring test annotations: @SpringBooTest @DataJpaTest @TestPropertySource @ActiveProfiles @Sql @SpringBootTest It is used f...
-
As the name implies, an anonymous inner class isn’t defined using an explicit name. An anonymous inner class is created when you combine ins...