Chapter 2.5.7 – Type Conversion of Strings | Introduction to Programming Using Java

Chapter 2.5.7 – Type Conversion of Strings | Introduction to Programming Using Java

 

2.5.7 Type Conversion of Strings

 

In addition to automatic type conversions and explicit type casts, there are some other cases where you might want to convert a value of one type into a value of a different type. One common example is the conversion of a String value into some other type, such as converting the string “10” into the int value 10 or the string “17.42e-2” into the double value 0.1742. In Java, these conversions are handled by built-in functions.

 

type conversion of strings

 

There is a standard class named Integer that contains several subroutines and variables related to the int data type. (Recall that since int is not a class, int itself can’t contain any subroutines or variables.) In particular, if str is any expression of type String, then Integer.parseInt(str) is a function call that attempts to convert the value of str into a value of type int.

For example, the value of Integer.parseInt(“10”) is the int value 10. If the parameter to Integer.parseInt does not represent a legal int value, then an error occurs.

Similarly, the standard class named Double includes a function Double.parseDouble that tries to convert a parameter of type String into a value of type double. For example, the value of the function call Double.parseDouble(“3.14”) is the double value 3.14. (Of course, in practice, the parameter used in Double.parseDouble or Integer.parseInt would be a variable or expression rather than a constant string.)

 

 

Chapter 2.5.7 - Type Conversion of Strings | Introduction to Programming Using Java

 

 

Type conversion functions also exist for converting strings into enumerated type values. (Enumerated types, or enums, were introduced in Subsection 2.3.3.) For any enum type, a predefined function named valueOf is automatically defined for that type. This is a function that takes a string as parameter and tries to convert it to a value belonging to the enum.

The valueOf function is part of the enum type, so the name of the enum is part of the full name of the function. For example, if an enum Suit is defined as enum Suit { SPADE, DIAMOND, CLUB, HEART }

then the name of the type conversion function would be Suit.valueOf. The value of the function call Suit.valueOf(“CLUB”) would be the enumerated type value Suit.CLUB. For the conversion to succeed, the string must exactly match the simple name of one of the enumerated type constants (without the “Suit.” in front).

 

Chapter 2.5.7 - Type Conversion of Strings | Introduction to Programming Using Java

 

2.5.8 Precedence Rules

 

If you use several operators in one expression, and if you don’t use parentheses to explicitly indicate the order of evaluation, then you have to worry about the precedence rules that determine the order of evaluation. (Advice: don’t confuse yourself or the reader of your program; use parentheses liberally.)

Here is a listing of the operators discussed in this section, listed in order from highest precedence (evaluated first) to lowest precedence (evaluated last):

 

Chapter 2.5.7 - Type Conversion of Strings | Introduction to Programming Using Java

 

Operators on the same line have the same precedence. When operators of the same precedence are strung together in the absence of parentheses, unary operators and assignment operators are evaluated right-to-left, while the remaining operators are evaluated left-to-right. For example, A*B/C means (A*B)/C, while A=B=C means A=(B=C). (Can you see how the expression A=B=C might be useful, given that the value of B=C as an expression is the same as the value that is assigned to B?)

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 2 – Names and Things | Introduction to Programming Using Java

Chapter 2.2.1 – Variables | Introduction to Programming Using Java

Chapter 2.3.3 – Introduction to Enums | Introduction to Programming Using Java

Chapter 2.4.4 – Formatted Output | Introduction to Programming Using Java

Chapter 2.5 – Details of Expressions | Introduction to Programming Using Java

Chapter 2.5.1 – Arithmetic Operators | Introduction to Programming Using Java

Chapter 2.5.3 – Relational Operators | Introduction to Programming Using Java

1 thought on “Chapter 2.5.7 – Type Conversion of Strings | Introduction to Programming Using Java”

Leave a Comment