Java programming notes for Macintosh

From Wikiversity
Jump to navigation Jump to search

These notes describe the experiences of new Java programmers using the Wikiversity Introduction to Programming in Java pages.

OSX 10.4[edit | edit source]

This section has notes for OSX 10.4, but if you are using a different version of OSX, please start a new section and document any differences.

Introduction to Java[edit | edit source]

"Apple sets everything up for you" <-- this may be true, but I wondered if I have the latest version of the Java files for Macintosh. I went to the Apple website in search of information about Java versions and updates. According to that webpage at the Apple website, "Mac OS X includes the full version of Java 2, Standard Edition" and according to the introduction, "Java 2 Standard Edition (J2SE) ... is the one this course uses".

Location of the Terminal utility for Macintosh OSX

I also followed the link from Introduction to Programming in Java to the Java Programming book module at Wikibooks. I went to Java Programming/Installation and found instructions for how to check the version of the "Java Development Kit". The instructions start with "first open a terminal window". If you have never opened a terminal window on a Macintosh, look for the Terminal utility in the Utilities folder of the Applications folder. Here is what my Terminal session looked like when I entered "javac -version":

Last login: Tue Mar 13 15:44:45 on console
Welcome to Darwin!
john-schmidts-emac:~ johnschmidt$ javac -version
javac 1.5.0_07
javac: no source files
Usage: javac <options> <source files>
where possible options include:
-g                         Generate all debugging info
-g:none                    Generate no debugging info
-g:{lines,vars,source}     Generate only some debugging info
-nowarn                    Generate no warnings
-verbose                   Output messages about what the compiler is doing
-deprecation               Output source locations where deprecated APIs are used
-classpath <path>          Specify where to find user class files
-cp <path>                 Specify where to find user class files
-sourcepath <path>         Specify where to find input source files
-bootclasspath <path>      Override location of bootstrap class files
-extdirs <dirs>            Override location of installed extensions
-endorseddirs <dirs>       Override location of endorsed standards path
-d <directory>             Specify where to place generated class files
-encoding <encoding>       Specify character encoding used by source files
-source <release>          Provide source compatibility with specified release
-target <release>          Generate class files for specific VM version
-version                   Version information
-help                      Print a synopsis of standard options
-X                         Print a synopsis of nonstandard options
-J<flag>                   Pass <flag> directly to the runtime system

At the Apple downloads page, I searched for "Java" and found Java for Mac OS X 10.4 Release 5 as the first item. It says that it updates your J2SE 5.0 to version 1.5.0_07. I assume this means I have the latest version.

Hello world[edit | edit source]

The HelloWorld.java and HelloWorld.class files

"Open up a text editor"
I used TextWrangler to create my HelloWorld.java text file.

I created a "Java" folder in my documents folder and saved my HelloWorld.java text file in that folder.

"navigate to the folder that you saved HelloWorld.java to"
To find the path to my HelloWorld.java file, I did a drag and drop of the file's icon into the Terminal utility. I deleted the name of the file, just leaving the path in the Terminal input line. I used the left arrow key to move the input cursor to the start of the input line, and entered cd (change directory):

john-schmidts-emac:~ johnschmidt$ cd /Users/johnschmidt/Documents/Java

After changing the directory, the prompt in Terminal looked like:

john-schmidts-emac:~/Documents/Java johnschmidt$

I then entered "javac HelloWorld.java" as shown below:

john-schmidts-emac:~/Documents/Java johnschmidt$ javac HelloWorld.java

After entering that line into the Terminal utility, there was a new "HelloWorld.class" file in my Java folder. I then entered "java HelloWorld" as shown below:

john-schmidts-emac:~/Documents/Java johnschmidt$ java HelloWorld
Hello World!

The text "Hello World!" was returned in the next line of the Terminal window.

Integer Variables[edit | edit source]

In the Integer Variables lesson, it was mentioned that Java represents text characters as a type of integer according to the Unicode standard numbering system. Since any Java program is a collection of characters, is it possible to allow Java programs to treat themselves as integer data files? Could a Java program print itself? Could a Java program replicate itself?

Reading[edit | edit source]

Quine challenge[edit | edit source]

Confirm that you can find a Java program that will print itself. Describe your results below.

mshonle:~/Desktop$ java Quine > QuineOutput
mshonle:~/Desktop$ diff Quine.java QuineOutput
mshonle:~/Desktop$

I'm guessing that the above three lines first store the "printed" output of the program "Quine" in a container called "QuineOutput". Then "diff Quine.java QuineOutput" probably compares the text in the Quine.java source code file to the printed output stored in "QuineOutput". The last of the three lines above apparently indicated that there is no "diff, the program has printed its own source code.