Feeds:
Posts
Comments

Archive for the ‘Java’ Category

Nov 13 2006

Sun released the first pieces of source code for Sun’s implementation of JSE (Java Platform Standard Edition) and a buildable implementation of JME (Java Platform Micro Edition). Sun will also be making JEE (Java Platform Enterprise Edition) available under the GPLv2 license. JEE had already been available under Sun’s CDDL (Common Development and Distribution License), through Project GlassFish.

All of the Java source code is scheduled to be released by March 2007. 

Sun states that this announcement represents one of the largest source code contributions under the GPL license, and also that it is the open-sourcing of one of the industry’s most significant and pervasive software platforms.

(more…)

Read Full Post »

Highlights of J2SE 5.0

  • Metadata: Additional data or annotations can be associated with classes, interfaces, methods and fields. This can be read by annotation tools or at runtime through Java Reflection API. This can be used for generating additional source code or for providing additional debugging information.

@debug(devbuild=production,counter=1) public void testMethod()

  • Generic Types:  Earlier version’s Collection API stored the values as the instances of ‘OBJECT’ and so required type casting while fetching the objects. The mismatch of data types therefore could not be discovered until runtime. This is now replaced with a possibility to declare the Collection with <> notation that will detect the mismatch of data types at compile time.  

ArrayList<Integer> list =  new ArrayList<Integer>();
list.add(0, new Integer(42));
int total = list.get(0).intValue();

  • Auto-boxing and auto-unboxing of primitive type variables: The conversion between primitive type variables and their object based counterparts is left to the compiler now and will not require extra coding effort.

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0, 42);
int total = list.get(0);

  • Enhanced for Loop: The enhanced for loop will now replace the traditional iteration code used to traverse a collection. There is no need of looping code and type casting as the collection objects will be converted to generic types.

ArrayList<Integer> list = new ArrayList<Integer>();  
for (Integer i : list) { … }

  • Enumerated types: Enumerated type is introduced as an alternative to static final constants.

public enum StopLight { red, amber, green };

  • Static import: The static import feature enables to refer to static constants from a class without needing to inherit it.

import static java.awt.BorderLayout.*;
getContentPane().add(new JPanel(), CENTER);

  • Formatted Output:  The traditional C specific ‘printf’ functionality can be used for formatted output.

System.out.printf(“name count%n”);
System.out.printf(“%s %5d%n”, user,total);

  • Formatted Input:   In contrast to the heavy code used to get input from standard input in earlier versions, the scanner API provides basic input functionality for reading data from standard input or other data stream.

Scanner s= new Scanner(System.in);
String param= s.next();
int value=s.nextInt();
s.close(); 

  • Varargs:  It allows to pass flexible number of arguments to a method.

void argtest(Object … args){}

  • Concurrency utilities:  The new version comes with more powerful concurrency control techniques like semaphores. It can be used to restrict access to a block of code, allows a defined number of concurrent thread accesses, and allows testing a lock before acquiring it.

final  private Semaphore s= new Semaphore(1, true);
s.acquireUninterruptibly(); //for non-blocking version use s.acquire()
try {    
      balance=balance+10; //protected value
} finally {
     s.release(); //return semaphore token
}

  • RMI compiler-rmic: There is no need to pre-generate stubs using rmic tool as the new version comes with the support of dynamic generation of stubs at runtime.
  • Class-data sharing:  This feature of class-data sharing enables the sharing of read-only data between multiple running JVMs and also improves startup time as core JVM classes are pre-packed.

  • Monitoring:   The JVM Monitoring and Management API enables monitoring and managing the Java virtual machine and the underlying operating system.  The API enables applications to monitor themselves and enables JMX-compliant tools to monitor and manage a virtual machine locally and remotely including low memory detector.
  • JVM Profiling API (JVMTI):  It includes byte code instrumentation. The advantage of this technique is that it allows more focused analysis and limits the interference of the profiling tools on the running JVM. The instrumentation can even be dynamically generated at runtime, as well as at class loading time, and pre-processed as class files.
  • Improved diagnostic capability:  New APIs are introduced to provide complete stack trace in case of any failure. The HotSpot JVM includes a fatal error handler that can run a user-supplied script or program if the JVM aborts.
  • Desktop Client:  New look and feel like introduction of a new theme for swing toolkit called ‘Ocean’.
  • Core XML support:  Several revisions to core XML support including XML 1.1, SAX 2.02, DOM level 3 support and XSLT with a fast XLSTC compiler.
  • Supplementary character support:  32-bit supplementary character support has been added. Supplementary characters are encoded as a special pair of UTF16 values to generate a different character A surrogate pair is a combination of a high UTF16 value and a following low UTF16 value. In general, when using a String or sequence of characters, the core API libraries will transparently handle the new supplementary characters for you.

