Professional Documents
Culture Documents
Object Oriented Programming With Java: Department of Ce/It Unit-1 Java Overview OOPJ (01CE0403)
Object Oriented Programming With Java: Department of Ce/It Unit-1 Java Overview OOPJ (01CE0403)
CE/IT
Java
The Three
OOP
Principles
Inheritance: It is a mechanism in which one class acquires
the property of another class. For example, a child inherits
the traits of his/her parents.
The Three
OOP
Principles
Polymorphism: It means "many forms", and it occurs when
we have many classes that are related to each other by
inheritance.
The Three
OOP
Principles
Simple
Secure
Portable
Object-oriented
Robust
The Java Multithreaded
Buzzwords Architecture-neutral
Interpreted
High performance
Distributed
Dynamic
Java bytecode is the instruction set for the Java Virtual Machine.
Jcreator
Program
Editors NetBeans
Eclipse
class Example
{
public static void main(String args[])
A First Simple
{
Program
System.out.println(“First Java
Example java program");
}
}
Compilation:
D:\Java\>javac Example.java
javac Path should be Set
set path=%path%; c:\Program Files\Java\jdk6_\bin
Compiling &
Executing the Example.java Should be in Current directory
Program
Execution:
D:\Java\>java Example
Example.class should be in current directory
abstract continue for new switch
Characters: char
Boolean: boolean
Datatype Size Description
Floating-Point Literals
Boolean Literals
Character Literals
Escape Description
Sequence
Variables Syntax:
Example:
Java's
Automatic
Conversions
class Test
{
public static void main(String[] args)
{
int i = 100;
(target-type) value
class Test
{
public static void main(String[] args)
{
double d = 100.04;
//explicit type casting
Casting long l = (long)d;
//explicit type casting
Incompatible int i = (int)l;
System.out.println("Double value "+d);
Types //fractional part lost
System.out.println("Long value "+l);
//fractional part lost
System.out.println("Int value "+i);
}
}
Operators are used to perform operations on variables and
values.
Assignment /= x /= 3 x=x/3
Operators %= x %= 3 x=x%3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Operator Name Example
== Equal to x == y
!= Not equal x != y
Byte.parseByte()
Command Short.parseShort()
Line Integer.parseInt()
Argument Long.parseLong()
Float.parseFloat()
Double.parseDouble()
Scanner class methods:
Syntax:
if(condition)
if Statement
{
//code to be executed
}
class IfStatement
{
public static void main(String[] args) {
int number = 10;
// checks if number is greater than 0
If-else {
//code to be executed
Statement }
else
{
//code to be executed when condition fails
}
class Main {
public static void main(String[] args) {
int number = 10;
// checks if number is greater than 0
if (number > 0) {
if-else-if }
else if (number < 0) {
Ladder
System.out.println("The number is negative.");
Example }
else {
System.out.println("The number is 0.");
}
}
}
The Java switch statement executes one statement from multiple
conditions, and its like if-else-if ladder statement.
The switch statement works with byte, short, int, long, enum types,
String.
Syntax:
switch(expression){
Switch case case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
default:
code to be executed if all cases are not matched;
}
class Main
{
public static void main(String[] args)
{
int expression = 2;
// switch statement to check size
switch (expression) {
case 1:
Switch case System.out.println("Case 1");
// matching case
Example case 2:
System.out.println("Case 2");
case 3:
System.out.println("Case 3");
default:
System.out.println("Default case");
}
}
}
Iteration statements create loops in the program.
while loop
do-while loop
The Java while loop is a control flow statement that
executes a part of the programs repeatedly on the basis of
given boolean condition.
Syntax:
while
while(condition)
//code to be executed
}
class Main {
public static void main(String[] args) {
// declare variables
int i = 1, n = 5;
// while loop from 1 to 5
while while(i <= n) {
System.out.println(i);
Example i++;
}
}
}
If the number of iteration is not fixed and you must have to
execute the loop at least once, it is recommended to use the
do-while loop.
Syntax:
do-while do
// body of loop
} while (condition);
class Main
{
public static void main(String[] args)
{
int i = 1, n = 5;
d0-while do {
Example System.out.println(i);
i++;
} while(i <= n);
}
}
The Java for loop is a control flow statement that iterates a
part of the programs multiple times.
for Syntax:
// body
}
class Main
{
public static void main(String[] args)
{
int n = 5;
for loop // for loop
for (int i = 1; i <= n; ++i) {
Example System.out.println("Java is fun");
}
}
}
The jumping statements are the control statements which
transfer the program execution control to a specific
statements.
Jump
Statements Three types :break, continue, and return.
{
for (int i = 1; i <= 10; ++i) {
break if (i == 5) {
Example break;
}
System.out.println(i);
}
}
}
The Java continue statement is used to continue the loop.
It continues the current flow of the program and skips the
remaining code at the specified condition.
In case of an inner loop, it continues the inner loop only.
continue while and do-while
Control transferred directly to the conditional
expression
In a for loop
Control goes first to the iteration portion
class Main
{
public static void main(String[] args)
{
// for loop
for (int i = 1; i <= 10; ++i) {
// if value of i is between 4
continue // and 9 continue is executed
if (i > 4 && i < 9)
Example {
continue;
}
System.out.println(i);
}
}
}
A return statement causes the program control to transfer
back to the caller of a method.