Parallel Streams

May 23rd, 2016

A Note On Java EE Testing

May 20th, 2016

Java 8 resources

December 4th, 2015

IIOP standalone EJB3 client lookup to Glassfish 3.1.2

May 18th, 2012

Setup to run a standalone EJB client and lookup to a Glassfish running EJB 3.1 stateless ejb.

  • Install Glassfish plugin in Eclipse.
  • Create a Enterprise Aplication Project using Glassfish server as the target runtime.
  • Create a HelloWorld example:
public interface HelloWorld {}

public class HelloWorldEjb implements HelloWorld {}
  • Create a Application Client Project using Glassfish server as the target runtime.
  • Add gf-client.jar to the project classpath. gf-client.jar is included on the glassfish plugin folder inside eclipse, or in the glassfish standalone installation.
  • Create the plain java code as the following:
InitialContext ctx;
HelloWorld ejbRef = null;
try {
   ctx = new InitialContext();

   ejbRef = (HelloWorld) ctx.lookup("eu.pedrocardoso.HelloWorld");


} catch (NamingException e) {


  • Optionally we can give properties to the initial context:
Properties p = new Properties();

Default Glassfish iiop port is 3700

Want Better Estimates? Stop Estimating!

April 4th, 2012

Just to share a presentation about good estimation.

Video and slides:

SCJP – practice results

July 14th, 2011

The results of my practice exams. Not included the mocks, questions that I did in several sites pointed by JavaRanch.

From OCP Java SE 6 Programmer Practice Exams book (more SCJP resources)

  • Assessment Test 1 - 71%
  • Assessment Test 1 - 29%
  • Exam 1 - 35%
  • Exam 2 - 50%
  • Exam 3 - 42%
  • Exam 4 - 58%
  • ExamLab Diagnostic Exam - 65%
  • Exam Lab Exam 1 - 59%
  • LearnKey Master Exam 1 - 65%
  • LearnKey Master Exam 1 - 70%
  • Oracle online practice exam - 87%
  • OCJP exam: 85% PASS

scjp 1st june change to Pearson VUE

June 29th, 2011

Since 1st June the SCJP, now OCJP, the exam (Exam Number : 1Z0-851) have 60 questions, 150 minutes duration and you have to pass in 61% of the questions (37).

Seems that there are no drag and drop questions.

All answers here:

From Oracle blog:

The exam objectives and the exam questions themselves did not change.

  • For three Java exams, some lengthy (time-consuming) questions were removed & replaced with shorter (less time-consuming) questions. This was done in order to shorten the required exam time (to 150 minutes).
  • Some interactive question types were removed from several Java and Solaris exams (including "matching" and "drag-and-drop" questions).

SCJP – Java Enums

June 1st, 2011
  • enum CoffeeSize { BIG, HUGE, OVERWHELMING };
    • CoffeeSize cs = CoffeeSize.BIG;
  • enum CoffeeSize { BIG, HUGE, OVERWHELMING }; // <--semicolon  is optional here


  • CoffeeSize.values()  // returns a Collection of CoffeeSize enums. A Collection, not an array


  • Syntax example of a enum usage
public class WeatherTest {
   static Weather w;
   public static void main(String[] args) {
       System.out.print(w.RAINY.count + " " + w.Sunny.count + " ");
enum Weather {
   RAINY, Sunny;
   int count = 0;
   Weather() {
      System.out.print("c ");
  • All of an enum’s values are initialized at the same time, and an enum’s variables are treated as if they were instance variables, not static variables


  • Enum declaration
    • Can declaring an enum outside a class
    • Can declaring an enum inside a class
    • Cannot declare enums in methods


  • enum really is a special kind of class
    • You can add constructors, instance variables, methods,
    • and something really strange known as a constant specific class body.
  • Constant specific class body: looks like an anonymous inner class for one enum value
    • OVERWHELMING(16) { body: methods override }


  • == or equals on enums values evaluates true in both cases.
  • The member variables declared inside an enum is by default static.


  • You can NEVER invoke an enum constructor directly
    • The enum constructor is invoked automatically. For example, BIG(8) invokes the CoffeeSize(int) constructor.
  • You can define more than one argument to the constructor, and
    • you can overload the enum constructors, just as you can overload a normal class constructor.

SCJP – New Java 6 features

May 20th, 2011

Main classes and methods

  • nextLong()
    • Scans the next token of the input as a long. This method will throw InputMismatchException if the next token cannot be translated into a valid long value as described below. If the translation is successful, the scanner advances past the input that matched.
  • hasNextLong
    • Returns true if the next token in this scanner's input can be interpreted as a long value in the default radix using the nextLong() method. The scanner does not advance past any input.
  • findInLine(Pattern pattern)


  •  System.console()
  • readLine(String format, String args) - returns a string with the line read.
  • Invoking close() on the objects returned by the reader() and the writer() will not close the underlying stream of those objects.
  • methods doesn't throw checked exceptions
  • console.reader().close();  //close method throws IOException
  • Invoking close() on the objects returned by the reader() and the writer() will not close the underlying stream of those objects.
  • Scanner.nextLong returns the next long, but doesn't advance to the the input if it's not a long.

NavigableSet / NavigableMap

  • It's a new interface (extends SortedSet / sorted Map) for Java 6.

SCJP – Java I/O

May 5th, 2011

Some diagrams and tables in this post from SCJP Sun Certified Programmer for Java 6 book.


File The API says that the class File is "An abstract representation of file and directory pathnames." The File class isn't used to actually read or write data; it's used to work at a higher level, making new empty files, searching for files, deleting files, making directories, and working with paths.
FileReader This class is used to read character files. Its read() methods are fairly low-level, allowing you to read single characters, the whole stream of characters, or a fixed number of characters. FileReaders are usually wrapped by higher-level objects such as BufferedReaders, which improve performanceand provide more convenient ways to work with the data.
BufferedReader This class is used to make lower-level Reader classes like FileReader more efficient and easier to use. Compared to FileReaders, BufferedReaders read relatively large chunks of data from a file at once, and keep this data in a buffer. When you ask for the next character or line of data,it is retrieved from the buffer, which minimizes the number of times tha ttime-intensive, file read operations are performed. In addition, BufferedReader provides more convenient methods such as readLine(), thatallow you to get the next line of characters from a file.
FileInputStream A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment.
FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
ObjectInputStream An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream.
FileWriter This class is used to write to character files. Its write()methods allow you to write character(s) or Strings to a file. FileWriters areusually wrapped by higher-level Writer objects such as BufferedWriters orPrintWriters, which provide better performance and higher-level, moreflexible methods to write data.
BufferedWriter This class is used to make lower-level classes likeFileWriters more efficient and easier to use. Compared to FileWriters,BufferedWriters write relatively large chunks of data to a file at once,minimizing the number of times that slow, file writing operations are performed. The BufferedWriter class also provides a newLine()method to create platform-specific line separators automatically.
PrintWriter This class has been enhanced significantly in Java 5. Because of newly created methods and constructors (like building a PrintWriter witha File or a String), you might find that you can use PrintWriter in placeswhere you previously needed a Writer to be wrapped with a FileWriter and/ora BufferedWriter. New methods like format(), printf(), and append()make PrintWriters very flexible and powerful.
Console This new, Java 6 convenience class provides methods to read input from the console and write formatted output to the console.
FileOutputStream A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.
ObjectOutputStream An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream. If the stream is a network socket stream, the objects can be reconsituted on another host or in another process.


Don't forget to study the io objects constructors:


  • dir.list()  //returns a new String[] with a list of all file and folders in the dir directory;
  • new PrintWriter("myFile.txt");
    • When the constructor is called, it truncates the existing file to zero size.
  • File f1 = new File("sub1");;     f1.mkdir();
    • if the file exists doesn't throw a exception. It returns a boolean indicating whether or not a new
      directory was made
    • Most of interfaces of returns boolean to indicate error or success.
  • File f3 = new File(f1, "sub3");;   PrintWriter pw = new PrintWriter(f3);
    • Creates a empty file, on the f1 folder, but only during the PrintWriter constructor.
  • f.renameTo() method takes only anothe File as arg, not a string.