Computer Science and Elements of Programming

Java Lecture 2
Dr Giovanna Di Marzo Serugendo
Department of Computer Science and Information Systems Birkbeck College, University of London Email: dimarzo@dcs.bbk.ac.uk Web Page: http://www.dcs.bbk.ac.uk/~dimarzo

Java Lecture 1: Review
• Program editing / compiling / executing • Program editing / compiling / executing in BlueJ • String Variables
– Declaration – Assignment

• Inputs/Outputs
– Inputs – Outputs to a terminal window

• Inputs/Outputs in BlueJ
Java Lectures – Giovanna Di Marzo Serugendo 2

1

Java Lecture 2: Overview
• Java Primitive types (numerical values)
– – – – – – Type byte Type short Type int Type long Type float Type double

• Values Range and Operators • Variables
– Declarations, Assignments, I/O

• Working with operators • Types conversion
Java Lectures – Giovanna Di Marzo Serugendo 3

Primitive Types vs Objects
• In Java everything is an object ….
… except variables whose type is a

Primitive Type
Values for those variables are: – Arithmetic / Numerical values: 3.14, 55, -27, 0.0004, … – Boolean values: true, false / 1, 0 – Characters: ‘a’, ‘b’,‘_’, ‘2’, ‘A’, …
Java Lectures – Giovanna Di Marzo Serugendo 4

2

Primitive Types: Numerical Values
• Types for numerical values
– Integers (-3, 0, 1, 5500, -29567, …)
• • • • Type byte Type short Type int Type long

– Real numbers (0.0, -23.45, 4.67093)
• Type float • Type double
Java Lectures – Giovanna Di Marzo Serugendo 5

