2-Data Types and Variables
2-Data Types and Variables
Variables
and
Array
Java Is a Strongly Typed Language
• First, every variable has a type, every expression has a type,
and every type is strictly defined.
• Second, all assignments, whether explicit or via parameter
passing in method calls, are checked for type compatibility.
• There are no automatic conversions of conflicting types as in
some languages.
• The Java compiler checks all expressions and parameters to
ensure that the types are compatible.
• Any type mismatches are errors
Note: in C and C++ allow the size of an integer to vary based upon execution
environment. However, Java is different. Because of Java’s portability requirement,
all data types have a strictly defined range. For example, an int is always 32 bits,
regardless of the particular platform.
1. Integers
• Java defines four integer types :
• All of these are signed, positive and negative values. Java does not support
unsigned.
• The width and ranges of these integer types vary widely, as shown in this table:
• Name Width Range
• byte 8 –128 to 127
• short 16 –32,768 to 32,767
• int 32 –2,147,483,648 to 2,147,483,647
• long 64 –9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
• For example:
– byte a, b;
– Long p,q,r;
– Short x,y,z;
Example
• For example, here is a program that computes the number of miles that light will travel in a specified
number of days.
• // Compute distance light travels using long variables.
class Light {
public static void main(String args[]) {
int lightspeed;
int days;
long seconds;
long distance;
// approximate speed of light in miles per second
lightspeed = 186000;
days = 1000; // specify number of days here
seconds = days * 24 * 60 * 60; // convert to seconds
distance = lightspeed * seconds; // compute distance
System.out.print("In " + days);
System.out.print(" days light will travel about ");
System.out.println(distance + " miles.");
}
}
This program generates the following output:
In 1000 days light will travel about 16070400000000 miles.
2. Floating-Point Types
• Floating-point numbers, also known as real numbers, are used when evaluating expressions that require
fractional precision.
• There are two kinds of floating-point types, float and double, which represent single- and double-precision
numbers, respectively.
Name Width Approximate Range
1. double 64 4.9e–324 to 1.8e+308
2. float 32 1.4e−045 to 3.4e+038
• Here is a short program that uses double variables to compute the area of a circle: Compute the area of a circle.
class Area {
public static void main(String args[]) {
double pi, r, a;
r = 10.8; // radius of circle
pi = 3.1416; // pi, approximately
a = pi * r * r; // compute area
b = false;
if(b)
System.out.println("This is not executed.");
// outcome of a relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));
}
}
Continue…
• The output generated by this program is
shown here:
b is false
b is true
This is executed.
10 > 9 is true
Literals.
• A constant value in Java is created by using a literal
representation of it. For example,
• here are some literals:
100 98.6 ‘X’ “This is a test”
(1) (2) (3) (4)
1. specifies an integer,
2. specifies a floating-point value.
3. specifies a character constant,
4. specifies a string.
Integer Literals.
• Any whole number value is an integer literal.
Examples are 1, 2, 3, and 42.
• Octal values are denoted in Java by a leading
zero as 07,06 but 09 is not.
• a hexadecimal constant with a leading zero-x,
(0x or 0X). The range of a hexadecimal digit is
0 to 15, so A through F (or a through f ) are
substituted for 10 through 15.
Floating-Point Literals
• They can be expressed in either standard or scientific notation.
• Standard notation consists of a whole number component followed
by a decimal point followed by a fractional component. For example,
2.0, 3.14159, and 0.6667 represent valid standard-notation
• Scientific notation consists of a floating-point number plus a suffix
that specifies a power of 10 by which the number is to be
multiplied.