Java read a file line by line – How Many Ways?

Processing a text file line by line is a common thing programmers do. There are many related classes in the Java I/O package and this may get confusing. This post shows 4 different ways of reading a file line by line in Java. 1. FileInputStream and BufferedReader private static void readFile1(File fin) throws IOException { … Read more

Java: get a set of random numbers from a specified range

This is a method that accepts a range (max) and a number (n), returns an array of n random numbers. n: size of random number array max: largest available number (exclusive) For example: Input: n=5, max=100 Output: an array containing 5 random numbers whose range is from 0 to 99. public static HashSet<Integer> getNRandom(int n, … Read more

Linux Process Programming – fork()

What is a process?

In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently.

Read more

How Compiler Works?

How compiler works is not easy to explain in one sentence. The best way is to use an example. A compiler is a computer program that transforms source code written in a high-level programming language into a lower level language. Basically, a compiler consists the following phases: Lexical Analysis, Syntax Analysis, Semantic Analysis, IR Generation, … Read more

Java Design Pattern: Observer

In brief, Observer Pattern = publisher + subscriber. Observer pattern has been used in GUI action listener. Swing GUI example shows how action listener works like an observer. The following is a typical example about head hunter. There are two roles in this diagram – HeadHunter and JobSeeker. Job seekers subscribe to a head hunter, … Read more

An example of C++ dot vs. arrow usage

vs. Overall, dot(.) is for object itself, arrow(->) is for pointer. A good example worth a thousand words. The following example should be a good one. #include <iostream>   using namespace std;   class Car { public: int number;   void Create() { cout << "Car created, number is: " << number << "\n" ; … Read more

Eclipse JDT tutorial: Parse a Java method by using ASTParser

This tutorial belongs to Eclipse JDT Tutorial Series. ASTParser can use its setKind() method to set source to be parsed as a compilation unit, single expression, a sequence of statements, or a sequence of class body declarations. parser.setKind(ASTParser.K_COMPILATION_UNIT);parser.setKind(ASTParser.K_COMPILATION_UNIT); Any of those four can not handle a single independent method. This is not pleasant. Following the … Read more

Eclipse JDT Tutorials

JDT is supported by 3 pillars: Java Model, Search Engine, and AST. It is a handy tool for manipulating Java source code. However, the handy tool comes with a steep learning curve even for an experienced Java developer and there are just not enough proper code examples. This page summarizes the code examples for using … Read more