java practical 05 1. unary operators 2. using reals 3. conversions 4. type casting 5. scope 6....

17
JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

Upload: annabelle-randall

Post on 19-Jan-2018

219 views

Category:

Documents


0 download

DESCRIPTION

Unary Operators Reminder These only need one variable (increment by 1) (decrement by 1) 3. variable += x (same as variable = variable + x) 4. variable - = x (same as variable = variable - x) 5. variable *= x (same as variable = variable * x) 6. variable /= x (same as variable = variable / x) 7. variable %= x (same as variable = variable % x)

TRANSCRIPT

Page 1: JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

JAVAPractical 05 1. Unary operators2. Using Reals3. Conversions4. Type Casting 5. Scope6. Constants

Page 2: JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

Unary Operators Unary operators such as -- or ++

involve only one variable

For example in order to increase x by 1 the statement x++ is enough

When two variables are used in a calculation it’s called a binary operation

Page 3: JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

Unary Operators Reminder These only need one variable

1. ++ (increment by 1)2. -- (decrement by 1)3. variable += x (same as variable = variable + x) 4. variable -= x (same as variable = variable - x)5. variable *= x (same as variable = variable * x)6. variable /= x (same as variable = variable / x)7. variable %= x (same as variable = variable % x)

Page 4: JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

Two ways of Using Unary Operators

Unary operators can be used in two ways;

1. Postfix2. Prefix

Type Statement Equivalent

Postfixn++ n = n +1n-- n = n – 1

Prefix++n n = n + 1--n n =n -1

Page 5: JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

Example Although the end result is the

same, there is a difference when using postfix or prefix

Page 6: JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

Output The outputs are the following;

In n++◦ The value of n (9) is stored in x◦ Then n is increased by 1 ◦ Resulting value stored in n

In ++n◦ First n (9) is increased by one ◦ Then the result is stored in x◦ So we end up with the output above

Page 7: JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

Using RealsWhen numbers with a fraction must

be stored, real type variables must be used;

1. float 2. Double

These can be used just like regular variables

However, when using float the letter f must be added at the end of the number

Page 8: JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

Example

Page 9: JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

Conversions When variables are assigned to

other variables for example num1 = num2, one must keep in mind the following:

1. Both variables are of the same data type, or

2. The two variables are compatible,

3. or4. The destination variable type is

larger than the source type.

Page 10: JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

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 we’re trying to fit long data into an int data type and it cannot be done since it’s smaller.

Page 11: JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

Converting Variable Types We must look at the hierarchy of

variable types in order to know which variables can be converted

Example double is the biggest, so it cannot be converted to anything else; whilst byte (since it’s the smallest) can be converted to any type (except char). Note that boolean data types cannot be converted to anything since they are not numbers

Page 12: JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

Hierarchy doublefloatlongint

char shortbyte

Page 13: JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

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.

Page 14: JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

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

Page 15: JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

Example

As you can see in the previous example x can be used throughout the program however j can only be used in that scope

Page 16: JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

Constants A constant is very similar to a variable

The difference is that its values cannot be changed

Hence a constant is read-only

A constant is declared the same as a variable however the keyword final must be used in order to show that it’s a constant

Page 17: JAVA Practical 05 1. Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants

Example For example the mathematical value for PI

never changes,

Hence it can be declared as a constant in our program

In order to identify variables from constants, constants are created using capital letters. 

final double PI = 3.142;