Java Clone() explanation

Read A Few Notes About Java clone() Method, and my explanation to why Java clone() acts like that is being here:

1) Java clone will clone a new instance without calling constructor, as constructor is used to used in initialisation of construction of object, and the value of object may changed later, if run constructor again during clone, sure will got problem, so by initial design the clone will not call constructor.


Apache Mahout A Machine Learning Library

Apache Mahout which is a Java based machine learning library released under Apache License.

Currently Mahout supports mainly four use cases: Recommendation mining takes users’ behavior and from that tries to find items users might like. Clustering takes e.g. text documents and groups them into groups of topically related documents. Classification learns from exisiting categorized documents what documents of a specific category look like and is able to assign unlabelled documents to the (hopefully) correct category. Frequent itemset mining takes a set of item groups (terms in a query session, shopping cart content) and identifies, which individual items usually appear together.

Learn more at

Have a QuickStart at

Java knowledge about Atomic Operation of Increment Operator

An atomic operation is an operation which is performed as a single unit of work without the possibility of interference from other operations.

A Java question and knowledge transfer about Atomic Operation.

Refer to the following code:

public void someMethod() {
int a = 1;

Normally the final result of variable a is 2, but the value can be other possible such as 3 or 4. Why?

It is because in Java the language specification guarantees that that reading or writing a variable is atomic (unless the variable is of type long or double). Long and double are only atomic if they declared as volatile.

The operation a++ it not atomic in Java for the standard variables (int, long, etc). It first reads the value which is currently stored in a (atomic operations) and then it adds one to it (atomic operation). But between the read and the write the value of a might have changed.

Bringing Node.JS WebSockets with Java

To add real time capability with Java, what you can do is through Comet (CometD Framework) or Jetty Continuations, however, these technologies had outdated, made worse performance. And currently most web server framework is based on thread-based, and thread-based does not scale when introducing in real time.

This post introduced us to build another separate server running Node.JS WebSockets, and using Redis to bridge Java and Node.JS based on publish/subscribe model, to plug in real time capability to Java. Java can push the data to Redis, when Node.JS subscribed to Redis, whenever got new data, it broadcasts the data to the WebSockets clients.



New Java Certification Question For You Guys

Today, fyhao gives you another Java Certification Question, see if you can answer it right. More practice before you earn a certification.


Develop code that implements all forms of loops and iterators, including the use of for, the enhanced for (for-each), do, while, labels, break, and continue; and explain the values taken by loop counter variables during and after loop execution.

2) Given:

5. public class Buddy {

6.   public static void main(String[] args) {

7.     def:

8.     for(short s = 1; s < 7; s++) {

9.       if(s == 5) break def;

10.       if(s == 2) continue;

11.       System.out.print(s + “.”);

12.     }

13.   }

14. }



What is the result?

a) 1.


b) 1.2.

c) 1.3.4. (*)




g) Compilation fails.


What will happen when you attempt to compile and run the following code?


