It seem great. But I am not sure first year is free unlimited? how about second year?
Author: fyhao
Java Tips on Array / List / Set / For Each Loop
This is a basic Java Tips on when and how to use Array, List, Set, and for each loop.
When you have a set of data you want to store, you can use Array / List / Set.
Array is a fixed size structure, meant, when you initialise Array to be size of 10, then you cannot alter the size already.
To declare an Array, you type:
String[] someArray = new String[3];
someArray[0] = "one";
someArray[1] = "two";
someArray[2] = "three";
List is a variable size structure, meant, you can add and remove items without declare its size yet.
To declare a List, you type:
List
someList.add("one");
someList.add("two");
someList.add("three");
You may want to convert Array into List, you do
String[] someArray = new String[3];
List
You may want to convert List back to an Array, you do
List
String[] someArray = someList.toArray();
Set is an unique, non repeated version of List. For example, using List, you can store [1,2,3,2,1], as 2 repeated 2 times. But, Set, you always cannot repeat the items.
The most common implementation is HashSet, you do
Set
someSet.add("one");
someSet.add("two");
someSet.add("three");
someSet.add("one");
Note, in the output, you only can find [“one”, “two”, “three”]
And you may not see “one”,”two”,”three” in insertion order. Actually, Java will hash each item, by converting the String into a hash key, then store inside the HashSet, so you may see “two”, “one”, “three” but not “one”, “two”, “three”.
If you want the Set in insertion order, you should use LinkedHashSet.
Set
someSet.add("one");
someSet.add("two");
someSet.add("three");
someSet.add("one");
Then you will see “one”, “two”, “three”.
If you want the Set in ordered, you can use TreeSet. Everytime you insert an item, it will auto sort.
Set
someSet.add("one");
someSet.add("two");
someSet.add("three");
someSet.add("one");
The output will be “one”, “three”, “two”. Or more better example,
Set
someSet.add(3);
someSet.add(2);
someSet.add(1);
someSet.add(3);
The output will be 1,2,3.
To iterate the array, list, set, you can use for each loop construct.
OK, normally, you may already know, something like this
String[] someArray = new String[3];
for(int i = 0; i < someArray.length; i++) {
someArray[i]
}
You can do this:
String[] someArray = new String[3];
for(String item : someArray) {
item
}
This is same applies to List and Set.
For example:
List
for(String item : someList) {
item
}
Sometimes, good to use Array, sometimes, good to use List / Set.
If using List and Set, you can do something like add(), remove(), contains().
For example, if you want to filter something, you can use contains(), you can check if the List “contains” the item you want to search.
someList.contains("some thing");
Finally, you can bookmark my blog to follow up more Java Tips posted in future.
Pharrell Williams – Happy (1AM) – YouTube
Pharrell Williams – Happy (1AM) – YouTube
Happy…
Java Tips String comparison
A Java tips during String comparison. We must use .equals() instead of ==.
Using ==, it checks for reference equality.
Using .equals(), it checks for the value equality.
For example,
String a = "YongHao";
String b = "YongHao";
System.out.println(a == b);
System.out.println(a.equals(b));
OK, if you run above code, you will see true true.
But,
String a = new String("YongHao");
String b = new String("YongHao");
System.out.println(a == b);
System.out.println(a.equals(b));
You will get false true.
In first case, a = “YongHao” and b = “YongHao”, as Java will perform String interning by default. String interning meant create a single copy of String and stored in String Constant Pool. So a and b will point to same object in this case, so does a == b is true. String constant pool is fixed size. The reason to create String constant pool is to save space and memory. Note, this is not always guarantee String interning will happens, this is based on JVM setting.
In second case, new String(“YongHao”) guarantee always new object, a and b points to different objects, so a == b return false, but a.equals(b) return true, which is expected.
Conclusion, in Java, when compare String, KEEP IN MIND, always use .equals()
JVisualVM and Eclipse Memory Analyzer a Java Dev Tools
JVisualVM is an open source software to analyze CPU Usage / Memory Usage in real time of a JVM application.
Eclipse Memory Analyzer is an open source software to analyze memory leaks. By passing the Java heap dump this tool will help to pinpoint where is the memory leaks and producing the report which aids developers to fix the bugs.
I am experienced using Eclipse Memory Analyzer along with JVisualVM in a production environment. Last time the production server will went down after running one weeks without any signs. We couldn’t find any clues how the system went down. We then used JVisualVM to monitor the JVM based application server. We got the memory and CPU usage in real time. Then when the server was went down, we quickly open JVisualVM and generate heap dump file. We then passed this heap dump feed into Eclipse Memory Analyzer. After that, Memory Analyzer will tell us where is the memory leaks, which Java object/instances eating up most spaces, then we know how to solve the problem. Finally, after the fix, no more server went down issue.
Additional note, I experienced solved a memory leaks issues on an Apache Tomcat. Technically, in order to enable JVisualVM Monitoring on Apache Tomcat, we need to enable JMX Port in Apache Tomcat conf/server.xml setting. Then open JVisualVM to connect to this JMX Port. Enable JVisualVM Monitoring via JMX on production server is safe as this will not degrade server performance. Same can applies to other JVM based application. This is proven by industry standard. So I strongly recommends.
JVisualVM: http://docs.oracle.com/javase/6/docs/technotes/tools/share/jvisualvm.html
Eclipse Memory Analyzer: http://www.eclipse.org/mat/
Feel free contact me if you need any expertise suggestions.
New in iOS 8: Everything an iOS Dev Needs to Know
New in iOS 8: Everything an iOS Dev Needs to Know
Just added this article into my reading list. My reading list getting longer and need to find time to clear it.
Link: http://feeds.dzone.com/~r/javalobby/frontpage/~3/pVp_dWfX9wo/new-ios-8-everything-ios-dev
Java utility to get class version compiled for
Want to check out what is the Java class version compiled for, here comes several ways.
1) javap tool comes along with JDK/JRE
Assume you have a Test.java source code with compiled version of Test.class.
Open your terminal, type
In Unix:
javap -verbose Test | grep "version"
In Windows:
javap -verbose Test | find "version"
Note down Major version number.
Refer to this table:
| Major | Minor | Java Platform Version |
| 45 | 3 | 1.0 |
| 45 | 3 | 1.1 |
| 46 | 0 | 1.2 |
| 47 | 0 | 1.3 |
| 48 | 0 | 1.4 |
| 49 | 0 | 1.5 |
| 50 | 0 | 1.6 |
| 51 | 0 | 1.7 |
| 52 | 0 | 1.8 |
2) DataInputStream
There is another clever guy on StackOverFlow just read Java byte class into DataInputStream, and getting the magic number directly yield the minor and major. I copied the code here.
DataInputStream in = new DataInputStream(new FileInputStream(filename));
int magic = in.readInt();
if(magic != 0xcafebabe) {
System.out.println(filename + " is not a valid class!");;
}
int minor = in.readUnsignedShort();
int major = in.readUnsignedShort();
System.out.println(filename + ": " + major + " . " + minor);
in.close();
3) Unix od
Just below the Java code, other guys found a Unix command line to get the magic number.
od -x HelloWorldJava.class |head -2
I ran this code on my box I found:
od -x Test.class | head -2 0000000 feca beba 0000 3400 0f00 000a 0003 070c 0000020 0d00 0007 010e 0600 693c 696e 3e74 0001
feca beba is a magic number, 0000 3400 represents Java SE 8.
Check it out my Java version:
java -version
java version "1.8.0_05" Java(TM) SE Runtime Environment (build 1.8.0_05-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)
What’s use for this check?
I experienced to maintain some legacy code on my box, with my Eclipse set to higher Java JDK version, when deploy to production server it encountered error showing something like “unrecognized class file version”. By using this check I can check the existing java class version on the server, and then go back to my Eclipse and adjust up/down (most probably down) my Java JDK version and compile a new one against it.
少年神探狄仁杰
前几天开始在 Youtube 看少年神探狄仁杰。这个是唐高宗和武则天时代的破案高手。
之前我都看包公的,旧版新版和少年版包公都看完了。狄仁杰则是第一次看,也是先直接看少年版的。
我上网搜索比较狄仁杰 vs 包公看谁比较厉害,多数人都觉得狄仁杰比较厉害。一,包公每次都要问公孙策,而狄仁杰可以自己破案。二,包公不会武功,狄仁杰会武功而且文武双全。三,包公虽然有王朝马汉那班人,但是狄仁杰当官的时候跟的人更多。四,狄仁杰的官衔比包公高。
最后,这部戏呢,太多儿女私情了,少一点应该会比较好。
The LLVM Compiler
The LLVM Compiler Project is a collection of modular and reusable compiler and toolchain technologies. The primary sub projects are LLVM Core, Clang, and 11 more.
LLVM has three important elements: front end, optimizer, back end.
Front end can be any languages.
Optimizer is a target independent intermediate language.
Back end can be any target system architecture, like x86_64.
I came approach to this tutorial, step-by-step approach to implement a simple functional language Kaleidoscope showing how fun and easy of LLVM. By following tutorial we can learn how to build a lexer, parser / AST, code generation to LLVM IR, adding JIT and Optimizer support, extending language with conditional flow (SSA construction), extending language with user defined operators (very significant), mutable variables.
Because LLVM is target independent, meant, we can implement any source language as input, convert to LLVM IR, and then output to any target architecture like Windows/Linux 32/64 bit, makes our languages portable.
Other than these, some keywords scanned. SSA and CPS. SSA, Static Single Assignment Form is a property of an intermediate representation (IR), which requires that each variable is assigned exactly once, and every variable is defined before it is used. In the other hand, CPS, Continuation-passing style is a style of programming in functional language by explicitly passing control in the form of continuation. But need to be careful as using CPS without tail call optimization will result call stack being grown and leaked. Tail Call Optimization is a compiler specific action to optimize tail call structure, which tail call is a subroutine can be performed as the final action of procedure. The procedure said to be tail-recursive when the subroutine call itself at the end, recursive function. Refer to Tail recursive in Erlang in my previous post.
I forced myself to read every single piece of information of this tutorial although it seem quite hard but yet valuable for knowledge.
Update:
10 August 2014 12:17pm, LLVM Publication, A Compilation framework for lifelong program analysis & Transformation. < http://llvm.org/pubs/2004-01-30-CGO-LLVM.pdf>
Mashape API Interface Down?
Today I spot check my Mashape API Analytics Usage on Mashape API Platform. Check out the link here. I felt really down when I see the traffic goes down to zero. I quickly control my tears log in to my several back end servers and confirmed no issue, working perfectly. But I tried to click “Test Endpoint” on Mashape API hub for my API, it showed no responsive.
I referred to Endpoint definition, found something strange. Under first box, the URL something like http://elb-proxy… this is an Amazon URL, they change to Amazon URL as a proxy. Previously is https://text-sentiment.p.mashape.com/analyze. They are not testing properly… Directly change in production.
And I manually run the CURL command line in my terminal, you know what happen?
It complained me to use HTTPS protocol. Eh, you changed to Amazon URL why don’t use HTTPS. I quickly test another people API, also got this problem, so, Mashape you tricks us.
Guys, please use the old API interface confirmed still working.
curl -X POST --include "https://text-sentiment.p.mashape.com/analyze" \
-H "X-Mashape-Host: text-sentiment.p.mashape.com" \
-H "X-Mashape-Key: Ri4j5gX4ORmshweHbjBSUUMevXWIp1i0xRujsnjCz7wW9w5zLB" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "text=I am not really happy"
Alternatively, please visit my Runnable example code, also working. Link: http://runnable.com/U80nxyBJjrp4wcOH/text-sentiment-analysis-for-node-js-and-mashape
I had inbox Mashape Guys to look into this issue matter.
Updated: 9 August 2014 4:37pm SGT Time checked the issue is solved


