- Forward


Java
An Introduction for Programmers


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Identifiers
Back SMYC Forward
  • Restrictions:
    • Must start with a letter
    • Can contain letters, digits, and '_'
    • Are case-sensitive
  • Common Style Specification:
    • Must start with a lowercase letter
    • Must be descriptive
Comments
Back SMYC Forward
  • Line Based:
    • Any text between a // and the end of the line is a comment
  • Block:
    • Any text between a /* and a */ is a comment
Some Atomic/Primitive/Fundamental Types in Java
Back SMYC Forward

Type Memory Range
byte 1 byte -128 to 127
short 2 bytes -32,768 to 32,767
int 4 bytes -2,147,483,648 to 2,147,483,647
float 4 bytes
double 8 bytes
char 2-4 bytes Unicode
boolean true or false

Variable Declarations in Java
Back SMYC Forward
  • Syntax: Click here for information.
    • type variable [, variable]... ;
  • Examples:
    • boolean done;
    • double expenses, income;
    • int numberOfChecks, styleNumber;
Assignment Operator
Back SMYC Forward
  • Assignment:
    • Placing a value into the space identified by a variable/constant
  • Syntax: Click here for information.
    • variable = literal|expression ;
  • A Detail:
    • Implicit loss of precision is not allowed
The final Modifier in Java
Back SMYC Forward
  • Purpose:
    • Indicate that a value can only be assigned to a variable once
  • Syntax: Click here for information.
    • final type variable [, variable]... ;
  • Examples:
    • final boolean CORRECT;
    • final double BUDGET;
    • final int CAPACITY;
The if Statement
Back SMYC Forward
  • Syntax: Click here for information.
    • if (boolean_expression) statement [else statement]
  • Note:
    • statement can be a simple or block statement
while Loops
Back SMYC Forward
  • Syntax: Click here for information.
    • while (boolean_expression) statement
  • Note:
    • statement can be a simple or block statement
do-while Loops
Back SMYC Forward
  • Syntax: Click here for information.
    • do statement while (boolean_expression);
  • Note:
    • statement can be a simple or block statement
for Loops
Back SMYC Forward
  • Syntax: Click here for information.
    • for (initialization; boolean_expression; update) statement
  • Note:
    • statement can be a simple or block statement
Arrays
Back SMYC Forward
  • Declaration:
    • Syntax: BaseType[] ArrayName;
    • Example: int[] income;
  • Memory Allocation:
    • Syntax: ArrayName = new BaseType[Length];
    • Example: income = new int[20];
  • Accessing Elements:
    • Syntax: ArrayName[Index]
    • Example: income[5] = 250000;
Arrays Utility Class
Back SMYC Forward

The Arrays class (in the java.util package) contains several static methods that are very convenient when working with arrays.

import java.util.Arrays; int target; int[] a; a = new int[100]; Arrays.fill(a, 0); // Fill a with zeroes Arrays.fill(a, 1, 10, 99) // Fill a[10] thru a[99] with ones ... Arrays.sort(a); // Sort a in ascending order // Searches for 5 in a and returns 5 if found // The array must be sorted // target = Arrays.binarySearch(a,5);
Classes
Back SMYC Forward
  • Classes:
    • accessibility class name { [attributes]... [methods]... }
  • Details:
    • The name of the file containing a class must be exactly the same as the name of the class (with .java appended)
    • The accessibility of a class is most frequently public
Classes: Accessibility of Members
Back SMYC Forward
  • public:
    • Accessible to all objects in all classes
  • protected:
    • Accessible to all objects in the containing class and specializations
  • private:
    • Accessible to all objects in the containing class
Classes: Declaring Methods
Back SMYC Forward
  • Syntax:
    • accessibility [static] type name([type parameter], ...)
  • Examples:
    • public static double circleArea(double diameter)
    • public void update()
Classes: Class-Members
Back SMYC Forward
  • Definition:
    • Attributes or methods that belong to a class (or all of its instances) rather than individual instances of a class
  • Implication:
    • They can be used without instantiating an object
  • Declaration:
    • Must be declared static
Classes: An Example
Back SMYC Forward
javaexamples/oopbasics/pictureframe/overloadedmethods/PictureFrame.java
 
Enumerated Types
Back SMYC Forward
  • enums:
    • accessibility enum name { instances, [attributes]... [methods]... }
  • Details:
    • Are generally the same as for classes
Enumerated Types: An Example
Back SMYC Forward
javaexamples/oopbasics/months/v2/Month.java
 
Specialization
Back SMYC Forward
  • Declaring a Subclass:
    • Use extends in the declaration of the class
  • Accessing Members in a Superclass from a Subclass:
    • Use super as the left-side operand of the . operator
  • Calling the Constructor in a Superclass from a Subclass:
    • Call super() (which must be in the first statement in a constructor of the subclass)
Preventing Specialization
Back SMYC Forward
  • A Particular Method:
    • A method that is declared to be final can't be overridden by a subclass
  • An Entire Class:
    • A class that is declared to be final can't be specialized (i.e., a class that extends a final class will not compile)
A Peculiarity of Specialization in Java
Back SMYC Forward
  • The Object Class:
    • All classes that don't explicitly extend a class implicitly extend the Object class
  • Implication:
    • All objects have clone(), equals() and toString() methods, though they don't always do what you want
Abstract Classes/Methods
Back SMYC Forward
  • Syntax of an Abstract Class:
    • accessibility abstract class name { body }
  • Syntax of an Abstract Method:
    • accessibility abstract type name([parameter...]);
Interfaces
Back SMYC Forward
  • Syntax:
    • accessibility interface name { body }
  • Contents of the body:
    • Only method signatures (i.e., only abstract methods)
  • Concrete Classes that Realize an Interface:
    • Declaration: accessibility class name implements iface1[, iface2]...
    • Implementation: Must implement all methods (in all interfaces)
Parameterized Classes
Back SMYC Forward
  • Parameterized Types in the Class Definition:
    • Class declarations can be parameterized
    • The parameters of the class are listed in angle brackets
    • Example: public class Identified<T>
  • Parameterized Types in Attributes and Methods:
    • Attributes and methods can use the parameterized type as if it were an actual type
Parameterized Classes: An Example
Back SMYC Forward
javaexamples/parameterized/Identified.java
 
Parameterized Classes: Using
Back SMYC Forward
javaexamples/parameterized/Driver.java (Fragment: 0)
 
Bounded Type Parameters
Back SMYC Forward
  • The Situation:
    • When constructing the parameterized class, you want to restrict the types that can be used as type arguments (i.e., the classes that can be bound to)
  • The Solution:
    • List the parameter name followed by extends, followed by the bounding type
  • A Note:
    • The term extends was probably a bad choice since any class that either extends the bounding type or implements the bounding type can be used
Bounded Type Parametetrs: An Example
Back SMYC Forward
javaexamples/parameterized/bounded/Identified.java
 
There's Always More to Learn
Back -