background preloader

Java Programming Notes

Java Programming Notes

Coreservlets.com: Java, JSF 2.0, Ajax, jQuery, Spring, Hibernate, REST, Hadoop, and GWT Training, Tutorials, Consulting, Books, & Resources Thought Spearmints: @SuppressWarnings and its applicability to various program elements I cooked up a toy Java 5 class that generates warnings of the "unchecked" family when compiled: package pholser.util; import java.util.Arrays; public class Stack<T> { private final Object[] storage; private int top; public Stack() { this.storage = new Object[ 5 ]; } public Stack( final Stack<T> other ) { this.storage = (Object[]) other.storage.clone(); this.top = other.top; final T currentTop = (T) storage[ top ]; } public void push( final T item ) { checkPrecondition( isFull(), "can't push a full stack" ); storage[ top++ ] = item; } public T pop() { checkPrecondition( isEmpty(), "can't pop an empty stack" ); return (T) storage[ --top ]; } public T peek() { checkPrecondition( isEmpty(), "can't peek an empty stack" ); return (T) storage[ --top ]; } @Override public boolean equals( final Object that ) { if ( this == that ) return true; if ( that == null || !getClass().equals( that.getClass() ) ) return false; final Stack<?> other = (Stack<? Sure enough, no warnings. Fair enough. Golden. or

Paola Magillo, Univestita' di Genova, Corso di Interfacce Utente per Informatica, a.a. 2002-2003. [Per corso su Java vedere Gestione degli eventi in Java Classi per gli eventi Classi event listener, che definiscono modi di reagire a eventi di una certa classe Parte di AWT, usate anche in Swing. Classi di eventi Gli eventi sono classificati a seconda della loro causa scatenante, che puo' essere: un'azione fisica: cliccare mouse, muovere mouse, premere tasto su tastiera... Classi di eventi hanno nomi del tipo XXXEvent dove XXX e' la causa. Ogni classe di componente e' una potenziale sorgente di certe classi di eventi. Event listeners Posso registrare un event listener per ricevere eventi di una certa classe da una certa componente dell'interfaccia. Vi e' un'interfaccia event listener per ogni classe di evento: classe evento: XXXEventlistener: XXXListener Esempio: voglio catturare azionamento di un bottone. Implementare un listener Listener e adapter Font

How to Write a Custom Swing Component When you hear comparisons between AWT and Swing components, one of the first points mentioned is that Swing is lightweight. What this essentially means is that there are no real native controls "behind" Swing buttons, internal frames, and menus. Everything is controlled in pure Java, including rendering and event handling. Basic Building Blocks The Swing architecture overview provides an excellent high-level overview of the architectural decisions that were made during the development of Swing. The main building blocks of all core Swing components are: The component class itself, which provides an API for creating, changing, and querying the component basic state.The model interface and the model default implementation(s) that handle the component business logic and change notifications.The UI delegate that handles component layout, event handling (mouse and keyboard), and painting. Figure 1. The Component Class: UI Delegate Plumbing private static final String uiClassID = "FlexiSliderUI";

Java Tutorial Blog Eamonn McManus's Blog: Getting rid of unchecked warnings for casts Posted by emcmanus on March 30, 2007 at 3:27 AM PDT If you've ever made a serious effort to get rid of "unchecked" warnings from the Java compiler (the ones it gives you with -Xlint:unchecked) then you'll probably have found some cases where you know a cast is correct but you can't convince the compiler of it. Is there anything better than adding @SuppressWarnings("unchecked") around the whole method? The problem with the @SuppressWarnings solution is that it's much too broad. It suppresses warnings for the whole method, even though it's just one little cast that you want to allow. Fortunately, there's a good solution. List<String> list = (List<String>) x; You can change it to this: List<String> list = cast(x); where the cast method is defined like this: @SuppressWarnings("unchecked")private static <T> T cast(Object x) { return (T) x;} If there several different classes where you need this method, you could make it public and put it in a utility class, say Util.java.

B-Trees Introduction Tree structures support various basic dynamic set operations including Search, Predecessor, Successor, Minimum, Maximum, Insert, and Delete in time proportional to the height of the tree. Ideally, a tree will be balanced and the height will be log n where n is the number of nodes in the tree. To ensure that the height of the tree is as small as possible and therefore provide the best running time, a balanced tree structure like a red-black tree, AVL tree, or b-tree must be used. When working with large sets of data, it is often not possible or desirable to maintain the entire structure in primary storage (RAM). Instead, a relatively small portion of the data structure is maintained in primary storage, and additional data is read from secondary storage as needed. B-trees are balanced trees that are optimized for situations when part or all of the tree must be maintained in secondary storage such as a magnetic disk. The Structure of B-Trees Height of B-Trees B-Tree-Search(x, k)

Spring Framework Introduction The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key element of Spring is infrastructural support at the application level: Spring focuses on the "plumbing" of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments. Features Dependency InjectionAspect-Oriented Programming including Spring's declarative transaction managementSpring MVC web application and RESTful web service frameworkFoundational support for JDBC, JPA, JMSMuch more... Quick Start Spring Framework includes a number of different modules, here we are showing spring-context which provides core functionality. Once you've set up your build with the spring-context dependency, you'll be able to do the following: hello/MessageService.java package hello; public interface MessageService { String getMessage();}

Annotations Many APIs require a fair amount of boilerplate code. For example, in order to write a JAX-RPC web service, you must provide a paired interface and implementation. This boilerplate could be generated automatically by a tool if the program were “decorated” with annotations indicating which methods were remotely accessible. Other APIs require “side files” to be maintained in parallel with programs. The Java platform has always had various ad hoc annotation mechanisms. Annotations do not directly affect program semantics, but they do affect the way programs are treated by tools and libraries, which can in turn affect the semantics of the running program. Annotations complement javadoc tags. Typical application programmers will never have to define an annotation type, but it is not hard to do so. Once an annotation type is defined, you can use it to annotate declarations. @RequestForEnhancement( id = 2868724, synopsis = "Enable time-travel", engineer = "Mr. Here is the testing tool:

Narisa.com | Software Society Top Ten New Things You Can Do with NIO New I/O? Why do we need a new I/O? What's wrong with the old I/O? There's nothing wrong with the classes in the java.io package; they work just dandy -- for what they do. But it turns out there are quite a lot of things the traditional Java I/O model can't handle. Things like non-blocking modes, file locks, readiness selection, scatter/gather, and so on. NIO brings a host of powerful new capabilities to the Java platform. In this article I'm not going to explain buffers, channels, selectors, and the other denizens of the NIO depths. So, without further ado, in the time-honored tradition of O'Reilly authors flogging their own books by cooking up lame top ten lists: Top Ten New Things You Can Do with NIO That You Couldn't Do Before (TTNTYCDW..., oh never mind). 10: File Locking File locking is one of those things most programmers don't need very often. With NIO, file locks are built right into the FileChannel class. Figure 1: Reader processes holding shared locks.

java - How to open jar file using open JDK? Get Ready for Java 7 with the Free JDK 7 Reference Card Oracle submitted the release content for both Java SE 7 and 8 to the JCP at the end of last year. The JDK 7 and 8 JSRs represented Oracle's "Plan B" approach for separating JDK 7 into two separate releases. In the months since, JDK 7 has progressed to the Developer Preview milestone and the final version isn't too far off. To provide developers with an at-a-glance reference guide of all the essential JDK 7 elements, Developer.com has published the JDK 7 Reference Card. Following a free registration, you can download this helpful reference guide to get JDK 7 information such as: Newly included JSRs (including NIO.2, The Da Vinci Project and Project Coin)Changed JSRs in maintenance reviews (including JAXP, JAXB and JAX-WS)Enhanced JSRs (including JLS and the JVM spec)Smaller enhancements (including GC and concurrency)JSRs deferred to JDK 8 or later (including closures) All this info and more are condensed into one comprehensive, double-side PDF.

Maven Getting Started Guide What is Maven? At first glance Maven can appear to be many things, but in a nutshell Maven is an attempt to apply patterns to a project's build infrastructure in order to promote comprehension and productivity by providing a clear path in the use of best practices. Maven is essentially a project management and comprehension tool and as such provides a way to help with managing: BuildsDocumentationReportingDependenciesSCMsReleasesDistribution If you want more background information on Maven you can check out The Philosophy of Maven and The History of Maven. How can Maven benefit my development process? Maven can provide benefits for your build process by employing standard conventions and practices to accelerate your development cycle while at the same time helping you achieve a higher rate of success. Now that we have covered a little bit of the history and purpose of Maven let's get into some real examples to get you up and running with Maven! How do I setup Maven? How do I use plug-ins?

Related: