Presentation is loading. Please wait.

Presentation is loading. Please wait.

Type Conversion, Constants, and the String Object

Similar presentations


Presentation on theme: "Type Conversion, Constants, and the String Object"— Presentation transcript:

1 Type Conversion, Constants, and the String Object
CS0007: Introduction to Computer Programming

2 Review Integer Data Types Floating-Point Data Types
byte short int long Floating-Point Data Types float double String Concatenation is… the process of appending to the end of a string. String Concatenation Operator +

3 Review Entities of the boolean data type can have one of two values:
true false Entities of the char data type can take one what kind of values: Single Characters Characters are represented in memory as… 2 bytes integers Character Literals are encapsulated in Single Quotes (') String Literals are encapsulated in Double Quotes (")

4 Review You can declare multiple variables on the same line by separating them with a… Comma (,) You can assign a variable a value in the same line as you declare it through a process called… Initialization Some Arithmetic Operators +, -, *, /, % You can do more complex mathematical operations like square root and exponents with the Math class.

5 Combined Assignment Operators
Often you want to do a mathematical operation to the value in a variable and store the result back into the same variable: Computing the sum: sum = sum + newNumber; Counting count = count + 1; Doubling a number: number = number * 2; Many others You can shorten these with combined assignment operators: sum += newNumber; count += 1; number *= 2; You can do this with any of the mathematical operators. There is also an operator to add one to (increment) a variable : ++ Example: sum++;

6 Sample Segment of Code number = 5; number += 2; System.out.println("number += 2 makes number = " + number); number -= 3; System.out.println("number -= 3 makes number = " + number); number *= 4; System.out.println("number *= 4 makes number = " + number); number /= 8; System.out.println("number /= 8 makes number = " + number); number++; System.out.println("number++ makes number = " + number); number--; System.out.println("number-- makes number = " + number); } }

7 Conversion Between Primitive Data Types
Java is known as a strongly typed language. In a Strongly Typed Language before a value is assigned to a variable, Java checks the types of the variable and the value being assigned to it to determine if they are compatible. For example: int x; double y = 2.5; x = y; This will cause an error short y; This will NOT cause an error …but, why?

8 Conversion Between Primitive Data Types
Types in Java have “ranks”. Ranks here means that if a type has a higher rank than another, it can hold more numbers, and thus, will not lose any precision. Ranks (Highest to Lowest): double float long int short byte

9 Conversion Between Primitive Data Types
In assignment statements where values of a lower-ranked data types are stored in variables of higher-ranked data types, Java automatically converts the lower-ranked value to the higher- ranked type. This is called a Widening Conversion. double x; int y = 10; x = y; A Narrowing Conversion is a conversion of a value to a lower-ranked type. These can cause a loss of data, so Java does not automatically perform them. Imagine converting from double to int… You can perform narrowing conversions with type casting operators.

10 Type Cast Operators Type Cast Operators allow you to manually convert from one type to another, even if it is a narrowing conversion. In Java they are unary operators that appear before what you want to convert. They are written as the type you want to convert to in parentheses. Example: x = (int)number; If number is of a numeric data type, it will convert the value in number to the int type and assign that value to x. If number is a floating-point type, the fractional part of the value would be lost converting it to int type and the value assigned to x. This is called truncation. The value in number would not be changed.

11 Type Casting double number1 = 2.0; int number2; byte number3; number2 = (int)number1; number3 = (byte)number2; System.out.println("Type Casting with NO Side Effects"); System.out.println(" "); System.out.print ("number1 = " + number1); System.out.print ("number2 = " + number2); System.out.println("number3 = " + number3 + "\n");

12 Type Casting with side effects
number1 = ; number2 = (int)number1; number3 = (byte)number2; System.out.println("Type Casting with Side Effects"); System.out.println(" "); System.out.println("number1 = " + number1); System.out.println("number2 = " + number2); System.out.println("number3 = " + number3);

13 Type Conversion in Arithmetic Operations
Recall integer division: int number1 = 10, number2 = 4; double number3 = number1 / number2; number3 will have 2.0 stored in it as a result of the division because both operands of the division are of type int. We can use type casting on one of the operands to make sure the result is a double: double number3 = (double)number1 / number2; number3 will have 2.5 stored in it as a result of the division because one of the operands is of type double. Note that type casting operators can be applied to expressions enclosed in parentheses: double number3 = (double)(number1 / number2); number3 will have 2.0 stored in it as a result of the division because the type casting operator is applied to the result of the integer division, which is 2.

14 Type Casting Example 2 int number1 = 7, number2 = 4; double number3; number3 = number1 / number2; System.out.println(number1 + " / " + number2 + " = " + number3); number3 = (double)number1 / number2; System.out.print((double)number1 + " / " + number2 + " = "); System.out.println(number3); number3 = (double)(number1 / number2);

15 Mixed Integer Operations
One of the problems in Java is that when you use any integer type in an arithmetic operation, it temporarily converts them to int. This can cause problems: short number1 = 10, number2 = 20, number3; number3 = number1 + number2; The second line will cause an error! Why? Because the result of the addition of number1 and number 2 is of the int type, which is over a higher rank than number3’s type, short. Cannot make the narrowing conversion. The way to fix this is to cast the entire expression to short: number3 = (short)(number1 + number2);

16 Other Mixed Mathematical Expressions
When Java sees that an expression has operands of double, float, or long, it attempts to convert all the operands of lower rank to that type. For Example: double number1 = 2.5, number3; int number2 = 4; number3 = number1 + number2; Before the addition number2 is converted to type double, and the result is a double.


Download ppt "Type Conversion, Constants, and the String Object"

Similar presentations


Ads by Google