The Artima Developer Community
Sponsored Link

Objects and Java Seminar by Bill Venners
Classes and Objects
Lecture Handout

Agenda


Where Data is Stored


Object References

// Reference only
Account acct;

// Reference and object
LogFile log = new LogFile();

// Two ways to create strings
String s1 = new String("Hello, World!");
String s2 = "Hello, World!";

Primitive Types


Wrapper Classes

int i = 2001;
Integer i1 = new Integer(2001);
Integer i2 = i1;

Arrays


Local Vars Go Out Of Scope

 1 {
 2     int i = 10;
 3     // Only i in scope here
 4     {
 5         int j = 20;
 6         // Both i and j in scope
 7         // Note: can't redefine i here
 8     }
 9     // i still in scope, j out of scope
10 }

Objects Become Unreferenced

 1 {
 2     Integer i = new Integer(10);
 3     {
 4         Character c = new Character('q');
 5     }
 6     // c out of scope
 7     // Character object unreferenced
 8 }
 9 // i out of scope
10 // Integer object unreferenced

A Class Defines a Type


Fields


Methods


Parameters


Naming Conventions


Packages and Names


The Import Statement


Static Members


EchoArgs: A Java Application

 1 // In file classesandobjects/ex2/EchoArgs.java
 2
 3 public class EchoArgs {
 4
 5     public static void main(String[] args) {
 6
 7         int argCount = args.length;
 8         for (int i = 0; i < argCount; ++i) {
 9
10             if (i != 0) {
11                 System.out.print(' ');
12             }
13             System.out.print(args[i]);
14         }
15         System.out.println();
16     }
17 }

Exercises

Problem 1.

Test your setup: compile and run EchoArgs.java:
 1 // In file classesandobjects/ex2/EchoArgs.java
 2
 3 public class EchoArgs {
 4
 5     public static void main(String[] args) {
 6
 7         int argCount = args.length;
 8         for (int i = 0; i < argCount; ++i) {
 9
10             if (i != 0) {
11                 System.out.print(' ');
12             }
13             System.out.print(args[i]);
14         }
15         System.out.println();
16     }
17 }

Problem 2.

Create a Java application named Hello that prints out a greeting, such as the traditional, "Hello, world." Feel free to use the traditional greeting or come up with your own creative greeting.

Problem 3.

Change EchoArgs.java so that it changes all lower case letters to upper case before echoing them. For example, for the command line:
java UpperArgs Four sCore and seven years ago
this program would print:
FOUR SCORE AND SEVEN YEARS AGO

Problem 4.

Create a Java application named Counter that prints out the numbers between 1 and 100, inclusive. (i.e.: 1, 2, 3, ..., 99, 100)

Problem 5.

Change EchoArgs.java so that it prints out the arguments in reverse order, including the letters of the individual arguments. For example, for the command line:
java ReverseArgs one two three
this program would print:
eerht owt eno

Sponsored Links

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us