1 / 17

JAVA

JAVA. Practical 05 Unary operators Using Reals Conversions Type Casting Scope 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

nubia
Download Presentation

JAVA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JAVA Practical 05 Unary operators Using Reals Conversions Type Casting Scope Constants

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

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

  4. Two ways of Using Unary Operators • Unary operators can be used in two ways; • Postfix • Prefix

  5. Example • Although the end result is the same, there is a difference when using postfix or prefix

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

  7. Using Reals • When numbers with a fraction must be stored, real type variables must be used; • float • 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

  8. Example

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

  10. Examples

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

  12. Hierarchy

  13. Type Casting • Types can also be changed by specifying to which type you want to convert them in brackets. For example: intx = (int) 9.45;

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

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

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

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

More Related