Unit - I: MCA-405 Elective-I: E1 (B) Java Programming & Technologies
Unit - I: MCA-405 Elective-I: E1 (B) Java Programming & Technologies
MCA-405
Elective-I : E1(b)
Java Programming & Technologies
James Gosling
WebRunner
Java(1995.5)
Browser implemented by Java
Hardware Platform
JDBC Drivers
■ Integers This group includes byte, short, Int, and long, which are for whole
valued signed numbers.
■ Floating-point numbers This group includes float and double, which represent
numbers with fractional precision.
■ Boolean This group includes Boolean, which is a special type for representing
true/false values.
MCA-405 Elective-I : E1(b) Java
Programming & Technologies prepared by
sudhir patel
Arithmetic Operators
• Arithmetic operators are used in mathematical expressions in the same way that
they are used in algebra. The following table lists the arithmetic operators:
Operator Result
+ Addition
– Subtraction (also unary minus)
* Multiplication
/ Division
% Modulus
++ Increment
+= Addition assignment
–= subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
– – Decrement
When these two conditions are met, a widening conversion takes place. For example,
the int type is always large enough to hold all valid byte values, so no explicit cast
statement is required.
For widening conversions, the numeric types, including integer and floating-point
types, are compatible with each other. However, the numeric types are not compatible
with char or boolean. Also, char and boolean are not compatible with each other.
As mentioned earlier, Java also performs an automatic type conversion when
storing a literal integer constant into variables of type byte, short, or long.
MCA-405 Elective-I : E1(b) Java
Programming & Technologies prepared by
sudhir patel
Casting Incompatible Types
Although the automatic type conversions are helpful, they will not fulfill all needs. For
example, what if you want to assign an int value to a byte variable? This conversion
will not be performed automatically, because a byte is smaller than an int. This kind of
conversion is sometimes called a narrowing conversion, since you are explicitly making
the value narrower so that it will fit into the target type.
To create a conversion between two incompatible types, you must use a cast. A cast
is simply an explicit type conversion. It has this general form:
(target-type) value
Here, target-type specifies the desired type to convert the specified value to. For
example, the following fragment casts an int to a byte. If the integer’s value is larger
than the range of a byte, it will be reduced modulo (the remainder of an integer
division by the) byte’s range.
int a;
byte b;
// ...
b = (byte) a;
MCA-405 Elective-I : E1(b) Java
Programming & Technologies prepared by
sudhir patel
Casting Incompatible Types
A different type of conversion will occur when a floating-
point value is assigned to an integer type: truncation. As
you know, integers do not have fractional components.
Thus, when a floating-point value is assigned to an
integer type, the fractional component is lost. For
example, if the value 1.23 is assigned to an integer, the
resulting value will simply be 1. The 0.23 will have been
truncated. Of course, if the size of the whole number
component is too large to fit into the target integer type,
then that value will be reduced modulo the target type’s
range.
MCA-405 Elective-I : E1(b) Java
Programming & Technologies prepared by
sudhir patel
Casting Incompatible Types example
The following program demonstrates some type conversions that require casts:
// Demonstrate casts.
class Conversion {
public static void main(String args[]) {
byte b;
int i = 257;
double d = 323.142;
System .out. println(“ \nConversion of int to byte.");
b = (byte) i;
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);
}
}
This program generates the following output:
Conversion of int to byte.
i and b 257 1
Conversion of double to int.
d and i 323.142 323
Conversion of double to byte.
d and b 323.142 67
MCA-405 Elective-I : E1(b) Java
Programming & Technologies prepared by
sudhir patel
if statement
The if statement is Java’s conditional branch statement. It can be used to route program
execution through two different paths. Here is the general form of the if statement:
if (condition) statement1;
else statement2;
if (i == 10) {
if (j < 20) a = b;
If (k > 100) c = d; // this if is
else a = c; // associated with this else
}
else a = d; // this else refers to if(i == 10)
As the comments indicate, the final else is not associated with if(j<20), because it is not
in the same block (even though it is the nearest if without an else). Rather, the final else
is associated with if(i==10). The inner else refers to if(k>100), because it is the closest if
within the same block.
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
...
case valueN:
// statement sequence
break;
default:
// default statement sequence
} MCA-405 Elective-I : E1(b) Java
Programming & Technologies prepared by
sudhir patel
A simple example of the switch.
class SampleSwitch {
public static void main(String args[]) {
for(int i=0; i<6; i++) The output produced by this
switch(i) { program is shown here:
case 0:
System.out.println ("i is zero.");
i is zero.
break; i is one.
case 1:
i is two.
System.out.println ("i is one.");
break; i is three.
case 2: i is greater than 3.
System.out.println("i is two.");
break; i is greater than 3.
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than 3.");
}
}
} MCA-405 Elective-I : E1(b) Java
Programming & Technologies prepared by
sudhir patel
While loop
The while loop is Java’s most fundamental looping statement. It
repeats a statement or block while its controlling expression is
true. Here is its general form:
while(condition) {
// body of loop
}
The condition can be any Boolean expression. The body of the
loop will be executed as long as the conditional expression is true.
When condition becomes false, control passes to the next line of
code immediately following the loop. The curly braces are
unnecessary
if only a single statement is being repeated.
MCA-405 Elective-I : E1(b) Java
Programming & Technologies prepared by
sudhir patel
While loop Example
Here is a while loop that counts When you run this program, it will
down from 10, printing exactly “tick” ten times:
ten lines of “tick”:Demonstrate tick 10
the while loop.
tick 9
tick 8
class While {
public static void main(String args[]) {
tick 7
int n = 10; tick 6
while(n > 0) { tick 5
System. out. println("tick " + n); tick 4
n--; tick 3
} tick 2
} tick 1
} MCA-405 Elective-I : E1(b) Java
Programming & Technologies prepared by
sudhir patel
do-while loop
// Demonstrate the do-while loop.
class DoWhile {
public static void main(String
args[]) {
int n = 10;
do {
System.out.println("tick " + n);
n--;
} while(n > 0);
}
}
MCA-405 Elective-I : E1(b) Java
Programming & Technologies prepared by
sudhir patel
The for Loop
The simplest form of the for loop is shown here:
In its most common form, 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
• superclass
•A class may have subclasses
•Each class inherits all the fields and methods
of its (possibly numerous) super classes
void birthday ( ) {
age++;
System.out.println (name + ' is
now ' + age);
}
}
MCA-405 Elective-I : E1(b) Java
Programming & Technologies prepared by
sudhir patel