CSE 110 ASU : Module 2

2.1 What is Data Type?

2.2 Java Data Types

Primitive

Type Size(Bits)

byte - 8

short - 16

int - 32

long - 64

float - 32

double - 64

Java's String Data Type - is not a primitive type.

  • If we want to get the computer to do math operations for us, then we will have to use the numeric data types like int and double.

2.3 What are Expressions?

Expressions - always evaluate to a value.

int literals are positive or negative numbers that do not have a decimal point.

Examples:

47

1337

-56

0

float literals are positive or negative numbers that can have a decimal point and a fractional component (which may be zero), and that have a f suffix.

Examples:

47f

13.37f

-56.2f

0.0f

double literals are positive or negative numbers that have a decimal point and a fractional component (which may be zero).

Examples:

47.65

3.14159

-56.2

0.0

char literals are single characters placed between single-quotes. Note that numbers can be represented in text as characters and as Strings, but they lose their arithmetic properties.

Examples:

'A'

'z'

'&'

'9'

Strings literals are groups of characters (zero or more) placed between double quotes.

Examples:

"Hello World!"

"Yes, I have 10 bananas."

"fudge"

"99 red balloons"

2.4 What are Operators?

Operators - operate on operands.

Operation

Java Operator

Addition

+

Subtraction

-

Multiplication

*

Division

/

Modulus

%

Example

Operands - Input

Operators - are instructions telling the computer what to do.

Integer Division - an int divided by an int will always give an INT result in JAVA.

2.5 How is an Expression Evaluated to a Value?

Compound Expressions - are expression with sub-expressions in them

important to remember that in Java, when you divide an int by another int, the result will always be an int, with any fractional component truncated (removed).**

2.6 What are String Expressions?

Escape Sequence - is a combination of symbols, starting with a backslash,

that denotes a special character like a Tab or a Newline,

in a String literal.

Concatenation - Is building longer Strings out of shorter Strings.

Escape Sequence

What it represents

\n

Newline character

\t

Tab character

\"

Double Quote character

\'

Single Quote character

\\

Backslash character

2.7 What is Type Casting?

Widening Cast -

From Type

To Type

int

float

int

double

float

double

Implicit Cast - Java will automatically cast a value of a narrower type

to an equivalent value of a wider type.

Narrowing Cast - will convert data from a data type with a wider range of valid values to a data type with a narrower range of valid values.

From Type

To Type

double

float

double

int

float

int


2.8 How Can We Convert Data Types?

Cast Operations & Parse Methods - are expressions, and expressions always evaluated to a value.

Java's parse method - The most commonly used parse methods for converting String data to numeric data are Integer.parseInt, Float.parseFloat, and Double.parseDouble.

2.9 How are Expressions Used?

2.10 Variables and assignments

ā€œ=ā€ - is not equals

In programming, = is an assignment of a left-side variable with a right-side value. = is NOT equality as in mathematics. Thus, x = 5 is read as "x is assigned with 5", and not as "x equals 5". When one sees x = 5, one might think of a value being put into a box.

2.11 Variables (int)

Variable Declaration - is a statement that declares a new variable, specifying the variable's name and type.

Allocation - is the process of determining a suitable memory location to store data like variables.

Assignment statement assigns - the variable on the left-side of the = with the current value of the right-side expression. Ex: numApples = 8; assigns numApples with the value of the right-side expression (in this case 8). assign

2.12 Identifiers

A name created by a programmer for an item like a variable or method is called an identifier. An identifier must:

be a sequence of letters (a-z, A-Z), underscore (_), dollar signs ($), and digits (0-9)

start with a letter, underscore, or dollar sign

2.13 Arithmetic expressions (general)