0% found this document useful (0 votes)
4 views22 pages

Unit-1 Data Types and Expression

This document provides an overview of data types, variable declarations, and expressions in Java. It covers various numeric data types, assignment statements, arithmetic expressions, and the rules for type casting. Additionally, it explains the use of compound arithmetic and assignment operators for efficient coding.

Uploaded by

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

Unit-1 Data Types and Expression

This document provides an overview of data types, variable declarations, and expressions in Java. It covers various numeric data types, assignment statements, arithmetic expressions, and the rules for type casting. Additionally, it explains the use of compound arithmetic and assignment operators for efficient coding.

Uploaded by

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

Data types, declarations, and

expressions in Java
Unit-1
Variables
• A variable is a named memory location
capable of storing data
• As we have already seen, object variables
refer to objects, which are created by
instantiating classes with the new operator
• We can also store data in simple variables,
which represent data only, without any
associated methods
Data declaration syntax
• The syntax for the declaration of a variable
is:
Data type identifier;
– “data type” may be the name of a class, as we
have seen, or may be one of the simple types,
which we’ll see in a moment
– “identifier” is a legal Java identifier; the rules
for simple variable identifiers are the same as
those for object identifiers
Variable declaration: examples
• For example:
int age; // int means integer
double cashAmount; // double is a real #
• We can also declare multiple variables of the same type
using a single instruction; for example:
int x, y, z; //or
int x,
y,
z;
• The second way is preferable, because it’s easier to
document the purpose of each variable this way.
Numeric data types in Java:
integers
Data type name Minimum value Maximum value

byte -128 127

short -32,768 32,767

int -2,147,483,648 2,147,483,647

long - 9,223,372,036,85
9,223,372,036,854,775,
808 4,775,807
Numeric data types in Java:
floating-point numbers
Data type Minimum value Maximum value
name
float -3.40282347 x 1038 3.40282347 x 1038

double -1.79769313486231570 1.79769313486231570


x 10308 x 10308
Numeric data types: some notes
• Most programmers use int for whole numbers and
double for real numbers
• Numeric data types in Java are primitive (non-
object) types; this means that a numeric variable is
somewhat different from an object:
– You don’t use the new operator to initialize a numeric
variable – just assign it a value
– Memory for a numeric variable is allocated at declaration
– Numeric variables actually store values; object names
store addresses
Scientific notation and real
numbers
• Both float and double have wide ranges to the values they
can represent
• In order to save space, particularly large or small values
are often displayed by default using a variation of
scientific notation
• For example, the value .0000258 would appear as 2.58 x
10-5 in conventional notation – as output from a Java
program, the number would appear as 2.58e-5
• The ‘e’ is for exponent, and can be upper or lowercase
Assignment statements
• We can store a value in a variable using an
assignment statement
• Assignment statement syntax:
variableName = expression;
– variableName must be the name of a declared
variable
– expression must evaluate to an appropriate
value for storage within the type of variable
specified
Arithmetic expressions
• An expression is a set of symbols that represents a
value
• An arithmetic expression represents a numeric
value
• Simple expressions are single values; examples:
18
-4
1.245e3
• Previously-declared and initialized variables or
constants can also be simple expressions
Arithmetic operators in Java
• Compound Operation Symbol
expressions are Addition +
formed by
combining simple Subtraction -
expressions using Multiplication *
arithmetic
operators Division /

Modulus %
Arithmetic operations in Java
• As in algebra, multiplication and division (and
modulus, which we’ll look at momentarily) take
precedence over addition and subtraction
• We can form larger expressions by adding more
operators and more operands
– Parentheses are used to group expressions, using the same
rule as in algebra: evaluate the innermost parenthesized
expression first, and work your way out through the levels
of nesting
– The one complication with this is we have only parentheses
to group with; you can’t use curly or square brackets, as
they have other specific meanings in Java
Examples
int x = 4, y = 9, z;

z = x + y * 2; // result is 22
z = (x + y) * 2; // result is 26
y = y – 1; // result is 8
Integer division
• When one real number is divided by another, the
result is a real number; for example:
double x = 5.2, y = 2.0, z;
z = x / y; // result is 2.6
• When dividing integers, we get an integer result
• For example:
int x = 4, y = 9, z;
z = x / 2; // result is 2
z = y / x; // result is 2, again
z = x / y; // result is 0
Integer division
• There are two ways to divide integers
– using the / operator, produces the quotient of the
two operands
– using the % operator, produces the remainder when
the operands are divided. This is called modular
division, or modulus (often abbreviated mod). For
example:
int x = 4, y = 9, z;
z = x % 2; // result is 0
z = y % x; // result is 1
z = x % y; // result is 4
Mixed-type expressions
• A mixed-type expression is one that involves operands of
different data types
– Like other expressions, such an expression will evaluate to a single
result
– The data type of that value will be the type of the operand with the
highest precision
– What this means, for all practical purposes, is that, if an
expression that involves both real numbers and whole
numbers, the result will be a real number.
• The numeric promotion that takes place in a mixed-type
expression is also known as implicit type casting
Explicit type casting
• We can perform a deliberate type conversion of an
operand or expression through the explicit cast
mechanism
• Explicit casts mean the operand or expression is
evaluated as a value of the specified type rather
than the type of the actual result
• The syntax for an explicit cast is:
(data type) operand -or-
(data type) (expression)
Explicit type casts - examples
int x = 2, y = 5;
double z;

z = (double) y / z; // z = 2.5
z = (double) (y / z); // z = 2.0
Assignment conversion
• Another kind of implicit conversion can
take place when an expression of one type
is assigned to a variable of another type
• For example, an integer can be assigned to a
real-number type variable; in this case, an
implicit promotion of the integer value
occurs
No demotions in assignment
conversions
• In Java we are not allowed to “demote” a higher-
precision type value by assigning it to a lower-
precision type variable
• Instead, we must do an explicit type cast. Some
examples:
int x = 10;
double y = x; // this is allowed; y = 10.0
x = y; // error: can’t demote value to int
y = y / 3; // y now contains 3.3333333333333333
x = (int)y; // allowed; x = 3
Compound arithmetic/assignment
operators
• Previous examples in the notes have included the
following statements:
y = y + 1;
y = y / 3;
• In each case, the current value of the variable is used to
evaluate the expression, and the resulting value is assigned
to the variable (erasing the previously-stored value)
• This type of operation is extremely common; so much so,
that Java (like C++ and C before it) provides a set of
shorthand operators to perform this type of operation. The
table on the next slide illustrates the use and meaning of
these operators
Compound arithmetic/assignment
operators
Operator Use Meaning

+= X += 1; X = X + 1;

-= X -= 1; X = X – 1;

*= X *= 5; X = X * 5;

/= X /= 2; X = X / 2;

%= X %= 10; X = X % 10;

You might also like