Professional Documents
Culture Documents
Introduction to Java
High-level language.
Paradigm: Object-Oriented.
Introduction to Java
Some languages are compiled (C/C++)
4 / 58
Introduction to Java
Some languages are compiled (C/C++)
Introduction to Java
Some languages are compiled (C/C++)
Introduction to Java
Java Syntax
Similar to C/C++.
Variables: byte, short, int, long, float, double, char,
boolean
Java Operators
Numerical: +, -, *, /, %
Boolean: >, <, <=, >=, ==, !=, !, instanceof
Others: ++, --,
Bitwise operators: & (AND),
^ (XOR),
| (OR),
<<, >> (shift)
9 / 58
if statements
if (boolean-expression) {
…
}
else if (…) {
…
}
else {
…
}
10 / 58
Conditional expression
boolean-expression ? expression1 : expression2
Example:
int x = 3;
int y = (x > 0) ? 1 : 5;
11 / 58
switch statement
switch (byte/short/char/int/String/etc.) {
case x:
break;
case y: …..
break;
case …:
break;
default: …..
}
12 / 58
Loops
while (boolean-expression) {
// do something
}
do {
// something
} while (boolean-expression);
Difference?
13 / 58
Loops
for (expression1; boolean-expression; expression2) {
// do something
}
Example:
int i;
for (i = 0; i <= 10; i++)
i++; // Value of i?
14 / 58
keyword break
break = used to “break out” of a loop.
Rest of the code is not executed.
int sum = 0;
int number = 0;
while (number < 20) {
number++;
sum += number;
if (sum > 100)
break;
}
15 / 58
keyword continue
continue = used in loops.
Break out of current statement, but continue with the rest of the
loop.
int sum = 0;
int number = 0;
while (number < 20) {
number++;
if (number == 10)
continue;
sum += number;
}
16 / 58
Methods
Program modularity.
Methods
modifier returnValueType name (list of parameters) {
...
}
Methods - modifiers
public = can be called by any class
private = can only be called by the class
also protected (will look at it later)
default modifier ???
ClassName.method(...)
The Math class – Math.sin(), Math.acos(), etc.
19 / 58
Methods
returnValueType = Can be primitive, class, etc.
Even void (nothing to return)
Recursion
Methods calling themselves
Recursion
public int fibonacci (int n) {
if (n == 0 || n == 1) // base case(s)
return n;
else
return fibonacci (n-1) + fibonacci (n-2);
}
22 / 58
Recursion
Later on we will look at recursion for other
algorithms (searching/sorting)
23 / 58
Method overloading
Can have multiple methods with the same name.
Showed “max” method with ints
Write one with double:
Commenting
Single-line:
// This is a single-line comment
Multi-line:
/* This is going to be on
multiple lines */
Object-Oriented Programming
Paradigm which uses “objects” and “classes”.
26 / 58
Object-Oriented Programming
Paradigm which uses “objects” and “classes”.
Object-Oriented Programming
Paradigm which uses “objects” and “classes”.
Object-Oriented Programming
Paradigm which uses “objects” and “classes”.
Object-Oriented Programming
Most modern languages support OOP
30 / 58
Object-Oriented Programming
Most modern languages support OOP
Alternatives:
Procedural/Imperative ( C )
Functional (Lisp/PROLOG)
31 / 58
Classes in Java
A template for objects of the same type.
32 / 58
Classes in Java
A template for objects of the same type.
Objects in Java
Unique identity, state and behavior.
34 / 58
Objects in Java
Unique identity, state and behavior.
Objects in Java
Unique identity, state and behavior.
Example
public Circle {
// Properties
private double radius;
// Constructors
public Circle() {
radius = 0.0;
}
// Methods
public double getArea() {
return radius * radius * Math.PI;
}
}
37 / 58
Properties
// Properties
private double radius;
Properties
// Properties
private double radius;
Properties
// Properties
private double radius;
Properties
Add get/set methods:
41 / 58
Properties
Add get/set methods:
public getRadius() {
return radius;
}
42 / 58
Properties
Add get/set methods:
public getRadius() {
return radius;
}
Properties
Add get/set methods:
public getRadius() {
return radius;
}
this keyword
Refers to the property of this specific class
Constructors
// Constructors
// default constructor
public Circle() {
}
Constructors
Special kind of method
Constructors
Initialization example:
Constructors
Should always provide a default constructor.
Does not take in any properties
Reference Variables
Objects accessed via reference variables.
Reference Variables
Can declare without initializing
Initialize later:
Accessing properties/methods
referenceName.property // only if public!
referenceName.method(...)
Example:
Revisiting static
Variables in classes can be static
Revisiting static
Example:
public class Student {
private String name;
public int ID;
this.ID = ++numberOfStudents;
}
}
54 / 58
Revisiting static
Example:
Revisiting static
Example:
System.out.println(student1.ID); // Output?
System.out.println(student2.ID); // Output?
56 / 58
Revisiting static
Example:
System.out.println(student1.ID); // Prints 1
System.out.println(student2.ID); // Prints 2
57 / 58
Summary
Creating classes
Next lecture
Arrays (single and multi-dimensional)
Strings
Inheritance