Sun Java Solaris Communities My SDN Account Join SDN
 
Java Technology Fundamentals Newsletter Index

Passing Information to a Method or a Constructor, Object as a Superclass, Creating and Editing Files, NetBeans IDE 5.x Tutorial for Applets, and Self-Paced Web, CD, and Virtual Courses

 
In This Issue

Welcome to the Java Technology Fundamentals Newsletter

This monthly newsletter provides a way for you to learn the basics of the Java programming language, discover new resources, and keep up-to-date on the latest additions to Sun Developer Network's New to Java Center.

Note: For the code in this issue of Fundamentals to compile use the JDK 6 software.

Basic Java Technology Programming
Making Sense of the Java Platform Classes and Tools
Desktop Java Platform Development
Server-Side Java Platform Development
Free Developer Tools
Java Technology
For More Information

 
Basic Java Technology Programming

Passing Information to a Method or a Constructor

The declaration for a method or a constructor declares the number and the type of arguments for that method or constructor. For example, the following is a method that computes the monthly payments for a home loan, based on the amount of the loan, the interest rate, the length of the loan (the number of periods), and the future value of the loan:

public double computePayment(double loanAmt,
                             double rate,
                             double futureValue,
                             int numPeriods) {
        double interest = rate / 100.0;
        double partial1 = Math.pow((1 + interest), -numPeriods);
        double denominator = (1 - partial1) / interest;
        double answer = (-loanAmt / denominator)
                        - ((futureValue * partial1) / denominator);
        return answer;
}
 

This method has four parameters: the loan amount, the interest rate, the future value, and the number of periods. The first three are double-precision floating point numbers, and the fourth is an integer. The parameters are used in the method body and at runtime will take on the values of the arguments that are passed in.

Note : "Parameters" refers to the list of variables in a method declaration. "Arguments" are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration's parameters in type and order.

Parameter Types

You can use any data type for a parameter of a method or a constructor. This includes primitive data types, such as doubles, floats, and integers, as you saw in the computePayment method, and reference data types, such as objects and arrays.

Here's an example of a method that accepts an array as an argument. In this example, the method creates a new Polygon object and initializes it from an array of Point objects (assume that Point is a class that represents an x, y coordinate):

    public Polygon polygonFrom(Point[] corners) {
        // Method body goes here.
    }
 

Note : The Java programming language doesn't let you pass methods into methods. But you can pass an object into a method and then invoke the object's methods.

Read the rest of this tutorial

Making Sense of the Java Platform Classes and Tools

Object as a Superclass

The Object class, in the java.lang package, sits at the top of the class hierarchy. Every class is a descendant, direct or indirect, of the Object class. Every class you use or write inherits the instance methods of Object. You need not use any of these methods, but if you choose to do so, you may need to override them with code that is specific to your class. The methods inherited from Object that are discussed in this section are the following:

  • protected Object clone() throws CloneNotSupportedException
       Creates and returns a copy of this object

  • public boolean equals(Object obj)     
       Indicates whether some other object is "equal to" this
       one

  • protected void finalize() throws Throwable   
       Called by the garbage collector on an object when garbage
       collection determines that there are no more references
       to the object

  • public final Class getClass()
       Returns the runtime class of an object

  • public int hashCode()
       Returns a hash code value for the object

  • public String toString()
       Returns a string representation of the object

The notify, notifyAll, and wait methods of Object all play a part in synchronizing the activities of independently running threads in a program, which is discussed in a later lesson and won't be covered here. There are five of these methods:

  • public final void notify()
  • public final void notifyAll()
  • public final void wait()
  • public final void wait(long timeout)
  • public final void wait(long timeout, int nanos)

Read the rest of this tutorial

Desktop Java Platform Development

Creating and Editing Files

Each month, this section highlights articles and tutorials to teach you about the Java technologies involved in desktop application development and the tools that you can use to create various types of desktop applications.

Last month, you read the article "GUI Building in NetBeans IDE 5.5." This month, you'll learn about creating and editing source code in the NetBeans integrated development environment (IDE).

Creating and editing Java source code is the most important function that the IDE serves, since that's what developers generally spend most of their day doing. NetBeans IDE provides a wide range of tools that can complement any developer's style, regardless of whether you prefer to code everything by hand or want the IDE to generate large chunks of code for you.

This tutorial covers the following topics:

  • Creating Java Files
  • Using File Templates
  • Using GUI Templates
  • Editing Java Files in the Source Editor
  • Code Completion
  • Code Templates
  • Special Code Template Syntax
  • Editor Hints
  • Refactoring
  • Working With Import Statements
  • Formatting Java Source Code
  • Navigating in the Source Editor
  • Navigating Within a Java File
  • Search and Selection Tools
  • Navigating Between Documents
  • Configuring the Editor

Read this tutorial

Server-Side Java Development

NetBeans IDE 5.x Tutorial for Applets

Each month, this section will highlight articles to get you familiar with the Java technologies involved in server-side development and the tools you can use to create various types of web applications.

Last month, you learned about using the NetBeans IDE for creating web applications. Now, you are going to learn about creating applets in the NetBeans IDE.

Though there are no project templates that are specifically designed for creating applets in NetBeans IDE 5.x, you can easily develop them in a Java project and package them in a web project. The application that you build in this tutorial guides you through the following activities in NetBeans IDE 5.x:

  • Creating or importing an applet source file. Create a Java project, create or import an applet source file (see the final section if you want to import a JAR file containing applet source files).

  • Running and debugging an applet source file. Run an applet source file, debug it by modifying its parameters in the src folder.

  • Embedding an applet in a web application. Create a web project, import a NetBeans IDE 5.x Java project or a JAR file containing applet source files, add applet tags to a JSP file or HTML file, run the web project.

This tutorial can be completed in 20 minutes.

Read this tutorial

Free Developer Tools

Sun is offering the award-winning Sun Java Studio Enterprise and Sun Java Studio Creator IDEs at no cost to all developers worldwide who join the Sun Developer Network (SDN).

Get your free tools

Java Technology Courses

Self-Paced Web, CD, and Virtual Courses

Fundamentals of the Java Programming Language (WJB-110A)
The Fundamentals of the Java Programming Language course provides students who have little or no programming experience with the basics of programming using theJava programming language.

Java Programming Language Workshop (VC-SL-285)
Students use graphical user interface (GUI) design principles and network communications capabilities to code a functional Java application that interacts with a networked database server. The significant amount of lab time illustrates the workshop nature of this course.

Developing Applications for the J2EE Platform (CDJ-310A)
This course provides students with the knowledge to build and deploy enterprise applications that comply with Java 2 Platform, Enterprise Edition (J2EE) platform standards. Students are taught how to assemble an application from reusable components and how to deploy an application in the J2EE platform runtime environment.

See the course catalog

For More Information

Rate and Review
Tell us what you think of the content of this page.
Excellent   Good   Fair   Poor  
Comments:
Your email address (no reply is possible without an address):
Sun Privacy Policy

Note: We are not able to respond to all submitted comments.