Integers
• Different sizes
– different sizes (# bits) allocated in memory for storing the value
• different range of possible values Type
byte short int long

# bits
8 bits 16 bits 32 bits 64 bits

Range
-128 to 127 -32768 to 32767 -2147483648 to 2147483647 -9223372036854775808 to 9223372036854775807

Java Lectures – Giovanna Di Marzo Serugendo

6

3

Real Numbers
• Different sizes
– different sizes (# bits)
• Floating point numbers (Float and Double) • Double
– higher precision (more decimals) – smaller and bigger values (smaller and bigger power values)

Type
float double

# bits
32 bits 64 bits

Range (absolute value)
1.40239846 *10 -45 to 3.40282347 *10 +38 4.94065645841246544 *10 -324 to 1.79769313486332570*10+308

Java Lectures – Giovanna Di Marzo Serugendo

7

Arithmetic operators
• Basic numerical operators
+ * / % Addition Subtraction Multiplication Division Remainder

Java Lectures – Giovanna Di Marzo Serugendo

8

4

Arithmetic Operators
• Operators precedence
– First priority
* / + %

– Second priority – Operators with higher priority are applied first – Operators with same priority are applied from left to right
Java Lectures – Giovanna Di Marzo Serugendo 9

Arithmetic Operators
• Operators precedence and evaluation
Expression 2+3*4 1+2–3 2+3*4 / 6 2+4/5 13 % 5 11.5 % 2.5 13 / 5 13.0 / 5.0 Evaluated as 2 + (3 * 4) (1 + 2) - 3 2 + ((3 * 4) / 6) 2 + (4 / 5) (13 % 5) (11.5 % 2.5) (13 / 5) (13.0 / 5.0) Result 14 0 4 2 3 1.5 2 2.6
10

Java Lectures – Giovanna Di Marzo Serugendo

5

Arithmetic operators
• Basic numeric operators: comparisons
== < > != <= >= Equality Less than Greater than Inequality Less than or equal to Greater than or equal to

Java Lectures – Giovanna Di Marzo Serugendo

11

Types Conversions
• Implicit conversion
– two operands of different numeric type towards broadest numeric type
byte short int long float double

• Occurs during:
– Assignments – Arithmetic expression evaluation – String concatenation
Java Lectures – Giovanna Di Marzo Serugendo 12

6

Types conversion
• Explicit conversion: Casting
(<type>) <expression> • Example double f; int i = 3; f=(double) i;

Java Lectures – Giovanna Di Marzo Serugendo

13

Variables Declaration
• Arithmetic Variable Declaration
Variable’s name

byte int short long float double

b; i; j; k; f; d;

Variable’s Type

If no values assigned: 0 by default
Java Lectures – Giovanna Di Marzo Serugendo 14

7

Variables Declaration
• Authorised names (as for String):
– Sequences of one or more characters. – Must begin with a letter or ‘_’ – Must consist entirely of letters, digits, the underscore character '_', and the dollar sign character '$' i i1 i2 anInteger a_float

Java Lectures – Giovanna Di Marzo Serugendo

15

Variables Declaration
• Where to place variables declarations
– Fields – Constructor – Methods – Warning:
• variables declared inside Constructors and Methods are visible (usable) only inside the corresponding Constructor or Method

Java Lectures – Giovanna Di Marzo Serugendo

16

8

Variables Declaration
public class examplesWithNumericalValues { // Fields (variables) int i;

In the Fields zone

/** * Constructor for objects of class examplesWithNumericValues */ public examplesWithNumericalValues() { byte b; In a constructor } /** *Prints the numeric value on a Terminal Window */ public static void printNumericalValue() { In a double pi = 3.14; System.out.println(pi); } } Java Lectures – Giovanna Di Marzo Serugendo 17

method

Variables Assignments
• Numerical variables assignment
<varname> = <arithmetic_expression>;
Assignment operator

• arithmetic_expression is one of:
– Arithmetic value – Variable of numeric primitive type – Combination of values and/or variables with numeric operators

Expression evaluates to same type as the variable type
Warning: implicit conversions may change types and results
Java Lectures – Giovanna Di Marzo Serugendo 18

9

Variables Assignment
Assignment double f = 13.0 / 5.0; double f = 13 / 5; int i = 13 / 5; long l = 200; Evaluated as 2.6 2.0 // implicit conversion (int -> double) 2 200 // implicit conversion (int -> long)

Java Lectures – Giovanna Di Marzo Serugendo

19

Numerical values are not Strings
• What is the difference between:
int i = 35; String s = ‘‘35’’;

• Assuming the above declarations, are these assignments legal?
i = ‘‘35’’; i = s; s = i;
String int

Java Lectures – Giovanna Di Marzo Serugendo

20

10

Output in BlueJ
public static void printNumericalValues1() { double pi = 3.14; System.out.println(3.14); System.out.println(pi); // sums up 5 and 7 then prints 12 as string System.out.println(5 + 7); /* converts 5 into double, sums up 5.0 and 3.14 then prints 8.14 as string */ System.out.println (5 + pi);

}
Java Lectures – Giovanna Di Marzo Serugendo 21

Output in BlueJ

Java Lectures – Giovanna Di Marzo Serugendo

22

11

Output in BlueJ
public static void printNumericalValues2() { double pi = 3.14; /* prints numerical value as string and concatenate with string */ System.out.println("the value of pi is: " + pi); /* prints numerical value as string and concatenate with string */ System.out.println("the result is " + 5 + 7);

+

operator as String concatenator

/* sums up numerical values, then prints numeric value as string and concatenate with string */ System.out.println ("the result is " + (5 + 7)); }

Java Lectures – Giovanna Di Marzo Serugendo

+ arithmetic operator 23

Output in BlueJ

Java Lectures – Giovanna Di Marzo Serugendo

24

12

Input in BlueJ
• Input of values
– Direct input without ‘‘ ’’
• Otherwise it is considered a String

public static void printDate(int month, int day, int year) { System.out.println( "Today is " + day + "/" + month + "/" + year); }

Java Lectures – Giovanna Di Marzo Serugendo

25

Input in BlueJ

Java Lectures – Giovanna Di Marzo Serugendo

26

13

Main – Regular methods

Java Lectures – Giovanna Di Marzo Serugendo

27

Main – Regular Method
• Consider the following class:

Java Lectures – Giovanna Di Marzo Serugendo

28

14

Main – Regular Methods
• How can we change
examplesWithNumericalValues3()

so that the main looks like:

Java Lectures – Giovanna Di Marzo Serugendo

29

Main – Regular Methods

Java Lectures – Giovanna Di Marzo Serugendo

30

15

Java Wrapper Classes
• All primitive types have corresponding Java classes
– With classes
• Numerical values are stored through objects
– Method for manipulating primitive data values – Fields storing the primitive data values

– With primitive types
• Numerical values are « traditional » numbers
3.14

3.14
31

public int intValue()
Java Lectures – Giovanna Di Marzo Serugendo

Java Wrapper Classes
• Wrapper classes serve to represent arithmetic primitive data value as objects
– Because you want to define methods on it – You want to pass your numerical value in parameter as an object – You need to define an object which is able to store information about the numerical value

Java Lectures – Giovanna Di Marzo Serugendo

32

16

Java Wrapper Classes
Primitive Type
byte short int long float double

Wrapper Class
Byte Short Integer Long Float Double

Java Lectures – Giovanna Di Marzo Serugendo

33

Java Wrapper Classes
public static void wrapperClasses() Object { int i = 45; Integer i_obj = new Integer(i); String s = i_obj.toString(); System.out.println(s); }
int

Activation of method on object

Java Lectures – Giovanna Di Marzo Serugendo

34

17

Class Math
• class Math
– contains methods for performing basic numeric operations:
• • • • Exponential, logarithm square root trigonometric functions random numbers between 0.0 and 1.0

Java Lectures – Giovanna Di Marzo Serugendo

35

Class Math
public static void MathExamples() { System.out.println(Math.sqrt(9)); System.out.println(Math.random()); System.out.println(Math.random()); }

Java Lectures – Giovanna Di Marzo Serugendo

36

18

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.