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:

Examples: C, C++
Perl Interprets [5/19]
Interpreters execute the source code directly:

Examples: BASIC, Perl
Java Uses a Virtual Machine [6/19]
Virtual machines use an intermediate byte code rather than
native object code:

Examples: Pascal p-code, Java, C#
- Compile (javac command); creates one or more "class files"
(.class extension)
- 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:

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.
- Must reside in file HelloWorld.java
- To compile: javac HelloWorld.java produces HelloWorld.class
- To run: java HelloWorld
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]
- "sandbox model" for applets
- Pointers are very limited: can't poke into the machine
- byte code verification: look for improper structures and/or control flows,
violation of access restrictions, etc.
- no access to local file system
- only communicate with originating host
- can't start other programs
- can't issue OS commands
- can't inquire about certain system properties
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]
boolean
: true
or false
; not 1 or 0 so:
-
if(x = 1){...}
//how will this compile?
byte, short, int, long, float, double
long aLong = 6;
int anInt = aLong;
//Is this Ok?
int anInt = 6; long aLong = anInt;
//Is this?
double aDouble = 9.9999; int anInt = (int)aDouble;
//What does aDouble
equal?
char
(a Unicode character)-
Unicode is two bytes, ASCII is one, doesn't ASCII cover the whole alphabet?
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 is a class, not an array of char's
- String index starts from 0
- String are immutable: "A String constant"
- String concatenation: s1 + s2 and s1 += s2
- s.length(): the length of a string s.
- s.charAt(i): character at position i.
- Many more. Let's take a look.
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