Java and supporting
features for JEE
Version: 0.5
Issue Date: 04/23/2007
Author: MeoLe
©2003First Consulting Group, Inc.
Agenda
Java 1.5 vs. C#
Java 5 Annotations
RCP
Ant introduction
Maven introduction
© FCG | Slide 2 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Objectives
Provide a quick reference for C# developer to learn Java
effectively.
Introduce useful java-based stuffs to be able to work effectively
on J2EE/JEE technology.
© FCG | Slide 3 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Java 1.5 vs. C#
© FCG | Slide 4 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Comments & Data Types
© FCG | Slide 5 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Constants & Enumerations
© FCG | Slide 6 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Operators
© FCG | Slide 7 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Choices
© FCG | Slide 8 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Loops
© FCG | Slide 9 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Arrays
© FCG | Slide 10 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Functions
© FCG | Slide 11 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Strings
© FCG | Slide 12 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Exception Handling
© FCG | Slide 13 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Namespaces
© FCG | Slide 14 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Classes/Interfaces
© FCG | Slide 15 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Constructors/Destructors
© FCG | Slide 16 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Objects
© FCG | Slide 17 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Properties
© FCG | Slide 18 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Structs
© FCG | Slide 19 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Console I/O
© FCG | Slide 20 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
File I/O
© FCG | Slide 21 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
More …
http://www.harding.edu/USER/fmccown/WWW/java1_5_csharp_
comparison.html
http://www.25hoursaday.com/CsharpVsJava.html
http://www.javacamp.org/javavscsharp/index.html
© FCG | Slide 22 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Java 5 Annotations
Java 5 Annotations
© FCG | Slide 23 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
What are annotations?
In short, annotations are metadata or data about data
The value of annotations:
– Compiler checking
– Code analysis
– Dependency injection (especially for JEE)
© FCG | Slide 24 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
The Override annotation
Indicates that the annotated method is overriding a method in a
superclass.
The compiler will give an error if the super class does not have that method.
Change public int hashCode() to public int hasCode() compiler error.
package com.fcgv.corejava.annotation;
public class OverrideTester {
public OverrideTester() { }
@Override
public String toString() {
return super.toString() + " [Override Tester Implementation]";
}
@Override
public int hashCode() {
return toString().hashCode();
}
}
© FCG | Slide 25 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
The Deprecated annotation
To annotate a method that shouldn't be used anymore.
The compiler will give an error/warning if this method is override or invoked .
package com.fcgv.corejava.annotation;
public class DeprecatedClass {
@Deprecated
public void doSomething() {
// some code
}
public void doSomethingElse() {
// This method presumably does what doSomething() does, but better
}
}
© FCG | Slide 26 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
The SuppressWarnings
annotation
Indicates that the annotated method is overriding a method in a
superclass.
The compiler will give an error/warning if there’s no @SuppressWarnings.
package com.fcgv.corejava.annotation;
import java.util.ArrayList;
import java.util.List;
import java.lang.SuppressWarnings;
public class SuppressWarningsClass {
@SuppressWarnings(value = { "unchecked" })
public void nonGenericsMethod() {
List wordList = new ArrayList(); // no typing information on the List
wordList.add("foo"); // causes error on list addition
}
© FCG | Slide 27 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
More built-in annotations
There are more built-in annotations can be found in
java.lang.annotation package:
– @Documented: Whether to put the annotation in Javadocs
– @Inherited: Whether subclasses get the annotation
– @Retention: When the annotation is needed
– @Target: Places the annotation can go
Custom annotations can be implemented if needed.
© FCG | Slide 28 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
JEE 5 annotations
The Java EE 5 platform simplifies deployment by removing the need
for deployment descriptors:
– Defining and using web services
– Developing EJB software applications
– Mapping Java technology classes to XML
– Mapping Java technology classes to databases
– Mapping methods to operations
– Specifying external dependencies
– Specifying deployment information, including security attributes
Custom annotations can be implemented if needed.
© FCG | Slide 29 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
EJB3 annotation examples
package com.fcgv.tdd.service;
@Stateless
public class BookManagementFacadeBean implements BookManagementFacadeLocal {
@Resource
private SessionContext ctx;
@PersistenceContext (name="Tdd-Unit")
private EntityManager manager;
public List<Book> getAllBooks() {
return manager.createQuery("select o from Book o").getResultList();
}
}
package com.fcgv.tdd.service;
@Local
public interface BookManagementFacadeLocal {
public List<Book> getAllBooks();
}
© FCG | Slide 30 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
EJB3 annotation examples (cont.)
package com.fcgv.tdd.model;
@Entity
@NamedQueries({@NamedQuery(name = "Book.findAll", query = "select o from Book o")})
@Table(name = "BOOK")
public class Book implements Serializable {
@Id
@Column(name = "ID", updatable = false)
private Integer id = null;
@Column(name = "TITLE", length=50)
private String title;
@Column(name = "ISBN", length=20)
private String isbn;
@Column(name = "QUANTITY")
private Integer quantity;
@Column(name = "DESCRIPTION", length=5000)
private String description;
…
}
© FCG | Slide 31 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Resource lookup example
Without annotation:
public javax.sql.DataSource getCatalogDS() {
try {
// Obtain the initial Java Naming and Directory Interface
// (JNDI) context.
InitialContext initCtx = new InitialContext();
// Perform JNDI lookup to obtain the resource.
catalogDS = (DataSource)
initCtx.lookup("java:comp/env/jdbc/catalogDS");
} catch (NamingException ex) {
// Handle failure.
}
}
...
public getProductsByCategory() {
// Get the DataSource.
DataSource catalogDS = getCatalogDS();
// Get a connection and execute the query.
Connection conn = catalogDS.getConnection();
...
}
© FCG | Slide 32 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
JNDI lookup without annotations
(cont.)
<resource-ref>
<description>Catalog DataSource</description>
<res-ref-name>jdbc/catalogDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
...
With annotation:
private @Resource DataSource catalogDS;
public getProductsByCategory() {
// Get a connection and execute the query.
Connection conn = catalogDS.getConnection();
...
}
...
© FCG | Slide 33 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Ant introduction
Ant introduction
Objectives
Provide a quick reference for C# developer to learn Java
effectively.
Introduce useful java-based stuffs to be able to work effectively
on J2EE/JEE technology.
© FCG | Slide 35 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Maven introduction
Maven introduction
Objectives
Provide a quick reference for C# developer to learn Java
effectively.
Introduce useful java-based stuffs to be able to work effectively
on J2EE/JEE technology.
© FCG | Slide 37 FCG Internal/Restricted/Confidential Version: ##.## | Issue Date: mm/dd/yyyy
Q&A
Q&A
Thank You