Lecture 2
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
What is Java?
Java History (1 of 2)
4
Java History (2 of 2)
5
What makes Java unique? (cont.)
Advantages of Java
6
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.
7
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.
8
Java Development Kit (JDK)
9
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
10
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
11
Testing the Installation
A few examples
12
Java Language Basics
Program.java:
13
Compiling from the Command Line
For example:
> javac Program.java
14
Running from the Command Line
15
Java Language Basics
Program.java:
16
Example Run in Windows
17
Elements of a Java Program
18
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
19
First Java Program (Hello World)
File name: HelloWorld.java
20