JAVA BASICS
UMBC CMSC 331 Java
Comments are almost like C++
The javadoc program generates HTML API documentation from the javadoc style comments in your code.
/* This kind of comment can span multiple lines */ // This kind is to the end of the line /** * This kind of comment is a special * javadoc style comment */
UMBC CMSC 331 Java
An example of a class
class Person { String name; int age;
Variable Method
void birthday ( ) { age++; [Link] (name + ' is now ' + age); } }
UMBC CMSC 331 Java
Scoping
As in C/C++, scope is determined by the placement of curly braces {}. A variable defined within a scope is available only to the end of that scope. { int x = 12; /* only x available */
{ int q = 96;
/* both x and q available */ } /* only x available */ /* q out of scope */ }
UMBC CMSC 331 Java
This is ok in C/C++ but not in Java. { int x = 12; { int x = 96; /* illegal */ } }
4
[Link]
C:\UMBC\331\java>type [Link] // This is the Echo example from the Sun tutorial class echo { public static void main(String args[]) { for (int i=0; i < [Link]; i++) { [Link]( args[i] ); } } } C:\UMBC\331\java>javac [Link] C:\UMBC\331\java>java echo this is pretty silly this is pretty silly C:\UMBC\331\java>
Factorial Example
From Java in a Nutshell
/** * This program computes the factorial of a number */ public class Factorial { // Define a class public static void main(String[] args) { // The program starts here int input = [Link](args[0]); // Get the user's input double result = factorial(input); // Compute the factorial [Link](result); // Print out the result } // The main() method ends here public static double factorial(int x) { if (x < 0) return 0.0; double fact = 1.0; while(x > 1) { fact = fact * x; x = x - 1; } return fact; } }
UMBC CMSC 331 Java
// // // // // // // // // // //
This method computes x! Check for bad input if bad, return 0 Begin with an initial value Loop until x equals 1 multiply by x each time and then decrement x Jump back to the star of loop Return the result factorial() ends here The class ends here 6
JAVA Classes
The class is the fundamental concept in JAVA (and other OOPLs) A class describes some data object(s), and the operations (or methods) that can be applied to those objects Every object and method in Java belongs to a class Classes have data (fields) and code (methods) and classes (member classes or inner classes) Static methods and fields belong to the class itself Others belong to instances
UMBC CMSC 331 Java
Constructors
Classes should define one or more methods to create or construct instances of the class Their name is the same as the class name
note deviation from convention that methods begin with lower case
Constructors are differentiated by the number and types of their arguments
An example of overloading
If you dont define a constructor, a default one will be created. Constructors automatically invoke the zero argument constructor of their superclass when they begin (note that this yields a recursive process!)
UMBC CMSC 331 Java
Overloading, overwriting, and shadowing
Overloading occurs when Java can distinguish two procedures with the same name by examining the number or types of their parameters. Shadowing or overriding occurs when two procedures with the same signature (name, the same number of parameters, and the same parameter types) are defined in different classes, one of which is a superclass of the other.
UMBC CMSC 331 Java
Data hiding and encapsulation
Data-hiding or encapsulation is an important part of the OO paradigm. Classes should carefully control access to their data and methods in order to
Hide the irrelevant implementation-level details so they can be easily changed Protect the class against accidental or malicious damage. Keep the externally visible class simple and easy to document
Java has a simple access control mechanism to help with encapsulation
Modifiers: public, protected, private, and package (default)
UMBC CMSC 331 Java
10
Abstract classes and methods
Abstract vs. concrete classes Abstract classes can not be instantiated
public abstract class shape { }
An abstract method is a method w/o a body
public abstract double area();
(Only) Abstract classes can have abstract methods In fact, any class with an abstract method is automatically an abstract class
UMBC CMSC 331 Java
11