I usually get confused with nested classes so I'll note some important points here.
Nested classes
- is also a member of enclosing class like variables and methods
- can use all access modifiers like variables and methods
Types of nested classes
1) static
- like a static method, it is not associated to any instance of enclosing class and has access only to all the static members
- syntax: Outer.Inner i = new Outer.Inner();
2) non-static / inner
- has access to other members of enclosing class even if declared private
- like non-static methods, it cannot define any static members as static members only belong to the (outer) class
- syntax: Outer.Inner i = new Outer().new Inner();
3) local inner - declared and known only within the body of method/block
4) anonymous inner - no name and known only within the statement in w/c they are defined
Monday, July 27, 2009
Thursday, July 9, 2009
Java modifiers summary
Modifier | Class | Interface | Inner Class | Inner Interface | Variable | Method | Constructor | Free-Floating Block |
public | yes | yes | yes | yes | yes | yes | yes | no |
protected | no | no | yes | yes | yes | yes | yes | no |
none or package or default | yes | yes | yes | yes | yes | yes | yes | yes |
private | no | no | yes | yes | yes | yes | yes | no |
final | yes | no | yes | no | yes | yes | no | no |
abstract | yes | yes/no | yes | yes/no | no | yes | no | no |
static | no | no | yes | yes | yes | yes | no | yes |
native | no | no | no | no | no | yes | no | no |
transient | no | no | no | no | yes | no | no | no |
volatile | no | no | no | no | yes | no | no | no |
synchronized | no | no | no | no | no | yes | no | yes |
strictfp | yes | yes | yes | yes | no | yes | yes | no |
Points:
- all access modifiers can be used to classes and members except for protected and private which cannot be used to outer classes and interfaces
- free-floating block cannot have access modifiers. it can only use static or synchronized
- constructors can only use access modifiers and strictfp
- native is only for methods
- transient and volatile are only for variables. Transient indicates that it is not serializable. Volatile indicates that it can be modified simultaneously by many threads.
- synchronized can only be used on methods or free-floating blocks
- strictfp can't be used on variables or free-floating blocks
Using strictfp ensures that you get the same result of floating-point expressions across multiple platforms. But it may also result to overflow or underflow hence the expression, "Write-Once-Get-Equally-Wrong-Results-Everywhere". If you don't use strictfp, JVM can calculate floating-point expressions however it want and thus could produce more accurate results.
Thursday, July 2, 2009
Getting started with Java
1) download the Java EE Software Development Kit (SDK)
- download link: http://www.oracle.com/technetwork/java/javaee/downloads/index.html
What Java Do I Need?
You must have a copy of the JRE (Java Runtime Environment) on your system to run Java applications and applets. To develop Java applications and applets, you need the JDK (Java Development Kit), which includes the JRE.
What's the difference between J2SE and J2EE?
J2SE has access to all of the SE libraries. However, EE adds a set of libraries for dealing with enterprise applications such as Servlets, JSP and Enterprise
Javabeans.
2) install JDK
- Windows instructions/troubleshooting: http://java.sun.com/javase/6/webnotes/install/jdk/install-windows.html
Why shouldn't I install in "C:\Program Files\Java"?
Some apps (e.g. maven, plugins) uses your Java path without considering potential whitespace on the path causing "C:\Program Files\Java" to become "C:\Program" w/c leads to errors. So either use a path w/out whitespace or set you path to JAVA_HOME=C:\Progra~1\Java not JAVA_HOME=C:\Program Files\Java
3) update environment variables (NOT case-sensitive under Windows)
PATH
- defines the search paths for executable programs (with file extension of ".exe", ".bat" or ".com" for Windows systems) invoked from a command shell ("cmd.exe")
- allows the use of javac and java
- if not set, you need to specify the full path to the executable every time you run it, such as: C:\Program Files\Java\jdk1.6.0\bin\javac MyClass.java
- so add here the JDK binary (bin) directory (e.g., "c:\jdk1.6\bin")
- Note: The JDK binary directory should be listed before "c:\windows\system32" and "c:\windows" in the PATH. This is because some Windows systems provide their own Java runtime (which is often outdated) in these directories (try search for "java.exe" in your computer!).
CLASSPATH
- defines the directories and Java's jar-files for searching for the Java classes referenced in a Java program
- normally, no explicit CLASSPATH setting is required
- if not set, default is current working directory (since JDK 1.3)
- if set, include the current working directory '.'
- link: How Classes are found
JAVA_HOME
- needed for running Tomcat and many Java applications
- set here the JDK installation directory, e.g., "c:\jdk1.6
How to set environment variables in Mac/Unix
1. Set environment variables for your user in ~/.bash_profile (will affect bash shells only).
Create the file if it does not exist:
Ref: http://www3.ntu.edu.sg/home/ehchua/programming/howto/environment_variables.html
- download link: http://www.oracle.com/technetwork/java/javaee/downloads/index.html
What Java Do I Need?
You must have a copy of the JRE (Java Runtime Environment) on your system to run Java applications and applets. To develop Java applications and applets, you need the JDK (Java Development Kit), which includes the JRE.
What's the difference between J2SE and J2EE?
J2SE has access to all of the SE libraries. However, EE adds a set of libraries for dealing with enterprise applications such as Servlets, JSP and Enterprise
Javabeans.
2) install JDK
- Windows instructions/troubleshooting: http://java.sun.com/javase/6/webnotes/install/jdk/install-windows.html
Why shouldn't I install in "C:\Program Files\Java"?
Some apps (e.g. maven, plugins) uses your Java path without considering potential whitespace on the path causing "C:\Program Files\Java" to become "C:\Program" w/c leads to errors. So either use a path w/out whitespace or set you path to JAVA_HOME=C:\Progra~1\Java not JAVA_HOME=C:\Program Files\Java
3) update environment variables (NOT case-sensitive under Windows)
PATH
- defines the search paths for executable programs (with file extension of ".exe", ".bat" or ".com" for Windows systems) invoked from a command shell ("cmd.exe")
- allows the use of javac and java
- if not set, you need to specify the full path to the executable every time you run it, such as: C:\Program Files\Java\jdk1.6.0\bin\javac MyClass.java
- so add here the JDK binary (bin) directory (e.g., "c:\jdk1.6\bin")
- Note: The JDK binary directory should be listed before "c:\windows\system32" and "c:\windows" in the PATH. This is because some Windows systems provide their own Java runtime (which is often outdated) in these directories (try search for "java.exe" in your computer!).
CLASSPATH
- defines the directories and Java's jar-files for searching for the Java classes referenced in a Java program
- normally, no explicit CLASSPATH setting is required
- if not set, default is current working directory (since JDK 1.3)
- if set, include the current working directory '.'
- link: How Classes are found
JAVA_HOME
- needed for running Tomcat and many Java applications
- set here the JDK installation directory, e.g., "c:\jdk1.6
How to set environment variables in Mac/Unix
1. Set environment variables for your user in ~/.bash_profile (will affect bash shells only).
Create the file if it does not exist:
touch ~/.bash_profileOpen the file:
open ~/.bash_profileAdd environment variables in the file:
export JAVA_HOME=$(/usr/libexec/java_home) export JRE_HOME=$(/usr/libexec/java_home)2. Set temporary environment variables for the current bash shell. Just type the same command in 1. to the current bash shell.
Ref: http://www3.ntu.edu.sg/home/ehchua/programming/howto/environment_variables.html
Wednesday, April 22, 2009
Design Patterns
Definitions on this blog came from the book Head First Design Patterns.
Ref:
Head First Design Patterns by By Eric Freeman, Elisabeth Robson, Kathy Sierra, and Bert Bates
- Strategy pattern - defines a family of algorithms, ancapsulate each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
- Observer pattern - defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
- think Swing/GUI - Decorator pattern - attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
- think Java I/O
From http://www.ida.liu.se |
From http://kur2003.if.itb.ac.id |
Using Java's built-in Observer pattern (allows observer to pull data from observable)...
From http://kur2003.if.itb.ac.id |
From http://oreilly.com |
Ref:
Head First Design Patterns by By Eric Freeman, Elisabeth Robson, Kathy Sierra, and Bert Bates
Monday, April 13, 2009
Web Application Basics
HTTP - Hypertext Transfer (or Transport) Protocol), is a connectionless protocol for communicating clients/browsers and web servers
TCP/IP - Transmission Control Protocol, allows communication between your application software. It is responsible for breaking data down into IP packets before they are sent, and for assembling the packets when they arrive.
-connection oriented
-used to make connections and exchange information to one another
IP - Internet Protocol, is responsible for sending the packets to the correct destination
- connectionless
URL -Uniform Resource Locator, is a full specification of a resource. It includes the protocol, host machine name (domain name or IP address), optional protocol number and resource location.
Three ranges of port numbers:
TCP/IP - Transmission Control Protocol, allows communication between your application software. It is responsible for breaking data down into IP packets before they are sent, and for assembling the packets when they arrive.
-connection oriented
-used to make connections and exchange information to one another
IP - Internet Protocol, is responsible for sending the packets to the correct destination
- connectionless
URL -Uniform Resource Locator, is a full specification of a resource. It includes the protocol, host machine name (domain name or IP address), optional protocol number and resource location.
Three ranges of port numbers:
- well-known ports - from 0 through 1023
20 & 21: File Transfer Protocol (FTP)
22: Secure Shell (SSH)
23: Telnet remote login service
25: Simple Mail Transfer Protocol (SMTP)
53: Domain Name System (DNS) service
80: Hypertext Transfer Protocol (HTTP) used in the World Wide Web
110: Post Office Protocol (POP3)
119: Network News Transfer Protocol (NNTP)
143: Internet Message Access Protocol (IMAP)
161: Simple Network Management Protocol (SNMP)
194: Internet Relay Chat (IRC)
443: HTTP Secure (HTTPS)
465: SMTP Secure (SMTPS) - the registered ports - from 1024 through 49151
- they can be registered to specific protocols by software corporations/companies or users
- assigned by or registered to Internet Assigned Numbers Authority (IANA) (or by Internet Corporation for Assigned Names and Numbers (ICANN) before March 21, 2001[1]) - dynamic or private ports - from 49152 through 65535
- available for use by any application or just about anybody
HTML - is the principal language b/w the client and server that expresses content of webpages.
- DOM - is an interface to the browser and HTML/XML documents.
- DHTML
Dynamic HTML is a term used to describe the combination of HTML, style sheets, and scripts that allow dynamic pages. - Javascript - is the most common scripting language for browsers.
- Inversion of Control and Dependency Injection
- Loose coupling - describes a relationship of two entities/objects where they can interact, but have very little knowledge of each other.
Martin Fowler, who first coined Dependency Injection, considers it the same to Inversion of Control. He used Dependency Injection since he finds Inversion of Control too generic. However, other sources claim that Dependency Injection is just a form of Inversion of Control. With all the articles and blogs written about them, it is very easy to get confused. So I'll try to summarize their description here according to my own understanding and hopefully, it will be simple and easy to understand.
Inversion of Control is a general principle in which the flow of control is inverted, unlike the traditional sequential flow. It follows the "Hollywood principle" - "don't call us, we'll call you". Your objects don't call services directly. Instead, your objects expect to be called. It is the framework that manages the objects and services, and is aware of what to instantiate and invoke.
The main idea of Inversion of Control is to have none of your classes know or care how they get the objects they depend on.
Dependency Injection is a way of implementing Inversion of Control in which an external mechanism is supplied in order for your objects not to call services (other objects) directly and therefore not depend on them. The external mechanism is the one responsible in injecting the concrete implementation that your objects needed.
To r: inline css vs external css
What is robustness?
Disadvan of frames
Website-static
Webapp-dynamic
Get vs post- when to use get?
Session - uses cookies
Enabling technoligies:
Compiled modules-java servlets
Interpreted scripts-jsp
Monday, February 16, 2009
Other Pointers
- Weblogic Erroneous Handlers Error - this is caused by the jrockit version of jdk. To get the correct error message, use the Java jdk first. Source: http://ricksolutions.blogspot.hk/2009/03/fixing-erroneous-handlers-error.html
- checking tables on db2 select * from sysibm.systables where dbname='NACS01DB' and name like 'PC%'
- command for unzipping war files jar xvf xxx.war
- Extracting the Contents a JAR File
The basic command to use for extracting the contents of a JAR file is:
jar xf jar-file [archived-file(s)]
- The x option indicates that you want to extract files from the JAR archive.
- The f options indicates that the JAR file from which files are to be extracted is specified on the command line, rather than through stdin.
- The jar-file argument is the filename (or path and filename) of the JAR file from which to extract files.
- archived-file(s) is an optional argument consisting of a space-delimited list of the files to be extracted from the archive. If this argument is not present, the Jar tool will extract all the files in the archive.
- character encoding - http://www.joelonsoftware.com/articles/Unicode.html
- DB2 Timestamp to java.util.Date or java.sql.Timestamp
- DB2 format = yyyy-MM-dd HH:mm:ss.ZZZZZZ where Z = seconds since previos midnight (00000 - 86400), http://pic.dhe.ibm.com/infocenter/db2luw/v9r7/index.jsp?topic=%2Fcom.ibm.db2.luw.sql.ref.doc%2Fdoc%2Fr0007107.html
- java.sql.Timestamp = yyyy-mm-dd hh:mm:ss.fffffffff where f = nanoseconds
To convert to java.util.Date, use
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr)
To convert to java.sql.Timestamp, use
resultSet.getTimestamp([column name or column index])
http://technobuz.com/2011/04/run-commands-for-microsoft-applications/
Java Pointers / Links
- strength and limitations of for each loop (for( : ))
http://today.java.net/article/2006/11/03/nuances-java-50-each-loop - Loading properties/configurationfiles
- Java 5 New Features
http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html
http://java.sun.com/developer/technicalArticles/releases/j2se15/
http://download.oracle.com/javase/1.5.0/docs/guide/language/index.html - Increase Java Performance
http://java.sun.com/developer/technicalArticles/Programming/Performance/ - Java Language Best Practices
http://docs.oracle.com/cd/A97688_16/generic.903/bp/java.htm - Code Conventions for the Java Programming Language
http://www.oracle.com/technetwork/java/codeconv-138413.html - ThreadLocal - http://java.dzone.com/articles/java-thread-local-%E2%80%93-how-use
- String Concatenation
- use plus operator(+) only if all operands are constants1) String temp = “alpha” + “beta”; '-> String temp = “alphabeta”; (compile time translation) 2) String temp = x + “beta”; '-> String temp = (new StringBuilder()).append(x).append(“beta”).toString();
- concat using plus operator uses StringBuilder and String behind the scenes:“alpha” + “beta” = new StringBuilder(String.toString(“alpha”)).append(“beta”).toString()
- use String.concat if concatenating only 2 or few strings
- use StringBuilder(unsynchronized)/StringBuffer(synchronized) for more than 2 strings, specially in a loop
- in a loop, SB is better than concat is better than +
- always initialize SB with initial capacity to greatly improve performancenew SB() has 16 char initial capacity by default
- best explanation is here: http://www.znetdevelopment.com/blogs/2009/04/06/java-string-concatenation/ - character set conversion
String stringToConvert = bmText; byte[] convertStringToByte = stringToConvert.getBytes("UTF-8"); bmText = new String(convertStringToByte, "ISO-8859-1"); options.add(new SelectOption(bmValue, new String(bmText)));
public void doHeader() throws Exception { Image logoImage = PDFUO.getLogoImage(); //logoImage.setAbsolutePosition(ReportConstantsIF.ITEXT_POINTS/2, length - ReportConstantsIF.ITEXT_POINTS); logoImage.setAlignment(Image.ALIGN_TOP | Image.TEXTWRAP); Paragraph p = new Paragraph(); p.add(logoImage); p.add(new Phrase("BEAR, STEARNS SECURITIES CORP." + "\n\n", new Font(Font.TIMES_ROMAN, 6, Font.BOLD))); p.add(new Phrase( "ONE METROTECH CENTER NORTH" + "\n" + "BROOKLYN, NEW YORK 11201-3859" + "\n" + "(212) 272-1000", smallFont)); p.setLeading(9f); HeaderFooter header = new HeaderFooter(p, false); header.setAlignment(Element.ALIGN_RIGHT); header.setBorder(PdfTable.NO_BORDER); document.setHeader(header); }
-error occurs and data is not displayed when the columns in a row is not complete in PdfPTable
Subscribe to:
Posts (Atom)