CSC 314 Lecture Notes


Week 1: Java Basics



Are You In The Right Room? [1/19]
Class Policies [2/19]
What's Java [3/19]
C/C++ Compiles [4/19]
Perl Interprets [5/19]
Java Uses a Virtual Machine [6/19]
JIT and HotSpot[7/19]
Hello, world! [8/19]
Some DOS/Windows Hints [9/19]
Very Brief Intro to Applets [10/19]
Security for Downloaded Code (i.e. Applets) [11/19]
Your Shrike Account is an HTML Server [12/19]
Hello World Applet [13/19]
Primitive Types [14/19]
Keyword final [15/19]
The java.lang.Math Class [16/19]
The java.lang.String Class [17/19]
The javax.swing.JOptionPane Class [18/19]
Exam 1 Next Week [19/19]

Are You In The Right Room? [1/19]



Course: CSC314 Programming in Java

Instructor: John Jungman (I usually go by my middle name, Greg)

Hypernews Forum! USE IT

E-mail: jjungman@cs.depaul.edu

Office: CST 631

Office Hours: listed here

Phone: 773.576.0113


Class Policies [2/19]


Grading: 50% Weekly Exams and 50% Homeworks
Exams:
Weekly exam, open book/note, 100 points each. EACH STUDENT GETS 100 POINTS EXTRA CREDIT AUTOMATICALLY!!!!
Homeworks:
Weekly homework assignment due BEFORE CLASS, 100 points each. Help from tutors, peers, hypernews forum are all fair game... BUT YOU BETTER UNDERSTAND IT FOR THE TEST!
Best possible score: 1900/1800

What's Java [3/19]


Object Oriented: No stand alone functions, everything is a method. Is C++ Object Oriented?

Standardized Libraries: Providing high level support for threads and networking, Check'em out

Safer: No pointer arithmetic, Garbage Collection, Strongly Typed

Platform Independent: "Compile once, run everywhere."

Why should you learn Java?


C/C++ Compiles [4/19]


Compilers use the traditional compile/link/run strategy:

Compile then Link then Execute

Examples: C, C++


Perl Interprets [5/19]


Interpreters execute the source code directly:

Load then Interpret

Examples: BASIC, Perl


Java Uses a Virtual Machine [6/19]


Virtual machines use an intermediate byte code rather than native object code:

Compile then Link then Execute (Bytecode)

Examples: Pascal p-code, Java, C#

  1. Compile (javac command); creates one or more "class files" (.class extension)
  2. Run (java command): the program is run by the Java interpreter

Is this fast?


JIT and HotSpot[7/19]


Just-in-time compilers are like VMs, but compile the byte code to native code:

Compile then Link then Compile then Execute

Examples: Java, C# (probably)

Variation: HotSpot for JDK1.3, compile most used methods only.


Hello, world! [8/19]


import java.io.*; public class HelloWorld { public static void main(String[ ] args){ System.out.println("Hello, world"); } } //We could also do this java.io.System.out.println("Hello, world"); w/o the import. What do each of the words in this program do?

Some DOS/Windows Hints [9/19]


This is not an OS class, but just to make sure you have enough to get around:

cd name_of_directory : CD stands for change directory, you need to change directory one directory at a time. Here you are moving into a sub-directory contained within your current directory. Let's do an example in class.

cd.. : This moves you out of the sub-directory into the super-directory

.bat: a .bat file is a saved series of commands to run in sequence, all you do is type the name of the .bat file and all the commands listed in the file are run in order. Create/open.bat files in/from notepad. Let's do an example in class

autoexec.bat: Sets the OS environment when you boot. This is the file where you add the jdk (as described in Chapter 2) and it can be slightly different for each system.


Very Brief Intro to Applets [10/19]


-Run programs on your browser's JVM.
-Download .class files from the server and run on the clients' processor.
-Very limiting security!


Security for Downloaded Code (i.e. Applets) [11/19]



Your Shrike Account is an HTML Server [12/19]


If you don't have a public_html directory on your shrike account, log-on, and type this into your Unix shell exactly:

cd ~
mkdir public_html
chmod 711 ~
chmod 711 public_html
cd public_html
touch index.html
chmod 644 index.html


Now you can load .html files and .class (java byte code) files onto your server! Your front page will be index.html and can be found at:
http://shrike.depaul.edu/~yourloginname

Let's do a demonstration in class, pay attention, you'll need this for the homework!

HTML is not part of this class, but you may want to learn a tiny bit of html on your own, it's easy and fun, here's a good tutorial:
http://www.davesite.com/webstation/html/index.shtml

Hello World Applet [13/19]


import java.applet.Applet; import java.awt.Graphics; public class HelloWorld extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); } }

Is that all it can do?

Coding: Primitive Types [14/19] [14/30]



Keyword final [15/19]


If you declare a variable with final it's value cannot be changed.

public class Constants{ public static void main(String[] args){ final double PI = 3.14; double radius = 2.98; System.out.println("Circumference: " + ((PI*radius)*(PI*radius)) + " inches"); PI=5.4; // error!!! } } What if you want to use the constant in several methods of a class?

public class Constants2{ public static final double PI = 3.14; public static void main(String[] args){ double radius = 2.98; System.out.println("Circumference: " + ((PI*radius)*(PI*radius)) + " inches"); PI=5.4; // error!!! } public int findVolume(){ ...use PI } } Why do we use static here?

The java.lang.Math Class [16/19]


Math: A class that holds methods and constants that we can use as a tool. Let's take a look at it!

...System.out.println("Circumference: " + Math.pow(Math.PI*radius, 2));

We need no package import. Why not?

The java.lang.String Class [17/19]


String name = "Joe";
boolean x = (name == "Joe");
//x may be false!
boolean y = (name.equals("Joe"));
//y is true.

The javax.swing.JOptionPane Class [18/19]


import javax.swing.*; //Why do we need this? public class InputTest{ public static void main(String[] args){ String name = JOptionPane.showInputDialog ("What is your name?"); String input = JOptionPane.showInputDialog ("How old are you?"); int age = Integer.parseInt(input); // Where can we find info on the "Integer" class? System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1)); System.exit(0); } }

Exam Next Week [19/19]


-First 20 minutes of class
-Covering all of Chapter 3 (which is pretty long) and this lecture
-Open book/open note (but not a lot of time to look things up)
-Homework due before class starts