0% found this document useful (0 votes)
67 views20 pages

CS175 Lecture 2

This document provides an overview of Java programming and the Java language. It discusses what programming is, why learn programming, and difficulties for beginners. It also covers Java terminology, history, advantages, platforms, and how to write programs in Java. The key topics covered include object-oriented programming, the Java virtual machine, portability, and the Java development kit.

Uploaded by

Pasiano Nemes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views20 pages

CS175 Lecture 2

This document provides an overview of Java programming and the Java language. It discusses what programming is, why learn programming, and difficulties for beginners. It also covers Java terminology, history, advantages, platforms, and how to write programs in Java. The key topics covered include object-oriented programming, the Java virtual machine, portability, and the Java development kit.

Uploaded by

Pasiano Nemes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

CS175: Programming in Java

Lecture 2

Dr. Juma Lungo


[email protected]

What is Programming?

l  A magic spell you cast over a computer to get it


to do what you want.
l  Developing software applications & games

l  Software is not limited to PC


•  most complex systems run software
•  smart phones, game devices, thermostats, even DVD
players

1
Programming …

l  is NOT a boring or repetitive activity


l  does NOT require you to sit in a dark room and
type at a computer all day!
l  does NOT (usually) involve complex math

l  requires logical thinking – technical common sense

l  write minimal code & combine with existing


components to build new applications
l  Solves customers’ problems & improves quality of
life for everyone.

Why learn programming?


l  It’s fun
l  Software Engineers get great pay!
l  Less stressful compared to several other high
paying jobs – room for trial & error
l  Automation continues…
l  Computers touch our lives more & more every
day…
l  More component based programming à always
room for simple programs to do large tasks!

2
Learning to program:Difficulties for
beginners
1. Syntax errors
u  struggle for hours to fix syntax errors

u  Lose confidence

u  Frustrating experience

u  Run away & never come back if possible!

2.Logic errors
l  Logic is simple for small programs.

l  It requires logical thinking.

How to reduce difficulties for


beginners?
u  Use the “state of the art” tools like NetBeans IDE
(Integrated Development Environment) to help
us!
u  Some other IDEs are Eclipse, JGRASP, …
(Search for “Java IDE” in the web to learn more)
u  IDEs take care of mundane steps so that we can
focus on learning and programming.
u  Also, take advantage of expanded libraries
provided by new languages and use them as
building blocks.

3
A few examples

• Recipe to make your favorite food


• Coming to college from home
What is common about these
activities?
Sequence

Java Language Basics

The simplest Java program:

Program.java:

4
Java Language Basics

The simplest Java program:


This program will do nothing.
Program.java:

Compiling from the


Command Line
To compile, type javac <source file> where “<source
file>” is the name of the file you want to compile.

For example:
> javac Program.java

This will output a file named “Program.class”.

5
Example Compilation on
Windows

Running from the Command


Line
Once compiled, use the java command to run. The java
command takes a single argument which is the name
of the class containing a main() method that you wish
to execute. You only need to specify the name of the
class, not the full name of the .class file; if you
specify the full name of the .class file, you will get an
error.

> java Program.class


Exception in thread "main" java.lang.NoClassDefFoundError: Program/class

> java Program


//execution of Program

6
Example Run in Windows

Java Language Basics

A slightly more useful example:


This program will print “My program is running!” to the screen.

Program.java:

7
Running from the Command
Line
Same steps as before:

> javac Program.java

> java Program

Example Run in Windows

8
Elements of a Java Program

Everything must be declared inside a class.


Classes in Java are analogous to classes in C++.

Program.java: class Program

Elements of a Java Program

Classes can contain fields and methods.


Methods in Java are the equivalent of member functions in C++.
Fields in Java are the equivalent of data members in C++.
Program.java: main() method

9
Elements of a Java Program

Members are either class or instance.


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: main() (class method –
declared static)

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

Every class and member must have a visibility.


A visibility indicates what places in a program the class, field, or method is accessible
from. Visibility keywords: public, private, and protected.
Program.java: visibility specifiers

First Java Program (Hello World)


File name: HelloWorld.java

public class HelloWorld {


public static void main(String args[]){
System.out.println(“Hellow World!”);
}
}

11
What is Java?

Java is a cross platform, object-oriented


programming language.

It was created by Sun Microsystems and first


made publicly available in January 1996.

Java History (1 of 2)

Java was designed and implemented by James Gosling.

