0% found this document useful (0 votes)
2 views20 pages

Java Fundamentals: Constants & Data Types

This is the programming 3rd lecture for java learner

Uploaded by

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

Java Fundamentals: Constants & Data Types

This is the programming 3rd lecture for java learner

Uploaded by

m73240483
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Starting Out with Java

From Control Structures through Objects

Session 3 (Chapter 2): Java Fundamentals

Dr. Muhammad Faheem

November 24, 2020

1 / 20
Constants

Memory location whose content cannot change during


execution.

Problem: Programs with literal values can make the program


hard do read and maintain.

Solution: Replacing literal values with constants remedies this


problem.

Advantages:
Constants allow the programmer to use a name rather than a
value throughout the program.
Constants also give a singular point for changing those values
when needed.
Constants keep the program organized and easier to maintain.

2 / 20
Constants

Constants are identifiers that can hold only a single value.

Declared using the keyword final .

Constants need not be initialized when declared; however, they


must be initialized before they are used or a compiler error will
be generated.

3 / 20
Example: Constants

final int MAX_STUDENT = 40; // Numeric Constant

final float SALES_TAX = 0.13; // Numeric Constant

final String DEPARTMENT_NAME = "Computer Science"; //String


Constant

4 / 20
The + Operator

Purpose:

String Concatenation (as a concatenation operator)

Numerical Addition (as a addition operator)

5 / 20
String Concatenation

Multiple lines string literal value is not allowed in Java.

Example (Following code will generate compiler error):


[Link]("This line is too long and now it
has spanned more than one line, which will cause a
syntax error to be generated by the compiler. ");

6 / 20
String Concatenation

However, sometime we use multiple lines for readability.

Solution (+ operator):
[Link]("This line is too long and now" +
"it has spanned more than one line using addition" +
"operator");

7 / 20
String Concatenation
The + operator can be used to concatenate variable value
with string.

Example:
int numberOfStudents = 40;
[Link]("Number of Students: " +
numberOfStudents);

Here, Java does implicit type casting.

First it converts the variable value (if number) to string.

Then appends it with string literal value.

Output:
Number of Students: 40
8 / 20
String Concatenation

The + operator can be used to format complex string objects.

Example:
[Link]("The following will be printed " +
"in a tabbed format: " +
"\n\tFirst = " + 5 * 6 + ", " +
"\n\tSecond = " (6 + 4) + "," +
"\n\tThird = " + 16.7 + ".");

Notice: If an addition operation required then use parentheses!

9 / 20
Primitive Data Types

Data Type: Set of values together with a set of operations.

Primitive data types are built into the Java language and are
not derived from classes.

1) byte 5) float
2) short 6) double
3) int 7) boolean
4) long 8) char

10 / 20
Numeric Data Types

byte 1 byte Integers Range: -128 to +127

short 2 bytes Integers Range: -32,768 to +32,767

int 4 bytes Integers Range: -2,147,483,648 to +2,147,483,647

long 8 bytes Integers Range: -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

float 4 bytes Floating-point Numbers Range: ±3.4x10−38 to ±3.4x1038 , with 7 digits of accuracy

double 8 bytes Floating-point Numbers Range: ±1.7x10−308 to ±1.7x10308 , with 15 digits of accuracy

11 / 20
Numeric Data Types

Cannot use a comma within a Numeric value.

Example (not allowed): 6,725

Commas are only used for seperating items in a list.

12 / 20
Incompatible Type Error

Incompatible Type Error: Variable receives a value that is not


compatible to its data type.

Example:
int numberOfLectures = 30;
numberOfLectures = "Thirty"; // Compiler will generate error

13 / 20
Example

See the Code Example 1 for Session 3!

14 / 20
Integer Data Types

Integer data types can hold only whole numbers.

It cannot hold numbers that have a decimal point.

byte, short, int, and long are all integer data types.

Integers embedded into Java source code are called integer


literals.

Default type for integer literal is int.

15 / 20
Floating Point Data Types
Data types that allow fractional values are called floating-point
numbers.

Example: 1.7, and -45.316

float (a.k.a. single precision, i.e. 7 decimal points), and


double (a.k.a. double precision, i.e. 15 decimal points) are
floating point data types.

Floating-point numbers embedded into Java source code are


called floating point literals.

Default type for floating point literals is double.

Java is a strongly-typed language (i.e. Compatible data type


values can be stored in variable).
16 / 20
Floating Point Data Types
A double value is not compatible with a float variable because
of its size and precision.

Example:
float number;
number = 23.5; // Error!

A double can be forced into a float by appending the letter F


or f to the literal.

Example:
float number;
number = 23.5F; // This will work

17 / 20
Scientific and E Notation

Floating-point literals can be represented in scientific notation.

Java uses E notation to represent values in scientific notation.

Decimal Notation Scientific Notation E Notation

247.91 2.4791x102 2.4791E2

0.00072 7.2x10−4 7.2E-4

2900000 2.9x106 2.9E6

18 / 20
Example

See the Code Example 2 for Session 3!

19 / 20
Credit

Tony Gaddis, Starting out with Java: From Control Structures


through Objects, 6th Edition, Pearson 2016.

20 / 20

You might also like