2 Type Casting Strings Input Output EscapeSequence
2 Type Casting Strings Input Output EscapeSequence
In the previous lesson, you learned that when evaluating an arithmetic expression if the
operator has mixed operands, the integer value is treated as a floating-point value with the
zero decimal part. When a value of one data type is automatically treated as another data type,
an implicit type coercion has occurred.
As examples in the preceding section illustrate, if you are not careful about data types, implicit
type coercion can generate unexpected results.
To avoid implicit type coercion, Java provides for explicit type conversion through the use of a
cast operator. The cast operator, also called type conversion or type casting, takes the
following form:
(dataTypeName) expression
First, the expression is evaluated. Its value is the treated as a value of the type specified by
dataTypeName.
When using the cast operator to treat a floating-point (decimal) number as an integer, you
simply drop the decimal part of the floating-point number. That is, the floating-point number is
truncated. The following examples show how cast operators work.
EXPRESSION EVALUATES TO
(int) (7.9) 7
(int) (3.3) 3
(double) (25) 25.0
(double) (5+3) = (double) (8) = 8.0
= 15.0 / 2 (because (double) (15) = 15.0)
(double) (15) / 2 = 15.0 / 2.0
= 7.5
= (double) (7) (because 15 / 2 = 7)
(double) (15 / 2)
= 7.0
= (int) (7.8 + 7.5)
(int) (7.8 + (double)(15) / 2) = (int) (15.3)
= 15
= (int) (7.8 + 7.0)
(int) (7.8 + (double)(15 / 2)) = (int) (14.8)
= 14
}
}
Output:
(int) (7.9) = 7
(int) (3.3) = 3
(double) (25) = 25.0
(double) (5+3) = 8.0
(double) (15) / 2 = 7.5
(double) (15 / 2) = 7.0
(int) (7.8 + (double)(15) / 2) = 15
(int) (7.8 + (double)(15 / 2)) = 14
class String
In the preceding lessons, we discussed primitives types to deal with data consisting of
numbers and characters. What about data values such as a person’s name? A person’s
name contains more than one character. Such values are called strings.
A string is a sequence of zero or more characters. Strings in Java is enclosed in double
quotation marks (not in single quotation marks are the char data types).
To process strings effectively, Java provides the class String. The class String contains various
operations to manipulate a string.
Technically, the class String is not a primitive type.
A string that contains no characters is called a null or empty string. The following are examples
of strings. Note that “ ” is the empty string.
“William Jacob”
“Mickey”
“”
Every character in a string has a specific position in the string. The position of the first
character is 0, the position of the second character is 1, and so on. The length of a string is the
number of characters in it.
Example:
String “Sunny Day”
Character in
‘S’ ‘u’ ‘n’ ‘n’ ‘y’ ‘ ’ ‘D’ ‘a’ ‘y’
the string
Position of the
character in 0 1 2 3 4 5 6 7 8
the string
Example 1:
Consider the following expression:
“Sunny” + “ Day”
“Sunny Day”
Example 2:
Now, consider the following expression:
“Amount Due = $”
Example 3:
Consider the following expression:
“The sum = ” + 12 + 26
This is not what you might have expected. Rather than adding 12 and 26, the values 12 and 26
are concatenated. This is because the associativity of the operator + is from left to right.
Example 4:
Consider the following expression:
Sample Code
public class string_class {
Output:
The sum = 1226
The sum = 38
38 is the sum
The sum of12 and 26 = 38
1) Assignment Statement
The assignment statement takes the following form:
Variable = expression;
In an assignment statement, the value of the expression should match the data type of the
variable. The expression on the right side is evaluated, and its value is assigned to the variable
on the left side.
A variable is said to be initialized the first time a value is placed in the variable.
In Java, = (the equal sign) is called the assignment operator.
Consider the following assignment statements:
num1 = 4;
num2 = 4 * 5 – 11;
sale = 0.02 * 1000;
first = ‘D’ ;
str = “It is a sunny day.”;
Sample Code
public class assignment_statement {
public static void main(String[] args) {
int num1;
int num2;
double sale;
char first;
String str;
num1 = 4;
System.out.println("num1 = " + num1);
num2 = 4 * 5 - 11;
System.out.println("num2 = " + num2);
first = 'D';
System.out.println("first = " + first);
Output:
num1 = 4
num2 = 9
sale = 20.0
first = D
str = It is a sunny day.
This statement creates the input stream object console and associates it with the
standard input device. The object console reads the next input as follows:
a) If the next input token can be interpreted as an integer, then the expression:
console.nextInt( )
b) If the next input token can be interpreted as a floating-point number, then the
expression:
console.nextDouble( )
c) The expression:
console.next( )
retrieves the next input token as a string; that is, the value of this expression is the
next input string.
d) The expression:
console.nextLine( )
retrieves the next input as a string until the end of the line, the value of this
expression is the next input line.
Sample code 1
import java.util.*;
public class input_read {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
int feet;
int inches;
feet = console.nextInt();
inches = console.nextInt();
Output:
import java.util.*;
or
import java.util.Scanner
Sample Code 2
import java.util.Scanner;
int age;
double weight;
System.out.println("Enter first name, last name, " + "age, and weight separated "
+ "by spaces:");
firstName = console.next();
lastName = console.next();
age = console.nextInt();
weight = console.nextDouble();
}
}
Output:
Enter first name, last name, age, and weight separated by spaces:
Sheila Mann 23 120.5
Name: Sheila Mann
Age: 23
Weight: 120.5
OUTPUT STATEMENT
In Java, output on the standard output device is accomplished by using the standard output
object System.out. The object System.out has access to two methods, print and println, to
output a string on the standard output device.
The syntax to use the object System.out and the methods print and println is:
System.out.println(expression);
System.out.print(expression);
These are output statements. The expression is evaluated, and its value is printed at the
current insertion point on the output device. After outputting the value of the expression, the
method print leaves the insertion point after the last character of the value of the expression,
while the method println position the insertion point at the beginning of the next line.
ESCAPE SEQUENCE