The Java language is specified by the book The Java Language


Specification, Third Edition by James Gosling, Bill Joy, Guy Steele, and
Gilad Bracha (ISBN: 0321246780).

It started in 1991 as a project, code-named “Green”, to develop


a platform for consumer devices, such as cable tv
switchboxes. Only later when the world wide web caught on in
1995 was the project refocused towards creating a general
purpose programming language.

12
Java History (2 of 2)

Java was originally called Oak, because of a tree


outside of the window of James Gosling’s office at
Sun, and was later renamed to Java when it was
discovered a language named Oak already existed.
The name “Java” was chosen because the creators of
the language often discussed ideas for the language
at a local coffee shop.

What makes Java unique?

Java is run inside a piece of software known as a Java


Virtual Machine.
In many languages, such as C and C++, the source
code (.c and .cpp files) is portable. What makes Java
unique is that Java’s executable code is also
portable (.class files).
This means that the same application can be run
without modification on any system that has a Java
Virtual Machine without being recompiled.

13
What makes Java unique?
(cont.)

Java programs require a Java Virtual Machine to run.

The Java Virtual Machine is written in C and C++, and


has been ported to many platforms, including
desktop computers running Windows and Mac OS X,
to high-end servers running Solaris and Linux, to
mobile phones and PDA’s.

Advantages of Java

l  Available on almost all platforms


l  Standardized (by Sun)
l  Well documented
l  Makes many traditionally difficult tasks very easy,
including networking, multithreading, and cross-
platform GUI programming (GUI stands for graphical
user interface)
l  Very large API
l  Most skills learned while using Java apply elsewhere

14
Java Terminology

Java Virtual Machine (JVM) – An abstract machine architecture specified by


the Java Virtual Machine Specification, Second Edition by Tim Lindholm
and Frank Yellin (ISBN: 0201432943).

Java Runtime Environment (JRE) – A runtime environment which


implements Java Virtual Machine, and provides all class libraries and
other facilities (such as JNI) necessary to execute Java programs. This is
the software on your computer that actually runs Java programs.

Note:
These two terms (JVM and JRE) are often used
interchangeably, but it should be noted that they are not
technically the same thing.

More Java Terminology

Java Development Kit (JDK) – The basic tools necessary to


compile, document, and package Java programs (javac,
javadoc, and jar, respectively). The JDK includes a complete
JRE.

15
Java Platforms

There are three main platforms for Java:


l  Java SE (short for Standard Edition) – runs on desktops
and laptops

l  Java ME (short for Micro Edition) – runs on mobile devices


such as cell phones

l  Java EE (short for Enterprise Edition) – runs on servers

Writing Programs in Java

Tools required to write Java programs:


l  Java Development Kit 6 (JDK 6). This can be obtained from
java.sun.com. Specifically:
http://java.sun.com/javase/downloads/index.jsp

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)

Comes with all tools necessary to compile and


run Java programs, including a complete Java
Runtime Environment (JRE) (located in the
jdk/jre directory).
Get the most recent version from:
http://java.sun.com/javase/downloads/index.jsp

The Class Path

The class path is a list of locations (folders) that are


searched for classes when the Java Virtual Machine
attempts to locate a referenced class.

The CLASSPATH variable is an environment variable


that specifies the class path on a given system. The
CLASSPATH must be set and must include the
current directory in order to run Java programs from
the command line with the java command.

17
Setting the Class Path

The CLASSPATH variables simply contains a list of


absolute directories, separated by a system
dependent separator. On Windows a semicolon “;”
is used to separate directories, on Linux, Unix, Mac
OS X, and Solaris, a colon “:” is used to separate
directories.

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.

About Class Paths

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

The Java compiler (the javac command) automatically looks in


the current directory when compiling files, but the Java
interpreter only looks in the current directory if the class path
is undefined. You would then be able to compile classes, but
be unable to run them.

18
More About Class Paths

The standard Java SE class libraries are located in


jre/lib/rt.jar
Any class libraries (.jar files) located in the jre/lib/
ext directory are also available to Java programs.
You don’t need to worry about adding jre/lib/
rt.jar or jre/lib/ext to your classpath because
both of these are searched automatically by both
java and javac

Example class path

Here are some example 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

Once you have installed the JDK, set the


CLASSPATH and JAVA_HOME environment
variables, and updated your PATH variable,
type the following on a command line:
> javac –version

You should get something like the following


output:
javac 14.0.1

20

You might also like