Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Sunday, May 3, 2009

Seeking in PrintStream

I played with maven today. I found the following output message when maven is downloading something. The xxx changes to indicate the download progress.

xxx/yyyK

I am curious about how this is done. As far as I know, System.out is PrintStream. In PrintStream, there is no method to seek. I searched for the answer on the web. Unfortunately, I can't find the answer. I downloaded the maven source code. After some browsing, I found that printing \r will position the following printings at the beginning of a line. The following Java code shows how it is done.

import java.io.PrintStream;

public class Print {

public static void main(String[] args) {
PrintStream ps = System.out;
ps.print("100");
sleep2s();
ps.print("\r200");
sleep2s();
ps.print("\r300");
}

private static void sleep2s() {
try {
Thread.sleep(2 * 2000);
} catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
}
}

I think that Javadoc should have documented this kind of usage, which can save developers a lot of time to scratch their heads.

Sunday, February 22, 2009

Java's file.encoding property on Windows platfor

This property is used for the default encoding in Java, all readers and writers would default to using this property. file.encoding is set to the default locale of Windows operationg system since Java 1.4.2. System.getProperty("file.encoding") can be used to access this property. Code such as System.setProperty("file.encoding", "UTF-8") can be used to change this property. However, the default encoding can be not changed dynamically even this property can be changed. So the conclusion is that the default encoding can't change after JVM starts. java -dfile.encoding=UTF-8 can be used to set the default encoding when starting a JVM. I have searched for this option Java official documentation. But I can't find it.

Friday, May 2, 2008

Genenared methods values and valueOf for Enum

I have recently tried Java Enum when doing software development. The following code is an example:

public Enum FooType {
ABC, DEF;
}

I found two methods valueOf and values in the Javadoc for my enum code. I did not write these 2 methods. And it's superclass java.lang.Enum<FooType> also does not have these 2 methods. Then I checked JLS 3.0. 8.9 Enums says these 2 methods are automatically generated. It is a little magic. One Java class can has methods which do not exist as source code. Too many magic which do not have a consistent rationale can make a language hard to learn and use.

Saturday, April 12, 2008

Hiding and overriding in Java

For some technical terms in this post, please refer to JLS 3.0. And the discussion is not very precise. I have looked for some explicit rules governing this topic in JLS. But I failed. So I am just trying to summarize my personal understanding. Any feedbacks are welcomed.

In the following discussion, A and B are used. Type A extends type B (type means class or interface).

Field


Only hiding applies tojava fields. Hiding happens in the following situation.Both A and B have a variable named as var. And var is of the same kind of variables ( static fields or instance fields).
Java allows a variable belonging to different variable kinds appears in both A and B. But hiding does not happen in such situations.

Method


Method m1 in A . Method m2 in B.

  1. m1 is subsignature of m2

  2. m1's visibility is no less than m2

  3. Both m1 and m2 are instance methods. Or both m1 and m2 are static methods. Otherwise, there will some compile errors.


Overriding


Overriding applies to instance methods.

Hiding


Overriding applies to static methods.

Saturday, April 5, 2008

ClassNotFoundException problem with rmiregistry

It is very possible that you will get ClassNotFoundException when you try Getting Started tutorial for RMI in JDK documentation. There is a easy way to fix it. Start rmiregistry and the server implementation in the same command line console on Windows (assuming that the current directory is the root directory of the class file tree for the example).
  • start rmiregistry

  • java example.hello.Server

The CLASSPATHs for both rmiregistry and java example.hello.Server must contains the path for the class file tree for the example if you want to run them from different command line console.

I have run into this problem twice. And JDK documentation does not explicitly specified. I searched the web. Remote Method Invocation (RMI) - RMI server ClassNotFoundExceptionsolves my problem.

So documentation is vital to software quality. Good documentation can save developers a lot of precious time.

Monday, March 24, 2008

Working Directory

To decide the working directory for a process is difficult. I have found a entry in wikipedia for working directory. And I have added some rules for deciding working directory in the wikipedia entry Working directory. You can refer to it for details.

For this reason, we should always try to use pathname relative to classpath instead of working directory in Java.