CS175 Lecture 2
CS175 Lecture 2
Lecture 2
What is Programming?
1
Programming …
2
Learning to program:Difficulties for
beginners
1. Syntax errors
u struggle for hours to fix syntax errors
u Lose confidence
u Frustrating experience
2.Logic errors
l Logic is simple for small programs.
3
A few examples
Program.java:
4
Java Language Basics
For example:
> javac Program.java
5
Example Compilation on
Windows
6
Example Run in Windows
Program.java:
7
Running from the Command
Line
Same steps as before:
8
Elements of a Java Program
9
Elements of a Java Program
Field example.
Instance members exist for every object (instance) of a class (this is the default).
Class members only have one copy per class, and are declared with static.
Program.java: number field
number is an instance
field because it is not
declared static
10
Elements of a Java Program
11
What is Java?
Java History (1 of 2)
12
Java History (2 of 2)
13
What makes Java unique?
(cont.)
Advantages of Java
14
Java Terminology
Note:
These two terms (JVM and JRE) are often used
interchangeably, but it should be noted that they are not
technically the same thing.
15
Java Platforms
After downloading and installing the most recent JDK, you must
set your CLASSPATH and JAVA_HOME environment
variables. JAVA_HOME should be the location of your JDK
installation.
If you want to use Java from the command line, you must also
add the jdk/bin directory to your PATH variable.
16
Java Development Kit (JDK)
17
Setting the Class Path
The reason for the discrepancy between Windows and Unix flavored
operating systems is that absolute file paths in Windows can contain
colons “:”, for example “C:\Program Files”, so colons can not be used to
identify a boundary between path entries as it can be in Unix. This is the
same reason why the PATH separator is different in Windows and Unix.
The class path must contain the current directory “.” or the Java
interpreter (the java command) will be unable to find
compiled class files and you will get an error similar to:
Exception in thread "main" java.lang.NoClassDefFoundError: SomeClass/java
18
More About Class Paths
Windows:
CLASSPATH=.;C:\Program Files\Java\jre1.6.0_01\lib\rt.jar;C:
\Tomcat-6.0.13\lib\servlet-api.jar;C:\Tomcat-6.0.13\lib\jsp-api.jar;C:
\Program Files\Java\jre1.6.0_02\lib\ext\QTJava.zip
Linux:
CLASSPATH=.:/usr/java/jdk1.5.0_03/jre/lib/rt.jar
19
Testing the Installation
20