Data Type
Data Type
The if Statement The Java if statement works much like the IF statement in
any other language.
This is x: 0
This is x: 1
This is x: 2
This is x: 3
This is x: 4
This is x: 5
This is x: 6
This is x: 7
This is x: 8
This is x: 9
Using Blocks of Code
if(x < y)
{ // begin a block
x = y;
y = 0;
} // end of block
Primitive (built‐in) Data types
• Integers
– byte 8‐bit integer (new)
– short 16‐bit integer
– int 32‐bit signed integer
– long 64‐bit signed integer
• Real Numbers
– float 32‐bit floating‐point
– double number 64‐bit floating‐
• Other point number
types
– char 16‐bit, Unicode 2.1 character
– boolean true or false, false is not 0 in Java
class Light
{
public static void main(String args[])
{
int lightspeed;
long days;
long seconds;
long distance;
// approximate speed of light in miles per second
lightspeed = 186000;
days = 1000; // specify number of days hereseconds = 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.");
}
}
output:
In 1000 days light will travel about 16070400000000 miles.
Clearly, the result could not have been held in an int variable.
Floating-Point Types
Double & Float
class Area
{
public static void main(String args[])
{
double pi, r
float a;
r = 10.8; // radius of circle
pi = 3.1416; // pi, approximately
a = pi * r * r; // compute area
System.out.println("Area of circle is " + a);
}
}
Characters
class CharDemo
{
public static void main(String args[])
{
char ch1, ch2;
ch1 = 88; // code for X
ch2 = 'Y';
System.out.print("ch1 and ch2: ");
System.out.println(ch1 + " " + ch2);
}
}
OUTPUT:
ch1 and ch2: X Y
Boolean Type- True/ False
class BoolTest
{
public static void main(String args[])
{
Output:
boolean b;
b is false
b = false;
b is true
System.out.println("b is " + b);
This is executed.
b = true;
10 > 9 is true
System.out.println("b is " + b);
// a boolean value can control the if statement
if(b) System.out.println("This is executed.");
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));
}
}
Non‐primitive Data types
• The non‐primitive data types in java are
– Objects
– Array
• Non‐primitive types are also called reference
types
Primitive vs. Non‐primitive type
• Primitive types are handled by value – the actual
primitive values are stored in variable and passed to
methods
int x = 10;
public MyPrimitive(int x)
{
}
• Non‐primitive data types (objects and arrays) are
handled by reference – the reference is stored in
variable and passed to methods
Box b = new Box(1,2,3);
public MyNonPrimitive(Box x)
{
}
Primitive vs. Non‐primitive type
• Primitive types are handled by value
– There is no easy way to swap two primitive integers in Java
– No method like void swap(int *x, int *y)
– Can only be done using object or array
• But do we actually need a method to swap?
– x += (y - (y = x)) does the same in a single statement
Variables
type identifier [ = value][, identifier [= value] ...]
Ex:
int a, b, c; // declares three ints, a, b, and c.
int d = 3, e, f = 5; // declares three more ints, initializing
// d and f.
byte z = 22; // initializes z.
double pi = 3.14159; // declares an approximation of pi.
char x = 'x';
The Scope and Lifetime of Variables
Dynamic Initialization Categories of scopes: global and local.
class DynInit class Scope
{ {
public static void main(String args[])
public static void main(String args[]) {
{ int x; // known to all code within main
double a = 3.0, b = 4.0; x = 10;
// c is dynamically initialized if(x == 10)
double c = Math.sqrt(a * a + b * b); { // start new scope
System.out.println("Hypotenuse is " + c); int y = 20; // known only to this block
} // x and y both known here.
System.out.println("x and y: " + x + " " + y);
} x = y * 2;
}
// y = 100; // Error! y not known here
// x is still known here.
System.out.println("x is " + x);
}
}
Type Conversion and Casting
Java’s Automatic Conversions
When one type of data is assigned to another type of variable, an automatic type
conversion will take place if the following two conditions are met:
• The two types are compatible.
• The destination type is larger than the source type.