Transcript Document
Java Basics
1
Compiling
A “compiler” is a program that translates from
one language to another
Typically from easy-to-read to fast-to-run
e.g. from C or Assembly to machine code
Java must be (explicitly) compiled before it is
run
The Java compiler turns Java source code
(.java) into Java bytecode (.class)
2
The Java Platform
The Java Virtual Machine (JVM) is
responsible for running bytecode
The idea: bytecode can be interpreted quickly
The same bytecode can be interpreted on
any architecture: write once, run anywhere
Code (C,C++) compiled to machine code
must be compiled to a specific system
3
The Java Language
Created by Sun Microsystems
Introduced in 1995, initial popularity grew due
to Internet applications
Excitement surrounding Java applets
Confusion with Javascript
Steady rise in popularity has continued for
“better” programming reasons
4
A Historical Interlude: The Java Team
Java originally intended to be used on “smart”
consumer electronics
Bill Joy
James Gosling (“the father of Java”)
Founded Sun, 1982
Intelligent robots will replace humanity in the near future…
University of Calgary grad
First JVM, compiler, interpreter
also developed Emacs
Patrick Naughton
Arrested in late 90s on child predator charges
Not mentioned so much as a founding father anymore
5
The Java Language (cont’d)
… is a high-level programming language
… is very object oriented
… is similar to C++ and C
… typically compiled to Java bytecode
… is often confused with the Java Platform,
but these are two different aspects of “Java”
6
Syntax and Semantics
The syntax rules of a language define how
we can combine reserved words, symbols,
and identifiers
The semantics of a program statement define
what the statement means
Problem with program syntax = “error”
Problem with program semantics = “bug”
7
Java Program Structure
A Java program consists of:
One or more classes
A class contains one or more methods
A method contains program statements
We will explore these terms in detail
8
Java Program Structure
//
comments about the class
public class MyProgram
{
class header
class body
Comments can be placed almost anywhere
}
9
Java Program Structure
//
comments about the class
public class MyProgram
{
//
comments about the method
public static void main (String[] args)
{
method body
method header
}
}
10
Hello World
//
HelloWorld.java
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println(“Hello World!”);
}
}
11
Hello World
//
HelloWorld.java
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println(“Hello World!”);
}
}
Creates a “class” called HelloWorld
Compiled to HelloWorld.class
Classes used to define objects… later
12
Hello World
//
HelloWorld.java
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println(“Hello World!”);
}
}
The “main” method is where it starts to run
Ignore “public static void” and “String[] args” for
now
13
Hello World
//
HelloWorld.java
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println(“Hello World!”);
}
}
Contains one “statement”
The System.out.println function comes from
the Java “class library”
Ends with a semicolon (all statements do)
14
Compiling and Running
Create the file HelloWorld.java in a text editor
Compile:
Run:
javac HelloWorld.java
java HelloWorld
Output:
Hello World!
15
Comments
Three kinds of comments:
// a one-line comment
/* a multi-line
comment */
/** a javadoc comment */
To simplify: comments are good
16
Reserved Words and Identifiers
Reserved words are specified by the
language
All Java reserved words are in the text
Identifiers are specified by a programmer
Maybe you: e.g. HelloWorld
Maybe someone else: e.g. println
17
Restrictions and Conventions
Restriction
Identifiers can not start with a digit
Conventions
Title case for class names: HelloWorld
Uppercase for constants: MAX
18
White Space Conventions
Idea: make programs easy to read
Use consistent indentation
Use blank lines and comments to visually
separate methods
The fact that it compiles doesn’t make it
right…
19
Strong Typing
Java is a “strongly typed” language
All variables and values have a specific type
Type is known when the program is
compiled…. before it is run
So all variables and values must be declared
with a type before being used
20
Declaring Variables
Syntax:
<variable declaration> ::= <type> <declarator>, …. ;
<declarator> ::= <identifier>
<declarator> ::= <identifier> = <expression>
Examples:
int count1, int count 2;
int count = 0;
String course1 = “CMPT 126”;
21
Assignment
We use the = operator for variable
assignment
Initialization is a special case
When a value is assigned, the old value is
overwritten
In Java, we use the final modifier to
declare a variable constant
final int MAX_HEIGHT = 6;
22
Primitive Data Types in Java
Four integer types:
Two floating point types
float, double
One of them is for characters
byte, short, int, long
char
One of them is for boolean values
boolean
23
Expressions and Assignment
An expression is a combination of one or
more operators and operands
Arithmetic operators: +, -, *, /, %
Use the normal order of operations
e.g. int exp = 2 * 5 +7;
count = count + 1;
count++;
Boolean operators: &&, ||
24
More Assignment Operators
x += y is equivalent to x = x + y
Also:
-=
*=
/=
%=
25
Data Conversion
Non-matching types can be converted
A widening conversion is automatic
A narrowing conversion may lose information
e.g. from short to int
e.g. from float to int
Three kinds of conversion:
Assignment
Promotion
Casting
26
Assignment Conversion
final int dollars = 6;
double money;
money = dollars;
Only works for widening conversion
27
Promotion
int count = 2;
float mass = 18.342;
mass = mass / count;
Passing count to an operator that expects
floating point values
28
Casting
float mass = 18.342;
int roundedmass = (int) mass;
Casting works for widening and narrowing
In this example, decimal part is just lost
Note: this does not actually round
29
Object Types
The primitive types aren’t really enough
Java also allows object types, or classes
Object variables hold references to objects
Typically capitalized
The declaration only creates a reference
This is different from primitive types
Variables of primitive type hold a value
30
Example: String Objects
We have already seen one object type in
Java: String
A String object is a list of characters
e.g. “Hello world!” or “My name is Aaron”
Can be passed to print or println
Can be concatenated using the (+) operator
e.g. “Hello world! ” + “My name is Aaron”
“I can also append numbers, like “ + 2
31
Object Instances
We must create a new “instance” of an object
to store something
Each object type has a constructor (more later)
Create instances using the reserved world new
e.g. course = new String(“CMPT 126”);
This creates a new String in memory
It stores the characters “CMPT 126”
The assignment sets course to refer to this
instance
32
References and Instances
String course;
course:
new String(“CMPT 126”)
CMPT 126
course = new String(“CMPT 126”);
course:
CMPT 126
33