0% found this document useful (0 votes)
13 views18 pages

Data Type

Uploaded by

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

Data Type

Uploaded by

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

Two Control Statements

The if Statement The Java if statement works much like the IF statement in
any other language.

Its simplest form is shown here:


if(condition) statement
Here, condition is a Boolean expression.
If condition is true, then the statement is executed.
If condition is false, then the statement is bypassed.
Here is an example:
if(num < 100) System.out.println("num is less than 100")
class IfSample
{
public static void main(String args[])
{
int x, y;
x = 10;
y = 20;
if(x < y)
System.out.println("x is less than y");
x = x * 2;
if(x == y)
System.out.println("x now equal to y");
x = x * 2;
if(x > y)
System.out.println("x now greater than y");
if(x == y)
System.out.println("you won't see this");
}
}
Output
x is less than y
x now equal to y
x now greater than y
FOR LOOP
• for(initialization; condition; iteration) statement;
• The initialization portion of the loop sets a loop control
variable to an initial value.
• The condition is a Boolean expression that tests the loop
control variable.
• If the outcome of that test is true, the for loop continues to
iterate.
• If it is false, the loop terminates.
• The iteration expression determines how the loop control
variable is changed each time the loop iterates.
• Here is a short program that illustrates the for loop:
class ForTest
{
public static void main(String args[])
{
int x;
for(x = 0; x <=10; x= x+1)
System.out.println("This is x: " + x);
}
}
Output

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.

Casting Incompatible Types

class Conversion { output:


public static void main(String args[]) { Conversion of int to byte.
byte b; i and b 257 1
int i = 257; Conversion of double to int.
double d = 323.142; d and i 323.142 323
System.out.println("\nConversion of int to byte."); Conversion of double to byte.
b = (byte) i; d and b 323.142 67
System.out.println("i and b " + i + " " + b);
System.out.println("\nConversion of double to int.");
i = (int) d;
System.out.println("d and i " + d + " " + i);
System.out.println("\nConversion of double to byte.");
b = (byte) d;
System.out.println("d and b " + d + " " + b);
}
}
The Java Keywords

You might also like