CS2514
Introduction to Java
Dr Derek Bridge
School of Computer Science & Information Technology
University College Cork
Variables
JavaScript variables should be explicitly declared.
Python | JavaScript |
hourly_pay = 9.5
hours_worked = 35
total_pay = hourly_pay * hours_worked
print(total_pay)
# Hurray! A pay rise:
hourly_pay = 10.5
total_pay = hourly_pay * hours_worked
print(total_pay)
|
var hourly_pay;
var hours_worked;
var total_pay;
hourly_pay = 9.5;
hours_worked = 35;
total_pay = hourly_pay * hours_worked;
console.log(total_pay);
// Hurray! A pay rise:
hourly_pay = 10.5;
total_pay = hourly_pay * hours_worked;
console.log(total_pay);
|
Variables, again
But JavaScript does allow you to combine variable declaration with initialization.
Python | JavaScript |
hourly_pay = 9.5
hours_worked = 35
total_pay = hourly_pay * hours_worked
print(total_pay)
# Hurray! A pay rise:
hourly_pay = 10.5
total_pay = hourly_pay * hours_worked
print(total_pay)
|
var hourly_pay = 9.5;
var hours_worked = 35;
var total_pay = hourly_pay * hours_worked;
console.log(total_pay);
// Hurray! A pay rise:
hourly_pay = 10.5;
total_pay = hourly_pay * hours_worked;
console.log(total_pay);
|
Variables
Java variables should be explicitly declared along with their type.
Python | Java |
hourly_pay = 9.5
hours_worked = 35
total_pay = hourly_pay * hours_worked
print(total_pay)
# Hurray! A pay rise:
hourly_pay = 10.5
total_pay = hourly_pay * hours_worked
print(total_pay)
|
double hourly_pay;
int hours_worked;
double total_pay;
hourly_pay = 9.5;
hours_worked = 35;
total_pay = hourly_pay * hours_worked;
System.out.println(total_pay);
// Hurray! A pay rise:
hourly_pay = 10.5;
total_pay = hourly_pay * hours_worked;
System.out.println(total_pay);
|
Variables, again
But Java does allow you to combine variable declaration with initialization.
Python | Java |
hourly_pay = 9.5
hours_worked = 35
total_pay = hourly_pay * hours_worked
print(total_pay)
# Hurray! A pay rise:
hourly_pay = 10.5
total_pay = hourly_pay * hours_worked
print(total_pay)
|
double hourly_pay = 9.5;
int hours_worked = 35;
double total_pay = hourly_pay * hours_worked;
System.out.println(total_pay);
// Hurray! A pay rise:
hourly_pay = 10.5;
total_pay = hourly_pay * hours_worked;
System.out.println(total_pay);
|
Java's main primitive data types
Data type | Values |
int |
32-bit signed 2's-complement integer
- from -231 to 231 -1
- i.e. from -2,147,483,648 to 2,147,483,647
|
double |
64-bit floating-point number
- negative numbers from -4.94065645841246544 × 10-324 to -1.79769313486231570 × 10308
- positive numbers from 4.94065645841246544 × 10-324 to 1.79769313486231570 × 10308
|
boolean | true or false |
Java's other primitive data types (for completeness)
- Java's other integer types:
Data type | Values |
long | 64-bit signed 2's complement integer |
short | 16-bit signed 2's complement integer |
byte | 8-bit signed 2's complement integer |
char | 16-bit Unicode character (unsigned integer) |
- Java's other floating-point type:
Data type | Values |
float | 32-bit floating-point number |
Arithmetic operators
Operator | |
+ | Addition (also String concatenation) |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (i.e. remainder) |
~, <<, >>, >>>, &, | | Bit-wise operators |
Notes
-
In Python3, / is always true division and // is floor division.
In Java, / is always true division unless both operands are integers – in which case it rounds towards zero.
-
Precedence: *, /, % have higher precedence than +, -
- Associativity: all the operators above are left-associative
i.e. in the event of a tie in precedence, evaluate from left-to-right
Class exercise
Evaluate each of these as Java would:
- 7 // 2
- 7 / 2
- 7.0 / 2.0
- 7.0 / 2
- 7 / 2.0
- 7 % 2
- 2 + 3 * 4
- (2 + 3) * 4
- 5 - 2 + 3
- 5 - (2 + 3)
Strongly-typed and statically-typed
- Java is a strongly-typed language:
- Every variable has a type
- Java checks for type violations:
- e.g. assigning data of the wrong type into a variable
- e.g. applying operators to data of the wrong type
- Java is a (mostly!) statically-typed language
- The type of every variable and (nearly) every expression is known
from the program text at compile-time
- Hence Java checks for type violations at compile-time
Strongly-typed and statically-typed
Strongly-typed | Statically-typed | e.g. Java |
Dynamically-typed | e.g. Python |
Weakly-typed | e.g. JavaScript, PHP |
Is Python really strongly-typed?
Python (no error) | Java (compile-time error) |
x = 3
...
x = "Hello"
|
int x = 3;
...
x = "Hello";
|
Errors in programs
- Compile-time errors
- error message from the compiler
- e.g. syntax errors (missing semi-colons, missing closing brackets,...)
- e.g. type violations in Java
- Run-time errors
- error message when running the program
- e.g. division by integer 0, array index out of bounds,...
- Logic errors
- no error message...
- but the output is not what it should be
Class exercises
Each of the next programs has errors.
- Spot the error(s)
- For each error, what kind of error is it (compile-time, run-time, logic)?
- For each error, give a fix
Exercise A
class ExerciseA {
pubic static void main(String[] args) {
system.out.println("Hello world!);
}
Solution to Exercise A
class ExerciseA {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
Exercise B
class ExerciseB {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in)
System.out.println("What is your age in years?")
String age = sc.nextInt()
System.out.println("You are currently " + Age + " years old.")
int age = age + 1
System.out.println("On your next birthday, you will be " + age + " years old.")
}
}
Solution to Exercise B
class ExerciseB {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.println("What is your age in years?");
int age = sc.nextInt();
System.out.println("You are currently " + age + " years old.");
age = age + 1;
System.out.println("On your next birthday, you will be " + age + " years old.");
}
}
Exercise C
class ExerciseC {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.println("Please input three integers");
x = sc.nextInt();
y = sc.nextInt();
z = sc.nextInt();
average = x + y + z / 3;
System.out.println("Their average is " + average);
}
}
Solution to Exercise C
class ExerciseC {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.println("Please input three integers");
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
double average = (x + y + z) / 3.0;
System.out.println("Their average is " + average);
}
}