Showing posts from April, 2012

Enabling Wireless Problem in Linux

On linux; after disabling the wireless network connection from network manager, enabling wireless connection through user interface may not be executed. In order to solve the problem, rfkill should have been installed on your system to recover wireless connection.

"rfkill list" command will list status of each wireless connection devices; with unblock parameter, you can enable a wireless connection adapter which is blocked soft or hard.

user@dell:~$ rfkill list 0: dell-wifi: Wireless LAN Soft blocked: yes Hard blocked: no 1: dell-bluetooth: Bluetooth Soft blocked: no Hard blocked: no 2: phy0: Wireless LAN Soft blocked: no Hard blocked: no 3: hci0: Bluetooth Soft blocked: no Hard blocked: no user@dell:~$ rfkill unblock 0
"rfkill event" command listen wireless switch button actions and print out current status as follows. I highlighted switch off event outputs with red, below.

user@dell:~$ rfkill event 1335774980.907617: idx 0 type 1 op 0 soft 0 hard 0 133…

"Is Given String Numeric" Control with Java Regexp

This morning, i want to blog a fundemantal issue of regular expressions: "Is given string Numeric?". This seams to me so reasonable to implement and note it here for future use.

In order to grab numbers from texts, [0-9]+ inclusive range can be simply used. However; what about minus-plus signs and '.' seperator for floating numbers. Things are getting a bit more complex, in this point. It is still easy by using iterative approach to support each rule. Lets list the rules:
A number contains [0-9], -, + and . charactersA number can have only one sign(- or +) prefixA number can be floating digits (includes '.' precedded and followed by digits) Take the first step into first rule, and try following pattern.Without any other case that defines in more detail; this must satisfy the rule of being a number. This regexp will fail to evaluate this text ("-.+") to true; and obviously it is not a number. Forget it...

Lets examine the second rule; it d…

Why Multiple Locks Allowed on a Same Monitor?

In Java language, any class instance is associated with a monitor that is used to lock-unlock objects by threads for synchronization.While only one thread at a time is holding a lock on an object's monitor, other threads are blocked until they can obtain a lock on that monitor. It is also legal to lock an object multiple times that each of unlock action will make the state of monitor reverse. But why do we lock an object multiple times?

Initially, i couldn' t grasp the reason of allowing multiple locks on a montior. Since the JVM guarantees that other threads will be blocked after holding lock on the montior. It seamed to me really useless but it obviously prevents compiler from controlling nested synchronized blocks' lock objects.

In addition to that, next locking attempts of a thread that already holding lock of same monitor is harmless for other threads. Beacuase each nested synchronized code blocks must be finished before the top level synchronized code block release…

Compiling Source Files with JAV Extension Myth in Java Language Spec 3.0

I' ve read something interesting about java source file extensions at Top level type declarations section in java langugae specification 3.0.

When packages are stored in a file system (§7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true: • The type is referred to by code in other compilation units of the package in which the type is declared. • The type is declared public (and therefore is potentially accessible from code in other packages).
While in this paragraph is mentioning about top level type class declarations and source file naming conventions; it is pointing that the java source files could be with file extensions .jav. Immediately, i tried with a sample demo class with name Demo.jav.

public class Demo { @Override public String toString() { return super.toString() + "…

Assertions in Java

Assertion is a feature of Java that enables to have contract compatibility controls and assumption tests which can be simply enabled in any time for problem tracking in code. Assertions can be simply implemented and makes easy to find the cause of problems, if it is properly used in error prone points of code.

Method parameters has specific expectations and limitations in API documentation. Thus, assertion with method parameters are generally used to detect problems with appropriate expressions. For example:

public class XFileReader { public XFileReader(String fileName) { // fileName cannot be null or empty string assert fileName != null || fileName.equals("") : "XFileReader cannot be initiated with fileName="+fileName; // ... constructing XFileReader } // ... file reading related code goes here public String readLine(int lineNumber) { // simple assertion without an expression, // only java.lang.AssertionError with related code l…

Final Members and Inlining in Java

Final is a keyword of Java language that can be applied to classes, methods, method variables and class attributes. Briefly, I will list usage and purpose of final keyword with each member.
A class definition with preceding a final keyword indicates that this class cannot be super class of any class. This rule is also applied to member classes, nested classes and local classes.A final method definition tells the compiler that this method cannot be overriden by any subclass. A final attribute in a class cannot be reset after its initialization. This attribute is constant in life cycle of any class instance.A final variable inside a method is constant and cannot be changed after it's set to initial value. Inspecting these rules makes me think that final keyword is used for disabling inheritance and defining constant values. I also infer that final keyword is absolutely opposite of abstract keyword; so in any case they cannot be used together for a same member.

Inlining Members of Par…

Casting and instanceof clause in Java

In java, an instance of a class can be cast to parent class references implicitly (a.k.a. upcasting). Casting to subclass references must be explicitly declared (a.k.a. downcasting). Downcasting is safe when an instanceof control of the reference and target class type is implemented before casting.

There are some reasonable restriction rules that lead developers to write sensible code while using instanceof clause in java.
A class reference can only be checked against parent classes, sub-classes or own class.A class reference can be checked against any interface.An interface reference can be checked against any class. Other cases, out of scope of these rules will cause compilation error. The reason of first rule is preventing developer from coding any impossible instanceof compares with comparing an instance of a class with any unrelated types in O-O hierarchy tree. The second and third rules are the result of possibility that a class can extend one class but implements many interfaces…

Constructors and Static-Instance Code Blocks Run Order in Java

When I deal with an objective of SCJP, I always prefer writing a sample code that is basic enough to show compile time and runtime behaviors and problems. After that, purposely, some modifications helps me to understand the idea behind the code.

While preparing SCJP 6.0, one of the most challenging and easy to understand objective is run order of static-instance code blocks and constructors in inheritance tree. Since it requires good understanding of the concept of class loading, and how classes are created when calling their constructors. I developed the sample code below to infer run order from it.

I wrote two static inner classes A and B (B is subclass of A) inside StaticInstanceBlocksRunOrder class that contains the main method to test. Both of static inner classes has two static and two instance code blocksthat helps to understand the runtime execution order of static-instance code blocks.Inheritance between A and B will show the runtime order of constructors and relations bet…