CS2514

Introduction to Java

Dr Derek Bridge

School of Computer Science & Information Technology

University College Cork

Variables

JavaScript variables should be explicitly declared.

PythonJavaScript
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.

PythonJavaScript
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.

PythonJava
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.

PythonJava
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 typeValues
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
booleantrue or false

Java's other primitive data types (for completeness)

Arithmetic operators

Operator
+Addition (also String concatenation)
-Subtraction
*Multiplication
/Division
%Modulus (i.e. remainder)
~, <<, >>, >>>, &, |Bit-wise operators

Notes

Class exercise

Evaluate each of these as Java would:

  1. 7 // 2
  2. 7 / 2
  3. 7.0 / 2.0
  4. 7.0 / 2
  5. 7 / 2.0
  6. 7 % 2
  7. 2 + 3 * 4
  8. (2 + 3) * 4
  9. 5 - 2 + 3
  10. 5 - (2 + 3)

Strongly-typed and statically-typed

Strongly-typed and statically-typed

Strongly-typedStatically-typede.g. Java
Dynamically-typede.g. Python
Weakly-typede.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

Class exercises

Each of the next programs has errors.

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);
    }
    
}

Type conversions