Programming Lecture 3 - Elementary Java PDF
Document Details
Uploaded by AdequateAlder755
University of West London
Dr. Ikram Ur Rehman
Tags
Summary
This is a programming lecture on Elementary Java. It covers topics like type casting, constants, concatenation, and relational operators. The lecture notes were presented by Dr. Ikram Ur Rehman at the University of West London.
Full Transcript
Programming Lecture 3: Elementary Java School of Computing and Engineering University of West London, UK Dr. Ikram Ur Rehman Overview Type Casting Constants Concatenation Relational Operators Type Casting Variables of one data type is assigned values to variables of a...
Programming Lecture 3: Elementary Java School of Computing and Engineering University of West London, UK Dr. Ikram Ur Rehman Overview Type Casting Constants Concatenation Relational Operators Type Casting Variables of one data type is assigned values to variables of another data type. Type casting is not possible for Boolean types. Two types of casting: 1. Implicit (Widening): small to large e.g. int → long → float → double 1. Explicit (Narrowing): large to small e.g. double → float → long → int Implicit casting is automatically carried out by the compiler. Explicit Casting Converting higher range variable into lower range variable → problem (information loss). Write data type of smaller range into round brackets For example: long l = 10000; int i = (int)l; This is explicit casting Explicit Casting (Information Loss) Truncation float → integer For example: float f = 3.14; int i = (int)f; ‘i’ will contain only 3 Out of Range long → integer Explicit casting does not mean, values can be assigned out of range, otherwise there will be information loss. For example: long l = 123456789; int i = (int)l; Constants Constants are values that do not change after declaration. Keyword final is used to declare a constant For example: final double pi = 3.14159; Now, this constant name can be used instead of its value, such as: double area = radius * radius * pi; Augmented Assignment Java provides a number of assignment operators to make it easier to write code. For example: Addition and assignment (+=): int num1 = 2; int num2 = 3; num2 += num1; // num2 = num2 + num1; // num2 is 5 and num1 is 2 Similarly, Java supports subtraction (-=),multiplication (*=), division (/=), and remainder (%=). Increment/Decrement Operators An increment or decrement operator provides a more convenient and compact way to increase or decrease the value of a variable by one. For example, the statement x=x+1; can be simplified to ++x; For example: int test = 4; ++test; // test is now 5 The decrement operator (--) is used to decrease the value of a variable by one. For example: int test = 5; --test; // test is now 4 Remember: Using increment and decrement operators makes expressions short, but it also makes them complex and difficult to read. Prefix - Increment/Decrement Increment/decrement can be done through prefix and postfix. With prefix form, the operator appears before the operand. For example: int x = 10 int y = ++x // y is 11 Note that the value of x is first incremented to 11, and is then assigned to y, so the values of both x and y are now 11. Postfix - Increment/Decrement With postfix, the operator appears after the operand. For example: int x = 10 int y = x++ // y is 10 Note that in postfix x is first assigned to y, and is then incremented by one. Therefore, x becomes 11, while y is assigned the value of 10. The same concept applies to the decrement operator. Concatenation Similar to numbers in Java, strings can also be added. This process is concatenation. For example: String firstName, lastName; firstName = “Joe"; lastName = “Mark"; System.out.println("My name is " + firstName +" "+lastName); // Prints: My name is Joe Mark Relational Operators In a program, you often need to compare two values, such as whether (2 is greater than 3) or (a is less than or equals to b). Java provides six comparison operators (also known as relational operators) that can be used to compare two values. The result of the comparison is a Boolean value: true or false. For example: boolean b = (1 > 2); Types of Relational Operators Java Mathematics Name Example Result Operator Symbol (radius is 5) < < less than radius < 0 false greater than radius > 0 true >= ≥ greater than or equal to radius >= 0 true == = equal to radius == 0 false != ≠ not equal to radius != 0 true