String u=”\uD840\uDC08″;

  • JDBC rowsets:  There are five new rowset objects added in the new version.
    Two of the most valuable are:

    • Cached rowset: It contains an in-memory collection of rows retrieved from the database that can, if needed, be synchronized at a later point in time.
    • Web rowset: It can write and read the RowSet in XML format.

Read Full Post »

   1. What is the servlet?

      Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet may be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company’s order database.

   2. What’s the difference between servlets and applets?

      Servlets are to servers; applets are to browsers. Unlike applets, however, servlets have no graphical user interface.

   3. What’s the advantages using servlets than using CGI?

      Servlets provide a way to generate dynamic documents that is both easier to write and faster to run. It is efficient, convenient, powerful, portable, secure and inexpensive. Servlets also address the problem of doing server-side programming with platform-specific APIs: they are developed with Java Servlet API, a standard Java extension.

   4. What are the uses of Servlets?

      A servlet can handle multiple requests concurrently, and can synchronize requests. This allows servlets to support systems such as on-line conferencing. Servlets can forward requests to other servers and servlets. Thus servlets can be used to balance load among several servers that mirror the same content, and to partition a single logical service over several servers, according to task type or organizational boundaries.

   5. What’s the Servlet Interface?

      The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or, more commonly, by extending a class that implements it such as HttpServlet. Servlets–>Generic Servlet–>HttpServlet–>MyServlet. The Servlet interface declares, but does not implement, methods that manage the servlet and its communications with clients. Servlet writers provide some or all of these methods when developing a servlet.

   6. When a servlet accepts a call from a client, it receives two objects. What are they?

      ServeltRequest: which encapsulates the communication from the client to the server.
      ServletResponse: which encapsulates the communication from the servlet back to the client.
      ServletRequest and ServletResponse are interfaces defined by the javax.servlet package.

Read Full Post »

JSP

   1. What is JSP technology?

      Java Server Page is a standard Java extension that is defined on top of the servlet Extensions. The goal of JSP is the simplified creation and management of dynamic Web pages. JSPs are secure, platform-independent, and best of all, make use of Java as a server-side scripting language.

   2. What is JSP page?

      A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format such as HTML, SVG, WML, and XML, and JSP elements, which construct dynamic content.

   3. What are the implicit objects?

      Implicit objects are created by the web container and contain information related to a particular request, page, or application. They are request, response, pageContext, session, application, out, config, page and exception.

   4. How many JSP scripting elements and what are they?

      There are three scripting language elements: declarations, scriptlets, and expressions.

   5. Why are JSP pages the preferred API for creating a web-based client program?

      Because no plug-ins or security policy files are needed on the client systems(applet does). Also, JSP pages enable cleaner and more module application design because they provide a way to separate applications programming from web page design. This means personnel involved in web page design do not need to understand Java programming language syntax to do their jobs.

   6. Is JSP technology extensible?

      YES. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.

Read Full Post »

The Java programming language is strongly-typed, which means that all variables must first be declared before they can be used.
This involves stating the variable’s type and name, as you’ve already seen:

int gear = 1;

Read Full Post »

Java Variables

Home Java Tutorial


int cadence = 0; 
int speed = 0; 
int gear = 1;

The Java programming language defines the following kinds of variables:

  • Instance variables (non-static fields) – are unique to each instance of a class.
  • Class variables (static fields) – are fields declared with the static modifier; there is exactly one copy of a class variable, regardless of how many times the class has been instantiated.
  • Local variables – store temporary state inside a method.
  • Parameters – are variables that provide extra information to a method; both local variables and parameters are always classified as “variables” (not “fields”).

Naming

  • Variable names are case-sensitive A variable’s name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign “$”, or the underscore character “_”.
  • Subsequent characters may be letters, digits, dollar signs, or underscore characters.
  • If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word.

Read Full Post »