Monday, September 29, 2008
Java is ' Pass by Value'
Friday, June 13, 2008
Using Regex in java
Replacing brackets in java using Regular Expression.
public static void main (String args[]){
String pattern = "throw new Exception\\("; // pattern
String b = "throw new Exception("; //Match Against
String c = "throw new CustomException(;"; //Replace By
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(b);
String d = m.replaceAll(c);
System.out.println(d);
boolean bo = m.matches();
System.out.println(bo);
}
Friday, March 21, 2008
Interfaces and Classes and unambiguous references
What if a class implements two interfaces, and both the interfaces have static or instance variables with the same name?
Example:
public interface InterfaceA {
static String name="Manish from A";
public String getName();
}
public interface InterfaceB {
static String name="Manish from A";
public String getName();
}
public class Main implements InterfaceA,InterfaceB {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Main main = new Main();
System.out.println(main.getName());
}
public String getName() {
return name;
//throw new UnsupportedOperationException("Not supported yet.");
}
This kind of implementation will throw an unambiguous reference too the object name error during compile time.
Labels: unambiguous reference
Tuesday, February 5, 2008
Introduction to XML
The Extensible Markup Language (XML) is a general-purpose markup language.
It is classified as an extensible language because it allows its users to define their own elements. Its primary purpose is to facilitate the sharing of structured data across different information systems, particularly via the Internet.
It is used both to encode documents and to serialize data. In the latter context, it is comparable with other text-based serialization languages such as JSON and YAML.
Well-formed and valid XML documents
There are two levels of correctness of an XML document:
Well-formed. A well-formed document conforms to all of XML's syntax rules. For example, if a start-tag appears without a corresponding end-tag, it is not well-formed. A document that is not well-formed is not considered to be XML; a conforming parser is not allowed to process it.
A "Well Formed" XML document has correct XML syntax, it should also satisfy the following conditions -
1. XML documents must have a root element
2. XML elements must have a closing tag
3. XML tags are case sensitive
4. XML elements must be properly nested
5. XML attribute values must be quoted
Valid. A valid document additionally conforms to some semantic rules. These rules are either user-defined, or included as an XML schema or DTD. For example, if a document contains an undefined element, then it is not valid; a validating parser is not allowed to process it.
Example of a meaningful XML Document
XML Root - Bookstore
XML Child Node - book
XML Attributes for book - category
XML Elements - title, author, year price
XML elements must follow these naming rules:
1. Names can contain letters, numbers, and other characters
2. Names must not start with a number or punctuation character
3, Names must not start with the letters xml (or XML, or Xml, etc)
4, Names cannot contain spaces
5. Any name can be used, no words are reserved.
Labels: XML Introduction
Monday, February 4, 2008
Singleton objects
The Abstract Factory, Builder, and Prototype patterns can use Singletons in their implementation. Façade objects are often Singletons because only one Façade object is required.
Variable might not have been initialized
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.
Labels: initialising, Java, local variables
Setting Java Heap Size
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 mistakes I have seen when using them:
- Missing
m, M, g
orG
at the end (they are case insensitive). For example,
The correct command should be:java -Xmx128 BigApp
java.lang.OutOfMemoryError: Java heap spacejava -Xmx128m BigApp
. To be precise,-Xmx128
is a valid setting for very small apps, like HelloWorld. But in real life, I guess you really mean-Xmx128m
- Extra space in JVM options, or incorrectly use =. For example,
The correct command should bejava -Xmx 128m BigApp
Invalid maximum heap size: -Xmx
Could not create the Java virtual machine.
java -Xmx=512m HelloWorld
Invalid maximum heap size: -Xmx=512m
Could not create the Java virtual machine.java -Xmx128m BigApp
, with no whitespace nor =. -X options are different than -Dkey=value system properties, where = is used. - Only setting
-Xms
JVM option and its value is greater than the default maximum heap size, which is64m
. The default minimum heap size seems to be0
. For example,
The correct command should bejava -Xms128m BigApp
Error occurred during initialization of VM
Incompatible initial and maximum heap sizes specifiedjava -Xms128m -Xmx128m BigApp
. It's a good idea to set the minimum and maximum heap size to the same value. In any case, don't let the minimum heap size exceed the maximum heap size. - Heap size is larger than your computer's physical memory. For example,
The fix is to make it lower than the physical memory:java -Xmx2g BigApp
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.java -Xmx1g BigApp
- Incorrectly use
mb
as the unit, wherem
orM
should be used instead.java -Xms256mb -Xmx256mb BigApp
Invalid initial heap size: -Xms256mb
Could not create the Java virtual machine. - The heap size is larger than JVM thinks you would ever need. For example,
The fix is to lower it to a reasonable value:java -Xmx256g BigApp
Invalid maximum heap size: -Xmx256g
The specified size exceeds the maximum representable size.
Could not create the Java virtual machine.java -Xmx256m BigApp
- The value is not expressed in whole number. For example,
The correct command should bejava -Xmx0.9g BigApp
Invalid maximum heap size: -Xmx0.9g
Could not create the Java virtual machine.java -Xmx928m BigApp
How to set java heap size in Tomcat?
Stop Tomcat server, set environment variable
CATALINA_OPTS
, and then restart Tomcat. Look at the file tomcat-install/bin/catalina.sh
or catalina.bat
for how this variable is used. For example,set CATALINA_OPTS="-Xms512m -Xmx512m" (Windows)
export CATALINA_OPTS="-Xms512m -Xmx512m" (ksh/bash)
setenv CATALINA_OPTS "-Xms512m -Xmx512m" (tcsh/csh)
In catalina.bat or catallina.sh, you may have noticed CATALINA_OPTS, JAVA_OPTS, or both can be used to specify Tomcat JVM options. What is the difference between CATALINA_OPTS and JAVA_OPTS? The name CATALINA_OPTS is specific for Tomcat servlet container, whereas JAVA_OPTS may be used by other java applications (e.g., JBoss). Since environment variables are shared by all applications, we don't want Tomcat to inadvertently pick up the JVM options intended for other apps. I prefer to use CATALINA_OPTS.How to set java heap size in JBoss?
Stop JBoss server, edit
$JBOSS_HOME/bin/run.conf
, and then restart JBoss server. You can change the line with JAVA_OPTS
to something like:JAVA_OPTS="-server -Xms128m -Xmx128m"
How to set java heap size in Eclipse?You have 2 options:
1. Edit eclipse-home/eclipse.ini to be something like the following and restart Eclipse.
-vmargs
-Xms64m
-Xmx256m
2. Or, you can just run eclipse command with additional options at the very end. Anything after -vmargs will be treated as JVM options and passed directly to the JVM. JVM options specified in the command line this way will always override those in eclipse.ini. For example,eclipse -vmargs -Xms64m -Xmx256m
How to set java heap size in NetBeans?Exit NetBeans, edit the file
netbeans-install/etc/netbeans.conf
. For example,netbeans_default_options="-J-Xms512m -J-Xmx512m -J-XX:PermSize=32m -J-XX:MaxPermSize=128m -J-Xverify:none
How to set java heap size in Apache Ant?Set environment variable
ANT_OPTS
. Look at the file $ANT_HOME/bin/ant
or %ANT_HOME%\bin\ant.bat
, for how this variable is used by Ant runtime.set ANT_OPTS="-Xms512m -Xmx512m" (Windows)
export ANT_OPTS="-Xms512m -Xmx512m" (ksh/bash)
setenv ANT_OPTS "-Xms512m -Xmx512m" (tcsh/csh)
How to set java heap size in jEdit?jEdit is a java application, and basically you need to set minimum/maximum heap size JVM options when you run java command. jEdit by default runs with a default maximum heap size 64m. When you work on large files, you are likely to get these errors:
java.lang.OutOfMemoryError: Java heap space
at java.lang.String.concat(String.java:2001)
at org.gjt.sp.jedit.buffer.UndoManager.contentInserted(UndoManager.java:160)
at org.gjt.sp.jedit.Buffer.insert(Buffer.java:1139)
at org.gjt.sp.jedit.textarea.JEditTextArea.setSelectedText(JEditTextArea.java:2052)
at org.gjt.sp.jedit.textarea.JEditTextArea.setSelectedText(JEditTextArea.java:2028)
at org.gjt.sp.jedit.Registers.paste(Registers.java:263)
How to fix it? If you click a desktop icon, or Start menu item to start jEdit: right-click the icon or menu item, view its property, and you can see its target is something like:
C:\jdk6\bin\javaw.exe -jar "C:\jedit\jedit.jar"
You can change that line to:C:\jdk6\bin\javaw.exe -Xmx128m -Xms128m -jar "C:\jedit\jedit.jar"
If you run a script to start jEdit: just add these JVM options to the java line inside the script file:java -Xmx128m -Xms128m -jar jedit.jar
If you start jEdit by running java command: just add these JVM options to your java command:java -Xmx128m -Xms128m -jar jedit.jar
Note that when you run java with -jar option, anything after -jar jar-file
will be treated as application arguments. So you should always put JVM options before -jar
. Otherwise, you will get error:C:\jedit>java -jar jedit.jar -Xmx128m
Unknown option: -Xmx128m
Usage: jedit [] []
How to set java heap size in JavaEE SDK/J2EE SDK/Glassfish/Sun Java System Application Server?Stop the application server, edit
$GLASSFISH_HOME/domains/domain1/config/domain.xml
, search for XML element name java-config
and jvm-options
. For example,
-Xmx512m
-XX:NewRatio=2
-XX:MaxPermSize=128m
...
You can also change these settings in the web-based admin console, typically at http://localhost:4848/, or https://localhost:4848/. Go to Application Server near the top of the left panel, and then on the right panel, click JVM Settings -> JVM Options, and you will see a list of existing JVM options. You can add new ones and modify existing ones there.Yet another option is to use its Command Line Interface (CLI) tool command, such as:
./asadmin help create-jvm-options
./asadmin help delete-jvm-options
They may be a bit hard to use manually, but are well suited for automated scripts.
Wednesday, January 16, 2008
Making an object Immutable.
Definition: Immutable objects are those whose state(i.e object data) cannot change once the object has been constructed.
Making an object immutable:
1. Make the class final
2. Make the constructor parameterized, so that we need not allow setter methods
3. Make all fields final and private
4. Provide getter methods for retrieving values of fields.
Example:
public final class XYZ{
private final String name;
//constructor
public XYZ(String paramName){name=paramName;
}
//getter method
public String getName(){return name;
}
}
Important: Note that javadoc 1.4 includes the -tag option, whereby simple custom tags may be defined. One might define an @is.Immutable tag, for example, to document a class as being immutable
Use of immutable objects?
We have objects of type reference and value in java. All value objects should be immutable. Why, what is the point in making it immutable?
Let's solve this by considering an example:
Case 1: If value objects were not immutable
We have two objects Person A and Person B, sharing one value object, let's say date of birth. If this value object were not immutable, then when we update the date of birth of 'Person A', then 'Person B''s date of birth is also changed.
Case 2: If value objects are immutable (which is true):
In this case, we can easily understand that, if there is a date value object being accessed by 'Person A', and 'Person B' also wants a date object, then a new object is created, which is used specifically by 'Person B' only.
Excerpt from UML distilled by Martin Fowler - "In other words, you should not be able to take a date object of 1-Jan-99 and change the same date object to be 2-Jan-99. Instead, you should create a new 2-Jan-99 object and link to that first object. The reason is that if the date were shared, you would update another object's date in an unpredictable way.