Java: Using Assertion in Eclipse

Categories: Java; Tagged with: ; @ May 21st, 2012 0:06

Assertion:

	public static void main(String[] args) {
		int x = 10;
		assert x==100:"assertion failed!";
	}

How to determine assertion is enabled or not?

		   boolean isEnable=false;
		   assert isEnable = true;
		   if(isEnable){
		       throw new RuntimeException("Assertion shoule be enable!");
		   }

This is a bad assertion, because it change the value, If assertion is enabled, isEnable will be set to true.

How to enable assertion in Eclipse?

Debug/Run configurations > VM arguments: add “-ea” to enable it or “-da” to disable assertion.

should not used for validate parameter in public method: because anyone can use public method, and can not make sure assertion is enabled or not;

About Exceptions in Java

Categories: Java; Tagged with: ; @ May 20th, 2012 17:22

Exceptions in Java:

                                      Object
                                     Throwable
                         Error                    Exception

                                           RuntimeException …..   

There are two types Exception:

1. Checked Exception , we need to declare ‘throws’ in mehtod if there maybe exception be  thrown out, and must be catch or throw it if we use the method.
2. UnChecked exception(RuntimeExcepiton), will be thorwed in runtime, no need to declare them in the method.

1. UnChecked Exception:

public class TestException {
	public void test() throws NullPointerException{
		System.out.println("Test  OK");
	}
	
	public static void main(String[] args) {
		new TestException().test();
	}
}

output: “Test OK”

2. Checked Exception:

public class TestException {
	public void test() throws IOException{
		System.out.println("Test  OK");
	}
	
	public static void main(String[] args) {
		new TestException().test(); // Can not compile: surround with try/catch or add throws declaration
	}
}

3. Overriding:

Access modifier can be enlarged, but exception only can be less or specialized.

package scjp.liguoliang.com;

import java.io.FileNotFoundException;
import java.io.IOException;

public class TestException {
	public void test() throws IOException{
		System.out.println();
	}

}

class TestExceptionSub extends TestException {
	public void test() throws FileNotFoundException{ // FileNotFoundException extends IOException
		
	}
}

This can work fine, but if we let TestExceptionSub to implement an Interface, like:

package scjp.liguoliang.com;

import java.sql.SQLException;

public interface ITestException {

	void test() throws  SQLException;
}
package scjp.liguoliang.com;

import java.io.FileNotFoundException;
import java.io.IOException;

public class TestException {
	public void test() throws IOException{
		System.out.println();
	}

}

class TestExceptionSub extends TestException implements ITestException{
	public void test() throws FileNotFoundException{ //Can not compile!
		
	}
}

Multiple markers at this line
    – overrides scjp.liguoliang.com.TestException.test
    – Exception FileNotFoundException is not compatible with throws clause in
     ITestException.test()

 

for test() in TestExceptionSub , If we want to throw any checked exception, we need get the sub class from ‘IOException’ and ‘SQLException’., or just remove throws declaration.

but for un-checked exception, you can throw any, and no matter with the super class declaration.

4. Catch sequence: the specification exception need catched before it’s super exception, otherwise:
“Unreachable catch block for Exception. It is already handled by the catch block for Throwable”

	public static void main(String[] args) {
		try{
			throw new SQLException();
		}catch (SQLException e) {
			System.out.println("SQLException catched!");
			return;
		}catch (Throwable e) {
			System.out.println("Exception Catched!");
			return;
		}
		finally {
			System.out.println("Finally");
		}
	}

Overloading and overriding method call

Categories: Java; Tagged with: ; @ May 15th, 2012 21:11

Overloading method will be determined by reference type, but override method will be determined by the actual object tyep.
property is determined by reference type too.

package newjob.guoliang.corejava;

public class ReferenceTypeTest {
	public static void main(String[] args) {
		AClass a = new BClass();
		new ReferenceTypeTest().printClassDesc(a);
	}
	
	protected void printClassDesc(AClass a) {
		System.out.println("A_" + a.desc);
	}
	
	protected void printClassDesc(BClass b) {
		System.out.println("B_" + b.desc);
	}
	
}

class AClass {
	String desc = "ClassA";
}

class BClass extends AClass{
	String desc = "ClassB";
}

Output is:

A_ClassA
MethodClassB_ClassB

Java method invoked sequence

Categories: Java; Tagged with: ; @ May 15th, 2012 20:51

Static block / variable;

Instance block / variable;

Constructor;

public class StaticTest {
	public static void main(String[] args) {
		// System.out.println(B.a);
		// System.out.println(C.c);
		StaticTest staticTest = new StaticTest();
		staticTest.printName();
	}
	
	static {
		System.out.println("Static_block_Main");
	}
	
	
	public StaticTest() {
		System.out.println("Consturctor_Main");
	}
	
	private void printName() {
		System.out.println("InstanceMethod");
	}
	{
		System.out.println("Instance_block_Main");
	}
	
}

Output is:

Static_block_Main
Instance_block_Main
Consturctor_Main
InstanceMethod

About Java static block

Categories: Java; Tagged with: ; @ May 15th, 2012 0:06
package scjp.liguoliang.com;

/**
 * I'm try to see the static block run rules,
 * B extends A, A has a static a, when we use B.a, class B did not loaed, just Class A loaded.
 * If B also has a static a, A and B will be loaded.
 * If a is finle, A and B will not be loaded both.
 * @author Li Guoliang
 *
 */
public class TestClassLoading {
	public static void main(String[] args) {
		System.out.println(B.a);
		System.out.println(C.c);
	}
}

class A {
	static String a = "A.a"; // how about if this is final
	static {
		System.out.println("Static_A_Class");
	}
	
	{
		System.out.println("Instance_A");
	}
}

class B extends A{
	// static String a = "B.a";
	static {
		System.out.println("Static_B_Class");
	}
}

/**
 * This class is designed for test the squence of static variable and block. 
 * They run by the sequence of the code.
 * @author Li Guoliang
 *
 */
class C {
	
	static {
		System.out.println("Static_C");
	}
	static C c = new C();
	
	public C() {
		System.out.println("Constructor_C");
	}
}

Output

Static_A_Class
A.a
Static_C
Constructor_C
scjp.liguoliang.com.C@161f10f

Newer Posts <-> Older Posts



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.