In Java, class instance variables and static variables have default values: null for all object types, false for boolean primitive and 0 for numeric primitives. But local variables inside a method have no defaults.

Consider the following code snippet:
public static void main(String[] args){
java.util.Date d;
System.out.println(d);
}
It will fail to compile with this error:
variable d might not have been initialized
System.out.println(d);
1 error
However, the following will compile and run successfully:
public static void main(String[] args){
java.util.Date d;
}
Because the rule is that all local variables must be initialized before they are first read. So it's perfectly OK to first declare a local variable without initializing it, initialize it later, and then use it:
public static void main(String[] args){
java.util.Date d;
//do more work
d = new java.util.Date();
System.out.println(d);
}
It may be an old habit from languages like C, where you need to declare all local variables at the beginning of a code block. But in Java with this rule in place, it's best to defer declaring local variables till needed.

9

View comments

java.util.Properties class (see Java SE 7 Javadoc) by default assumes ISO 8859-1 character encoding in reading and writing. So when a properties file is in other character encoding, you will see strange characters and behaviors.
This is the simple test to show the values of properties when loading from a properties file. It tries to answer some common questions about properties value:

Do I need to trim whitespaces from a property value loaded from a properties file?

Yes.
This is the error from running a webapp deployed to appserver: java.lang.NoSuchMethodError: javax.xml.parsers.DocumentBuilderFactory.newInstance(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljavax/xml/parsers/DocumentBuilderFactory; The cause: there is an xml-apis.jar under JBOSS_HOME/lib/endorsed dire
You need to create a new file with a file URI like file:/tmp/a.txt, but had this error: java.io.IOException: No such file or directory at java.io.UnixFileSystem.createFileExclusively(Native Method) at java.io.File.createNewFile(File.java:947) at FileTest.main(FileTest.java:7) The directory /tmp (or
When analysing a thead dump, if the thread is created with a custom thread name, we can easily trace it to where the thread pool is created by the unique thread name.
The following java test app runs and terminates normally on Unix but hangs on Windows.
These are the steps to configure hprof profiler in GlassFish 3.x:

1, Identify the target JVM to profile.  In most cases, it's the domain administration (DAS) JVM, but it can be other JVM such as another standalone server instance, or a cluster server instance.
To search by keywords for a command, setting, shortcut, etc, and execute it:

Command + Shift + A

To maximize or full screen (useful when reading long lines).  You can also use the same shortcut to go full screen in FireFox, Safari, Chrome, Preview, iTunes, etc.
These are the steps to configure hprof profiler in JBoss AS 7.x:

1, Identify the target JVM to profile.  In most cases, it's the standalone server JVM, but it can be other JVM such as domain process controller, host controller, or individual server.
JBoss AS7 is secured by default, which means you will have to create users before accessing services and components, such as admin console and remote EJB.

$JBOSS_HOME/bin/add-user.sh, or add-user.bat is the tool for such purpose.
In other Java application servers and previous versions of JBoss AS, there is the concept of common lib, where users can put shared libraries for use by all deployed apps.
A rough comparison of GlassFish 3.x CLI and JBoss AS 7 CLI commands:
Option 1, when the server is stopped, add to $JBOSS_HOME/standalone/configuration/standalone.xml, after the <extensions> element:

<system-properties> <property name="season" value="spring"/> </system-properties>

Option 2, when the server is running, run jboss-cli.sh or jboss-cli.bat command to add
I want to use ant to control deploy and undeploy apps to JBoss AS 7 in a simple test. I know I could just have ant copy the WAR, jar, or EAR files to $JBOSS_HOME/standalone/deployments directory, but then I will have to wait till the app is fully initialized before accessing it.
Jandex is a tool that processes Java annotations within a directory or jar file, and saves the resulting metadata in an index file. This is similar to what a jar file index is to a classloader.
To start standalone server with the default server config (standalone.xml):

$JBOSS_HOME/bin/standalone.sh

To start standalone server on custom, non-default port numbers, using offset=1, 2, 3, etc (negative offset number is invalid).
GlassFish asadmin multimode command runs a series of asadmin subcommands within one single session. The --file option takes a file containing the list of subcommands.
Type (class/interface) visibility can be managed at three levels:

1, at Java language level, a type can be declared as either public, private, or package private.
In previous post, Standalone Java Client for JBoss AS 7.1.1, I showed a hand-written project of standalone client in JBoss AS 7. In this post, I will rewrite it as a maven project with JUnit and jboss-as-maven-plugin. The result is project jboss-as-7-client at github.
In this previous post, I created a sample Java standalone client connecting to JBoss AS 5 & 6. Now let's look at how to do it in JBoss AS 7.1.1.
2 main usages of java.lang.ThreadLocal variables:

Vertical sharing: sharing and propagation of contextual data through the entire processing cycle. This is similar to http request attributes binding, except for the different scopes.
When we create or refactor a method, we find some input data is needed for the method to do its job.
4 steps when accessing a cache implemented with java.util.ConcurrentHashMap (javadoc):

get the value from the ConcurrentMap;if null, assume it's the first access, and create the value;call putIfAbsent on the concurrentMap to store the new value;if return value is not null (it's rare but happens), u
In addition to simple value custom resources, GlassFish also provides decent support for POJO JavaBean custom resources. The steps are very similar to String and primitive custom resources, except that you will need to provide your resource impl class and use a different GlassFish factory class.
Sometimes you need to create simple resources in application server so that all deployed applications can look them up. Their types can be string, boolean, number, or any custom java bean types.
In this post, I will go over the basic constructs of dynamic proxy, followed by some notes and things to watch out when implementing it. First, the 5 elements of a dynamic proxy as implemented in this example:

TestImpl: the class behind the proxy, not to be directly invoked by the client.
How to extract/expand/explode file.tar.gz, or file.tgz, with any of the following commands:

gtar xzvf file.tar.gz

gnutar xzf file.tar.gz

gunzip < file.tgz | tar xvf -

Note the use of < (gunzip file.tgz | tar xvf - won't work).
There are two ways to create your own thread type: subclass java.lang.Thread class, or implementing java.lang.Runnable and pass it to Thread constructor or java.util.concurrent.ThreadFactory.
Settings to remember when I need to use tcsh in a foreign environment:set prompt="%/ > "

# set prompt="%{\033]0;%M: %c\007%}%/ > "

set complete=enhance

set autolist=true

set colorcat

setenv GREP_OPTIONS --color=auto

setenv GREP_COLOR 32

Copy the above lines to $HOME/.tcshrc.
When submitting tasks to java executor service, some thread will be allocated to perform that task. The servicing thread may be assigned from the internal thread pool, or created on-demand.
Some notes on using NetBeans Java Debugger. They also apply in principal to other Java debuggers.

1, Set line breakpoint (the most common breakpoint), optionally make them conditional breakpoint to filter out irelevant hits.
Labels
Archive
Popular Posts
Popular Posts
  • Two JVM options are often used to tune JVM heap size: -Xmx for maximum heap size, and -Xms for initial heap size. Here are some common mi...
  • Simple enum . The ; after the last element is optional, when this is the end of enum definition. public enum Color { WHITE, BLACK, RED, ...
  • How to set project classpath in Eclipse and NetBeans are similar: just right-click the project name, choose Properties to bring up the Prope...
  • Let's say I need to spawn multiple threads to do the work, and continue to the next step only after all of them complete. I will need t...
  • This is a sample web.xml based on Servlet 2.5 (part of Java EE 5) that declares common elements. All top-level elements are optional, and c...
  • The default string value for java enum is its face value, or the element name. However, you can customize the string value by overriding toS...
  • Prior to JDK 6, we can check if a string is empty in 2 ways: if(s != null && s.length() == 0) if(("").equals(s)) Checking ...
  • When writing javadocs, IntelliJ automatically adds a closing tag for html elements. For instance, after typing <lt>, it automaticaly a...
  • StringBuilder was introduced in JDK 1.5. What's the difference between StringBuilder and StringBuffer? According to javadoc , StringBu...
  • With array, we can easily declare and initialize it at the same time: String[] favorites = new String[] {"EJB", "JPA", ...
Loading