public class Agg{

static public long i=10;

public static void main(String argv[]){



System.out.println(“no value given”);

case 1:


case 10:


case 5:






A. Compile time error

B. Output of “ten” followed by “five”

C. Output of “ten”

D. Compilation and run-time error because of location of default

Is it simple to you?

Read Java Certification Question Q1 to Q3

Java Certification Question (20111003)


QUESTION: What happens when you attempt to compile and run these two files in the same directory?



package MyPackage;

class P1{

void afancymethod(){

System.out.println(“What a fancy method”);




public class P2 extends P1{

public static void main(String argv[]){

P2 p2 = new P2();





A. Both compile and P2 outputs “What a fancy method” when run.

B. Neither will compile.

C. Both compile but P2 has an error at run time.

D. P1 compiles cleanly but P2 has an error at compile time.

Answer: D

Noticed that these two files said to be in the same directory, but second file didn’t have package declaration while first file have. Therefore, P2 will prompt error at compile time.


What will be the output of the following code?


public class Sample {


public String getCountryName(){

return “USA”;



public StringBuffer getCountryName(){

StringBuffer sb = new StringBuffer();


return sb;



public static void main(String[] args){

Sample s = new Sample();





A. Compile with error



D. Runtime Exception

Answer: A.

Java allows overloading method, meant with same method name, but having different parameters. However, Java does not allow overloading method with different returned types if the parameter list is the same. Because you see, it is contradicted themselves.


What will be the result of attempting to compile and run the following code?


abstract class MineBase {

abstract void amethod();

static int i;


public class Mine extends MineBase {

public static void main(String argv[]){

int[] ar = new int[5];

for(i=0;i < ar.length;i++)





A. A sequence of 5 0’s will be printed.

B. Error: ar is used before it is initialized.

C. Error: Mine must be declared abstract.

D. IndexOutOfBoundes Error

Answer: C.

The answer is C, Mine must be declared abstract, or Mine must implements the method abstract void method().

Java 8 geared up to cloud and its feature

Java SE 8 is geared up to the cloud. Multi-tenancy and modularity is featured.

Multi-tenancy is considered as one of the main components characteristics of cloud computing, as single instance of software application runs on the server but served multiple clients.

Modularity is reorganization of JDK into a set of clearly defined though interdependent modules.

Java 8 also decides to include C# / Scala’s Lambda style which worked pretty well in subjective measure. Java 7 should have Lambda but its construct is weird.

JSR TBD is included as language support for collections, where literal expressions for immutable lists, sets, and maps, and indexing-access syntax for lists and maps.

Below codes are possible when define a Set or List.

List<Integer> aListOfNumber = [1,2,3,4,7,2,4,6,7];

Set<Integer> aSetOfNumber = {1,5,34,45645,23};

Map<Integer, String> aHashMap = {5 : “fyhao”, 6 : “bmwjob”, 18 : “Singapore”};

Virtual extension method is included as providing default implementation for interface virtual methods. If you are not afford an implementation of method, one will providing you an implementation with no charge.

Sharing state often introduce complexity in concurrency programming. If the state is not shared, or said immutable state, it will not brought trouble because it has fixed input-output pair. Functional language comes with this benefit, but Java is not going to switch language but just adopt the principle functional language based on, seek to increase immutability and decrease sharing. Fork-join decomposition introduced in Java SE 7 to easily write algorithm that parallelized automatically across wide range of hardware configurations.

Labeled break in Java

According to Java Tutorials, the Break Statement, break used in Java has two forms: labeled break and unlabeled break.

Unlabeled break

Normally, we knew unlabeled break used in switch-case statement, to break the following execution in the block.


int HOUR = 3;
switch(HOUR) {
case 1:
case 2:
case 3:
case 4:

Or used in for, while, do-while loop, for example:

for(int i=0; i<10; i++) {
System.out.print(i + " ");
if(i == 3) break;

(Which printed: [1 2 3 ])

Labeled break

There is another type of break called labeled break. Besides to use goto statement in Java (you know goto is reserved keyword in Java but it is not supported), we can use labeled break to break the following execution and transfer the flow of control back to the labeled statement immediately.


int input = 3;
validation: {
if(input == 4) System.out.println("pass");
if(input < 4) break validation;
System.out.println("the input is not equal 4 and not smaller than 4");
if(input > 4) break validation;

System.out.println("another end");

It should print:
another end

The program execution will break from the validation block (declared curly braces) and jump to the end followed by the block.

Another example, if you have a nested loop, and you want to break and returns to the outer loop, you can:

cat: {
for(int i=0; i<3; i++) {
for(int j=0; j<5; j++) {
System.out.println(i + " - " + j);
if(j == 3) break cat;

It should print:
0 – 0
0 – 1
0 – 2
0 – 3

In the example 4, the cat block has curly braces surrounds the inner nested-for loop code. But if, we make something like this:

for(int i=0; i<3; i++) {
for(int j=0; j<5; j++) {
System.out.println(i + " - " + j);
if(j == 3) break cat;

In the example 5, the statement followed by label cat is now the nested for-loop without curly brace. Now we said label cat is just for the nested for-loop, if you break or continue in the inner for-loop, it will return the outer for-loop.

The result will be the same as Example 4, but if we change the break into continue, (continue will be valid in this example, but invalid in previous example), it turned into magic, see:

for(int i=0; i<3; i++) {
for(int j=0; j<5; j++) {
System.out.println(i + " - " + j);
if(j == 3) continue cat;

The result will be:
0 – 0
0 – 1
0 – 2
0 – 3
1 – 0
1 – 1
1 – 2
1 – 3
2 – 0
2 – 1
2 – 2
2 – 3


But, this kind of feature is rarely found in most program code. Programmer who worked 16 years experience told us this.

Labeled break provides similar or equivalent feature just like goto do. The goto is considered evil to the program because it makes the code unreadable. That’s why Java is not supported it. But I think there is another reason. But you may see labeled break do just goto do, then why Java supports labeled break but not supports goto. So, there is a reason, they are still different semantically.


x : {
int a = 5;
if(a == 3) break x;
if(a == 5) break y;

y: {
int b = 3;
if(b == 3) break y;
if(b == 5) break x;

In the first X block, if a equal to 5, it will not jump break for Y block, and compiler will also prompt error. Labeled break is not able to break forward, but backward only. That’s mean, if you are inside X block, you only can break from X block or said current block, but not another block. This is the difference between labeled break and goto.

It just provides another flexibility to Java programmer, make code easier in some cases when there is a need to jump out from nested loop, I think this is the most use cases can use labeled break.

Additionally, repeated labeled block with same name is valid. Example:

test : break test;
test : { break test;}
test : {
break test;

It prints “test”.


I think it is valuable knowledge to share with you. Wish happy coding in Java.

Related Links:


SableCC Framework

I have read the article written by James Carman, Write once, persist anywhere, and I have found an useful framework when implementing our own DML, that is using lexer parser provided by SableCC Framework.

By using SableCC, you can just provide a grammar definition, and using SableCC generator tool to generate some classes, then you implement the classes based on your needs. To learn more, visit here for its thesis.