0% found this document useful (0 votes)
20 views40 pages

Data Types and Operators

Uploaded by

alromaysaamo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views40 pages

Data Types and Operators

Uploaded by

alromaysaamo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Data Types

and
Operators

PAGE 1
Identifiers
 An identifier is a sequence of characters that consist of letters, digits,
underscores (_), and dollar signs ($).
 An identifier must start with a letter, an underscore (_), or a dollar
sign ($). It cannot start with a digit.
 An identifier cannot be a reserved word. (See Appendix A, “Java
Keywords,” for a list of reserved words).
 An identifier cannot be true, false, or null.
 An identifier can be of any length.

2
Declaring Variables

int x; // Declare x to be an
// integer variable;
double radius; // Declare radius to
// be a double variable;
char a; // Declare a to be a
// character variable;

3
Assignment Statements

x = 1; // Assign 1 to x;
radius = 1.0; // Assign 1.0 to radius;
a = 'A'; // Assign 'A' to a;

4
Declaring and Initializing in
One Step

 int x = 1;
 double d = 1.4;

5
Named Constants

final datatype CONSTANTNAME = VALUE;

final double PI = 3.14159;


final int SIZE = 3;

6
Naming Conventions

 Choose meaningful and descriptive names.


 Variables and method names:
 Use lowercase.
 If the name consists of several words, concatenate all in
one, use lowercase for the first word, and capitalize the
first letter of each subsequent word in the name.
 For example, the variables radius and area, and
the method computeArea.
7
Naming Conventions,
cont.

 Class names:
 Capitalize the first letter of each word in the name. For
example, the class name ComputeArea.

 Constants:
 Capitalize all letters in constants, and use underscores to
connect words.
 For example, the constant PI and MAX_VALUE

8
Primitive Data Types
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127

short 2 bytes Stores whole numbers from -32,768 to 32,767

int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647

long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits

double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits

boolean 1 bit Stores true or false values

char 2 bytes Stores a single character/letter or ASCII values

9
Numeric Operators

Name Meaning Example Result

+ Addition 34 + 1 35

- Subtraction 34.0 – 0.1 33.9

* Multiplication 300 * 30 9000

/ Division 1.0 / 2.0 0.5

% Remainder 20 % 3 2

10
Integer Division

+, -, *, /, and %

5 / 2 yields an integer 2.
5.0 / 2 yields a double value 2.5

5 % 2 yields 1 (the remainder of the division)

11
Remainder Operator
Remainder is very useful in programming. For example, an even
number % 2 is always 0 and an odd number % 2 is always 1. So you
can use this property to determine whether a number is even or odd.
Suppose today is Saturday and you and your friends are going
to meet in 10 days. What day is in 10 days? You can find that
day is Tuesday using the following expression:
Saturday is the 6th day in a week
A week has 7 days
(6 + 10) % 7 is 2
The 2nd day in a week is Tuesday
After 10 days
12
Introducing Programming with an
Example
Computing the Area of a Circle
 This program computes the area of the circle.

