Java Programming Home Page: Archive: Message #121

Date: Nov 07 2000 10:41:30 EST
From: "Java Programming" <javaProgramming-owner@listbot.com>
Subject: SW408 questions

Equals and ==

A student writes:
>1.  I'm not sure about the difference between .equals and == 
when comparing
>strings.
>
>If...
>
>String s1 = new String("a")
>String s2 = new String("a")
>
>then...
>s1.equals(s2)      -   true because they have the exact same 
letters in the
>exact same order, even though they are 2
>               separate objects?
>s1 == s2                 -   false because they are pointing to 2 
separate
>locations in memory (in other words, 2 separate                   
objects?)
>

The "equals" method compares values, but
== compares records.
For example, if the two strings, you made above, reference
different areas in memory, then == will fail, but equals will
return true. Many times, this is a cause for confusion.
The equals method can be written to compare for any
property. For example an apple could be equal to an orange if
they both have the same weight...as a result, you can write
your own equals method. However, you have no control over
how == is performed.

More below:

>2.  In Java, what is the difference between 'a' and "a"?  Can 
either be
>used?

They are different. A 'a' is a primitive data type called a char.
The "a" is a string or reference data type. When you print:
System.out.println("a"+'a'); it does not matter. It only matters
when you are trying to make use of the reference data types in
a more meaningful way.

For example,
public void f(String s) {}
f('a');
Will fail, because 'a', cannot be promoted to a String 
automatically
by the compiler.

---------------
A student writes:
1. I'm having a hard time understanding how to construct a Java 
program.
... write a few entire (short) programs on the board or
computer, then go through them (slowly) line by line, explaining 
what each
line does & why it is needed, I think it would make it more clear.
Additionally, I don't really understand the order of execution.  If 
you
could then go through the same code line by line in order of 
execution I
think that would help too.

public class HelloWorld {
// in java, all functions (which we call methods) reside in a class.
// public is a modifier that permits the class to be accessed
// public and class are both called reserved words
// class is a reserved word that introduces an identifier
// The identifier, "HelloWorld" may be of any length and is
// called the "class name"
// The "{" is called an open-brace and must be matched
// with a "}" closed brace.

public static void main(String args[]) {
// static is a reserved work that enables a method to
// be referenced directly. It is required for
// Java applications to have a method of the above
// type (i.e., public static void main(String args[]) {)
// in order for the application to be run.
// void is a reserved word in Java.
// It is used to indicate that the method does not return
// anything.
// main is a special method that indicates the point of
// entry for the flow-of-control in the java program
// "(" is called an open-parenthesis). It is matched
// by a ")" a closed parenthesis. The () pair is
// used in Java to pass parameters.
// String is a reference data type. It is not
// a reserved word, but a pre-defined class that
// is used to define arguments being passed into
// the command line (more on this later).
// args[] is an array of strings that contain the
// command line arguments.
      System.out.println("Hello world");
// The above is a Java statement.
// Java statements terminate with a ";" semicolon.
// This one makes reference to a class called "System".
// The System class contains an instance variable.
// An instance variable is also known as an instance of
// a class. In this case, the instance variable is called "out".
// One of the methods in the out instance variable is called
// "println". The println method takes a string argument,
// which is printed on the console.
} // end of class.

The program is run from the command line using
javac HelloWorld.java
java HelloWorld

Keep in mind that this is generally easier with a tool,
like codewarrior.

The javac command comes from the JDK, which should be
installed on your system. Javac is a compiler which takes
java source code on input and generates a class file on
output. The class file is called
HelloWorld.class.
java HelloWorld
invokes the main method in the HelloWorld class.
--------------------

A student writes:
when you say 'specification without implementation'
what does that mean?  I think it means you create a method with 
an empty
body?? ...Then you implement it elsewhere, ie, in a subclass?  
But I'm not
sure.

ANS:
public interface Runnable {
	public void run();
}

In the Runnable interface we see a run method that is
stated with out any implementation. As a result,
the compiler does not have any instructions for
exactly how to run.

To provide an implementation for the Runnable
interface, you must create a class. The class will use
the keyword "implements". For example:

public class Dog implements Runnable {
	public void run() {
		System.out.println("woof woof!");
	}
	public static void main(String args[]) {
		Dog d = new Dog();
		d.run();
	}
}

The program starts at the main method. A new instance
of the Dog class is created (new Dog()) and set to a variable,
called "d". The instance of the dog class is then "run".
Thus, Dog provides an implementation of the run method,
which was not present in the specification stated in the
Runnable interface.

Thus, we have a specification, without implementation, in
the Runnable interface. The Dog class is said to provide
the implementation of the specification.

--------------

A student asks:
What is picojava?

Ans: picojava is Sun's new Java chip. It is able to run
Java as a low-power, low-cost alternative in smaller
appliances (like toasters).

Regards,
  - DL