p05 - arithmetic operators 2
TRANSCRIPT
-
7/30/2019 P05 - Arithmetic Operators 2
1/5
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 1 of 5
Arithmetic Operators Part 2 (05)
Basic calculations, assignments, unary, type conversion and type casting.
Unary Operators
Unary operators such as -- or ++ involve only one variable. For instance in order to increase n by 1 the
statement n++ is enough. When two variables are used in a calculation its called a binary operation. Unary
operators can be used in two ways which arepostfixandprefix.
Type Statement Equivalent
Postfix
n++ n = n +1
n-- n = n 1
Prefix
++n n = n + 1
--n n =n -1
Although there end result is the same, there is a difference when postfix and prefix are used in expressions.
Lets look at the following example:
class PostPre {
public static void main (String args[]){
int x;
int n;
//Example of postfix
n = 9;x = n++;
System.out.println("x is "+x+" and n is "+n);
//Example of prefix
n = 9;
x = ++n;
System.out.println("x is "+x+" and n is "+n);
}
}
-
7/30/2019 P05 - Arithmetic Operators 2
2/5
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 2 of 5
In the previous example of postfix, what happens is first the value of n (9) is stored in x, then n is increased
by 1 and the resulting value stored in n. On the other hand in the prefix case, first n is increased by one and
then the result is stored in x. So if we run the program we end up with the following output.
x is 9 and n is 10
x is 10 and n is 10
Using Reals
When numbers with a fraction must be stored, real type variables must be used; float or double. These can
be used just like regular variable types. However, when using float the letter fmust be added at the end of
the number as shown below (otherwise the program wont build).
class FloatExample {
public static void main (String args[]) {
float num = 3.45f;
}
}
Conversions
When variables are assigned to other variables for example num1 = num2, one must keep in mind the
following:
Both variables are of the same data type, or The two variables are compatible, or The destination variable type is larger than the source type.
Consider the following examples.
byte num2 =13;
short num1 = num2;
This is correct since num1 is short and so it s a bigger variable type than byte.
long num2;
int num1 = num2;
This is incorrect since were trying to fit long data into an int data type and it
cannot be done since its smaller.
-
7/30/2019 P05 - Arithmetic Operators 2
3/5
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 3 of 5
This hierarchy shows how variables can be converted. For instance double is the biggest, so it cannot be
converted to anything else; whilst byte (since its the smallest) can be converted to any type (except char).
Note that boolean data types cannot be converted to anything since they are not considered to be numbers
in Java.
Type Casting
Types can also be changed by specifying to which type you want to convert them in brackets. For example:
int x = (int) 9.45;
This will change 9.45 to an integer (so it becomes a 9) and stores it in x.
double x = 9.45;
int p = (int) x;
This has the same result as the previous statement.
boolean tall = true;
int x = (int) tall;
This will not work since tall is not a number and hence it cannot be converted.
double
float
long
int
char short
byte
-
7/30/2019 P05 - Arithmetic Operators 2
4/5
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 4 of 5
Scope
Whenever the curly brackets are opened and closed, a scope is created. If a variable is declared in the main
scope it can be used throughout the method, however if it is declared between the curly brackets further in
the program it will be only available in that scope.
class Scope {
public static void main (String args[]){
int x = 5;
{
int j = 2;
System.out.println(" x is "+x+" and j is "+j);
}
System.out.println ("x is "+x+" and j cannot be shown!");
}
}
As you can see in the previous example x can be used throughout the program however j can only be used
in that scope (the blue shade).
Constants
A constant is very similar to a variable, however the difference is that its contents cannot be changed; so its
read-only. A constant is declared the same as a variable however the keyword final must be used in order to
show that its a constant. So for instance the mathematical value for PI never changes, so it can be declared
as a constant as follows:
final double PI = 3.142;
In order to identify variables from constants, constants are created using capital letters.
-
7/30/2019 P05 - Arithmetic Operators 2
5/5
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 5 of 5
Activities
1. Create a program to illustrate the difference between postfix and prefix using --.2. Which letter must be added after a number assigned to a float variable? Give an example.3. Can the following types be converted to the specified types?
a. long doubleb. boolean floatc. float doubled. char int
4. Create a program to illustrate how scope works.5. Create a program to calculate the circumference of a circle with radius 9.34. You must use variables
and/or constants to store the values.
6. Create a program that calculates the following (always use variables and constants wherenecessary):
a. Perimeter of a Box with length 4 and breadth 10b. Area of a Sphere with radius 6.542c. Convert 300 Fahrenheit into Celsius
***