13
Trace a Program
Execution allocate memory
public class ComputeArea { for radius
/** Main method */
public static void main(String[] args) { radius no value
double radius;
double area;

// Assign a radius
radius = 20;

// Compute area
area = radius * radius * 3.14159;

// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
} 14
Trace a Program
Execution
public class ComputeArea {
memory
/** Main method */
public static void main(String[] args) { radius no value
double radius;
area no value
double area;

// Assign a radius allocate memory


radius = 20; for area

// Compute area
area = radius * radius * 3.14159;

// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
} 15
Trace a Program
Execution
public class ComputeArea { assign 20 to radius
/** Main method */
public static void main(String[] args) {
radius 20
double radius;
double area; area no value

// Assign a radius
radius = 20;

// Compute area
area = radius * radius * 3.14159;

// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
16
Trace a Program
Execution
public class ComputeArea {
/** Main method */ memory
public static void main(String[] args) {
radius 20
double radius;
double area; area 1256.636

// Assign a radius
radius = 20;
compute area and assign it
to variable area
// Compute area
area = radius * radius * 3.14159;

// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
17
Trace a Program
Execution
public class ComputeArea {
/** Main method */ memory
public static void main(String[] args) {
radius 20
double radius;
double area; area 1256.636

// Assign a radius
radius = 20;

// Compute area print a message to the


area = radius * radius * 3.14159;
console

// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
18
Variables

// Compute the first area


radius = 1.0;
area = radius * radius * 3.14159;
System.out.println("The area is “ + area + " for radius
"+radius);

// Compute the second area


radius = 2.0;
area = radius * radius * 3.14159;
System.out.println("The area is “ + area + " for radius
"+radius);
19
Reading Input from the Console

1. Create a Scanner object


Scanner input = new Scanner(System.in);
2. Use the method nextDouble() to obtain to a double value. For
example,
System.out.print("Enter a double value: ");
Scanner input = new Scanner(System.in);
double d = input.nextDouble();

20
Reading Numbers from the
Keyboard
Scanner input = new Scanner(System.in);
int value = input.nextInt();
Method Description

nextByte() reads an integer of the byte type.


nextShort() reads an integer of the short type.
nextInt() reads an integer of the int type.
nextLong() reads an integer of the long type.
nextFloat() reads a number of the float type.
nextDouble() reads a number of the double type.

next().CharAt(0) reads a character


21
Trace a Program
Execution
public class ComputeArea {
/** Main method */
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter a double radius value: ");

double radius = input.nextDouble();

double area;

// Compute area
area = radius * radius * 3.14159;

// Display results
System.out.println("The area for the circle of radius "
+ radius + " is " + area);
}
} 22
Problem: Displaying
Time
Write a program that obtains minutes and remaining
seconds from seconds.

23
program that obtains minutes and
remaining seconds from seconds
 public class DisplayTime {
 public static void main(String[] args) {
 Scanner input = new Scanner(System.in);
 // Prompt the user for input
 System.out.print("Enter an integer for seconds: ");
 int seconds = input.nextInt();

 int minutes = seconds / 60; // Find minutes in seconds
 int remainingSeconds = seconds % 60; // Seconds remaining
 System.out.println(seconds + " seconds is " + minutes +
 " minutes and " + remainingSeconds + " seconds");
 }
 }

PAGE 24
Number Literals

A literal is a constant value that appears directly in the program.


For example, 34, 1,000,000, and 5.0 are literals in the following
statements:
int i = 34;
long x = 1000000;
double d = 5.0;

25
Integer Literals
An integer literal can be assigned to an integer variable as long as it can
fit into the variable. A compilation error would occur if the literal were
too large for the variable to hold. For example, the statement byte b =
1000 would cause a compilation error, because 1000 cannot be stored
in a variable of the byte type.
An integer literal is assumed to be of the int type, whose value is
between -231 (-2147483648) to 231–1 (2147483647). To denote an
integer literal of the long type, append it with the letter L or l. L is
preferred because l (lowercase L) can easily be confused with 1 (the
digit one).

26
Floating-Point Literals
Floating-point literals are written with a decimal point. By default,
a floating-point literal is treated as a double type value.
For example, 5.0 is considered a double value, not a float value.
You can make a number a float by appending the letter f or F, and
make a number a double by appending the letter d or D.
For example, you can use 100.2f or 100.2F for a float number, and
100.2d or 100.2D for a double number.

27
double vs. float

The double type values are more accurate than the float type values. For example,

System.out.println("1.0 / 3.0 is " + 1.0 / 3.0);

displays 1.0 / 3.0 is 0.3333333333333333

16 digits

System.out.println("1.0F / 3.0F is " + 1.0F / 3.0F);

displays 1.0F / 3.0F is 0.33333334


7 digits

28
Arithmetic Expressions

3+4 𝑥 10( 𝑦 − 5)(𝑎+𝑏+𝑐) 4 9+𝑥


− +9( + )
5 𝑥 𝑥 𝑦

is translated to

(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)

29
How to Evaluate an
Expression
3 + 4 * 4 + 5 * (4 + 3) - 1
(1) inside parentheses first
Though Java has its own way to evaluate an 3 + 4 * 4 + 5 * 7 – 1
(2) multiplication
expression behind the scene, the result of a Java 3 + 16 + 5 * 7 – 1
(3) multiplication
expression and its corresponding arithmetic 3 + 16 + 35 – 1
expression are the same. Therefore, you can (4) addition
19 + 35 – 1
safely apply the arithmetic rule for evaluating a (5) addition
54 - 1
Java expression. (6) subtraction
53

30
Problem: Converting
Temperatures

Write a program that converts a Fahrenheit degree to Celsius using the


formula:
5
𝑐𝑒𝑙𝑠𝑖𝑢𝑠=( )( 𝑓𝑎h𝑟𝑒𝑛h𝑒𝑖𝑡 − 32)
9

Note: you have to write


celsius = (5.0 / 9) * (fahrenheit – 32)

31
Problem: Converting
Temperatures
 public class FahrenheitToCelsius {
 public static void main(String[] args) {
 Scanner input = new Scanner(System.in);

System.out.print("Enter a degree in Fahrenheit: ");
 double fahrenheit = input.nextDouble();

// Convert Fahrenheit to Celsius
 double celsius = (5.0 / 9) * (fahrenheit - 32);
 System.out.println("Fahrenheit " + fahrenheit + " is " +
 celsius + " in Celsius");
 }
 }

PAGE 32
Augmented Assignment
Operators

33
Increment and Decrement
Operators

34
Increment and Decrement
Operators, cont.

int i = 10; Same effect as


int newNum = 10 * i++; int newNum = 10 * i;
i = i + 1;

int i = 10; Same effect as


int newNum = 10 * (++i); i = i + 1;
int newNum = 10 * i;

35
Numeric Type
Conversion
Consider the following statements:
byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;

36
Conversion Rules

 When performing a binary operation involving two operands of different


types, Java automatically converts the operand based on the following rules:

1. If one of the operands is double, the other is converted into double.


2. Otherwise, if one of the operands is float, the other is converted into float.
3. Otherwise, if one of the operands is long, the other is converted into long.
4. Otherwise, both operands are converted into int.

37
Type Casting
Implicit casting
double d = 3; (type widening)

Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (Fraction part is truncated)

What is wrong? int x = 5 / 2.0;


range increases

byte, short, int, long, float, double

38
Type Casting

 Widening example • Narrowing example


public static void main(String[] args) { public static void main(String[] args) {
int myInt = 9; double myDouble = 9.78d;
// Automatic casting: int to double // Manual casting: double to int
double myDouble = myInt; int myInt = (int) myDouble;

System.out.println(myInt); // Outputs 9 System.out.println(myDouble); // Outputs 9.78


System.out.println(myDouble); // Outputs 9.0 System.out.println(myInt); // Outputs 9
} }

PAGE 39
Non primitive data type
 Java Strings
Strings are used for storing text.

A String variable contains a collection of characters surrounded by double quotes:

Create a variable of type String and assign it a value:

String greeting = "Hello";

40

You might also like