0% found this document useful (0 votes)
63 views8 pages

2 Type Casting Strings Input Output EscapeSequence

The document discusses Java programming fundamentals including type conversion (casting), strings, input/output statements, and escape sequences. It covers implicit and explicit type conversion using casting operators, string operations like concatenation, and using assignment statements and input statements to put data into variables. The Scanner class is introduced for reading input from the standard input device. Sample code is provided to demonstrate these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views8 pages

2 Type Casting Strings Input Output EscapeSequence

The document discusses Java programming fundamentals including type conversion (casting), strings, input/output statements, and escape sequences. It covers implicit and explicit type conversion using casting operators, string operations like concatenation, and using assignment statements and input statements to put data into variables. The Scanner class is introduced for reading input from the standard input device. Sample code is provided to demonstrate these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

JAVA PROGRAMMING FUNDAMENTALS

TYPE CONVERSION (CASTING), STRING, INPUT/OUTPUT STATEMENT, ESCAPE SEQUENCE

TYPE CONVERSION (CASTING)

 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

The following Java program evaluates the preceding expressions:


Example 1
Sample Code
public class Type_casting {
public static void main(String[] args) {
System.out.println("(int) (7.9) = " + (int) (7.9));
System.out.println("(int) (3.3) = " + (int) (3.3));
System.out.println("(double) (25) = " + (double) (25));
System.out.println("(double) (5+3) = " + (double) (5+3));
System.out.println("(double) (15) / 2 = " + (double) (15) / 2);
System.out.println("(double) (15 / 2) = " + (double) (15 / 2));
System.out.println("(int) (7.8 + (double)(15) / 2) = " + (int) (7.8 +(double)(15)
/ 2));
System.out.println("(int) (7.8 + (double)(15 / 2)) = " + (int) (7.8 + (double)(15
/ 2)));

}
}

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

THE LENGTH OF THE STRING “Sunny Day” is 9


STRINGS AND THE OPERATOR +
 One of the most common operations performed on strings is the concatenation operation,
which allows strings to be appended at the end of another string. The operator + can be used
to concatenate (or join) two strings as well as a string and a numeric value or a character.

Example 1:
 Consider the following expression:

“Sunny” + “ Day”

This expression evaluates to:

“Sunny Day”

Example 2:
 Now, consider the following expression:

“Amount Due = $”

Therefore, the expression “Amount Due = $” + 576.35 evaluates to

“Amount Due = $576.35”

Example 3:
 Consider the following expression:
“The sum = ” + 12 + 26

This expression evaluates to:

“The sum = 1226”

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:

“The sum = ” + (12 + 26)

This expression evaluates to:

“The sum = 38”

Sample Code
public class string_class {

public static void main(String[] args) {


System.out.println("The sum = " + 12 + 26);
System.out.println("The sum = " + (12 + 26));
System.out.println(12 + 26 + " is the sum");
System.out.println("The sum of" + 12 + " and " + 26 + " = " + (12 + 26));
}
}

Output:
The sum = 1226
The sum = 38
38 is the sum
The sum of12 and 26 = 38

PUTTING DATA INTO VARIABLES


 Now that you know how to declare variables, the next question is: How do you put data into
those variables? The two common ways to place data into a variable are:
1) Use an assignment statement.
2) Use input (read) statements.

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);

sale = 0.02 * 1000;


System.out.println("sale = " + sale);

first = 'D';
System.out.println("first = " + first);

str = "It is a sunny day.";


System.out.println("str = " + str);
}
}

Output:
num1 = 4
num2 = 9
sale = 20.0
first = D
str = It is a sunny day.

2) Input (Read) Statement


 In this section, you will learn how to put data into variables from the standard input device
using Java’s input (or read) statements.
Reading Data Using The Scanner class
 To put data into variables from the standard input device, Java provides the class
Scanner. Using this class, we first create an input stream object and associate it with
the standard input device. The following statement accomplishes this:

Static Scanner console = new Scanner(System.in);

 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;

System.out.println("Enter two integers separated by spaces:");

feet = console.nextInt();
inches = console.nextInt();

System.out.println("feet = " + feet);


System.out.println("inches = " + inches);
}
}

Output:

Enter two integers separated by spaces:


23 7
feet = 23
inches = 7

In the preceding program, notice the first line:

import java.util.*;

or

import java.util.Scanner

THIS LINE IS REQUIRED TO USE THE CLASS SCANNER.

Sample Code 2

import java.util.Scanner;

public class input_read2 {


static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
String firstName;
String lastName;

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();

System.out.println("Name: " + firstName + " " + lastName);


System.out.println("Age: " + age);
System.out.println("Weight: " + weight);

}
}

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

ESCAPE SEQUENCE DESCRIPTION


\n New line Cursor moves to the beginning of the next line
\t Tab Cursor moves to the next tab stop
\b Backspace Cursor moves on space to the left
\r Return Cursor moves to the beginning of the current line (not the
next line)
\\ Backslash Backslash is printed
\’ Single quotation Single quotation mark is printed
\” Double quotation Double quotation mark is printed

You might also like