Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 1

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 22

Unit -1 Introduction To Java

Que. What is Object Oriented Programming?

It is an approach that provides a way of modularizing programs by creating partitioned memory


area for both data and function that can be used as templates for creating copies of such modules
on demand.

Que. Basic concepts of OOP

 Class
Class is design time entity that consists of data and member function.
Class is a collection of objects of similar type.

 Object
Object is basic run time entity that consists of data and member function.
It is variable of class type.

 Data Encapsulation
Binding of data and methods into single unit (called class) is known as encapsulation.
Insulation of the direct access by program is called data hiding.

 Data Abstraction
The act of representing essential features without including background details or
explanations is called data abstraction.

 Inheritance
It is the process by which object of one class acquire properties of another class.

 Polymorphism
It means ability to take more than one form. There are two types of polymorphism.
(1) Compile time polymorphism
(2) Run time polymorphism

 Binding
Binding refers to the linking of a procedure call to the code to be executed in response to the
call.
Static binding means code associated with given procedure call is not known until the time
of call at compile time.
Dynamic binding means code associated with given procedure call is not known until the
time of call at runtime.

Que. What is Java?

Java is an object-oriented programming language which was developed by Sun Microsystems in.
Java programs are platform independant which means they can be run on any operating system
with any type of processor as long as the Java interpreter is available on that system. Java is a
case sensitive language.

Riddhi Mehta(ICT Department) Page 1


Unit -1 Introduction To Java

 History of Java:-
Java was developed by James gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike
Sheridan at Sun Microsystems Inc. in 1991. Initial name of this language was “Oak” but it
was renamed in 1995 as “Java”.

Que. Explain advantages of JAVA.

 Java is a powerful programming language. It is easy to learn for whom already familiar
with C or C++.
 Through inheritance, we can eliminate redundant code and extend use of classes.

 Programs written in Java can be smaller than C++.

 Its garbage collection helps you to reuse memory and also you can reuse people’s tested
code with fewer bugs.

 In java, development time may be as much as twice faster that C++ Programs.

 Avoid platform dependencies. Java programs can be run on any platform.

 Features of data hiding and absence of pointer make program more secure.

 Java byte codes run on any platform it is portable.

 You can update applets easily from a central server. New classes to be loaded without
recompiling entire program.

 It is easy to partition the work in a project based on objects.

Que. Explain Features of Java.

 Simple
Java is a small and simple language. Many features of C and C + + that are either redundant
or sources of unreliable code are not part of Java.
For example, Java does not use pointers, preprocessor header files, goto statement and many
others. Java is a simplified version of C++. Java is simple for development.

 Platform Independent
Platform independence means that a program can run on any computer system. Java
programs can run on any system for which a Java virtual machine has been installed.

Platform independence at the source level means that you can move Java source files from
system to system and have them compile and run on any system.

Riddhi Mehta(ICT Department) Page 2


Unit -1 Introduction To Java

Platform independence in Java doesn't stop at the source level, however. Java compiled
binary files are also platform independent and can run on multiple platforms (if they have a
Java virtual machine available) without the need to recompile the source.

 Portable
The most significant and important difference between java and other languages is its
portability.
Java programs can be easily moved from one computer system to other.
Changes and upgrades in OS, processor and system resources will not force any change in
java program.
Java ensures portability in two ways
->First, java compiler generates byte code instructions that can be implemented on any
machine.
->Secondly, the size of primitive data types is machine independent.

 Compiled and Interpreted


Usually a computer language is either compiled or interpreted. Java combines both these
approaches thus making Java a two-stage system.
First, Java compiler translates source code into what is known as bytecode instructions.
Bytecodes are not machine instructions and therefore, in the second stage, Java interpreter
generates machine code from byte code that can be directly executed by the machine that is
running the Java program. We can thus say that Java is both a compiled and an interpreted
language.

 Distributed
Java is designed as a distributed language for creating applications on networks. It has the
ability to share both data and programs.
Java applications can open and access remote objects on Internet as easily as they can do in
a local system. This enables multiple programmers at multiple remote locations to
collaborate and work together on a single project.

 Multithreaded
Multithreaded means handling multiple tasks simultaneously. Java supports multithreaded
programs. This means that we need not wait for the application to finish one task before
beginning another.
For example, we can listen to an audio clip while scrolling a page and at the same time
download an applet from a distant computer. This feature greatly improves the interactive
performance of graphical applications.
The Java runtime comes with tools that support multiprocess synchronization and
construct smoothly running interactive systems.

 High Performance
Java performance is impressive for an interpreted language mainly due to the use of
intermediate bytecode. According to Sun, Java speed is comparable to the native C/C + +,
Java architecture is also designed to reduce overheads during runtime. Further, the
incorporation of multithreading enhances the overall execution speed of Java programs.

Riddhi Mehta(ICT Department) Page 3


Unit -1 Introduction To Java

 Dynamic
Java is a dynamic language. Java is capable of dynamically linking in new class libraries,
methods, and objects. Java can also determine the type of class through a query, making it
possible to either dynamically link or abort the program, depending on the response.
Java programs support functions written in other languages such as C and C + +.
These functions are known as native methods. This facility enables the programmers to use
the efficient functions available in this language. Native methods are linked dynamically at
runtime.

 Object-Oriented
Java is a true object-oriented language. Almost everything in Java is an object. All program
code and data reside within objects and classes. Java comes with an extensive set of classes,
arranged in packages that we can use in our programs by inheritance. The object model in
Java is simple and easy to extend.

 Robust and Secure


Java is a robust language. It provides many safeguards to ensure reliable code. It has strict
compile time and run time checking for data types. It is designed as a garbage-collected
language relieving the programmers virtually all memory management problems. The
absence of pointers in java ensures that programs cannot gain access to memory locations
without proper authorization.

Que. Write difference between Java and C

SR JAVA C
NO
1 Java is object-oriented language. C is procedure oriented language.
2 Java does not support structure, union, C supports structure, union enum.
enum.
3 Java does not support Pointer. C supports pointer.
4 Java does not support Global variable C supports Global variable
5 Java does not support goto, sizeof, C supports goto, sizeof, typedef.
typedef.
6 There are no header files In java There is no. of header files In C.
7 Java does not support Pre-processor. C support Pre-processor.
8 Java doesn’t define type modifiers like C defines type modifiers keyword
auto, extern, register, signed, unsigned. Auto, extern, register, signed, unsigned.

Que. Write Difference between Java and C++

SR JAVA C++

Riddhi Mehta(ICT Department) Page 4


Unit -1 Introduction To Java

NO
1 Java does not support Operator C++ supports Operator overloading.
overloading.
2 Java does not support Template class. C++ support Template class.
3 Java does not support Multiple C++ supports Multiple inheritance.
Inheritance but it supports interface.
4 Java does not support Global variable C++ support Global variable
5 Java does not use Pointer. C++ use Pointer.
6 There are no header files in java There is no. of header files In C++.
7 Java has replace destructor Function C++ has a constructor & also
with finalize ( ). Destructor Function.
8 It does not support Pre-processor. C++ support Pre-processor.

Que. What is Bytecode? Explain Java Virtual Machine (JVM).

The output of a Java compiler is bytecode not the machine code (“.class” file). Bytecode is a
highly optimized set of instructions designed to be executed by the Java run-time system, which
is called as JVM (Java Virtual Machine). JVM is the interpreter which interprets the bytecode.

JVM is an abstract computing machine like any other real computing machine which interpretes
the byte code (.class file which contains virtual machine instructions) to a machine dependent
native code. The output of JVM is the native code contains low level instructions which are
easily understandable by the micro processors which process the native code to give the desire
output.

A Java Virtual Machine (JVM) is a set of computer software programs and data structures which
use a virtual machine model for the execution of other computer programs and scripts.

The JVM runtime executes .class or .jar files, emulating the JVM instruction set by interpreting
it, or using a just-in-time compiler (JIT) such as Sun's Hotspot. JIT compiling, not interpreting, is
used in most Jams today to achieve greater speed. Like most virtual machines, the Java Virtual
Machine has a stack-based architecture.

Que. Explain Java Environment

Java environment includes a large number of development tools and hundreds of classes and
methods. The development tools are part of the system known as Java Development Kit (JDK)
and the classes and methods are part of the Java Standard Library (JSL), also known as the
Application Programming Interface (API).

 Java Development Kit


The Java Development Kit comes with a collection of tools that are used for developing and
running Java programs. They include:
 Applet viewer ( for viewing Java applets )

Riddhi Mehta(ICT Department) Page 5


Unit -1 Introduction To Java

 Javac ( Java compiler )


 Java ( Java interpreter )
 Javap (Java disassemble )
 Javah ( for C header files )
 Javadoc ( for creating HTML documents )
 Jdb ( Java debugger )

Que. Write the full form of following

1. JDK -> Java Development Kit


2. JVM -> Java Virtual Machine
3. JRE -> Java Runtime Environment
4. JSP -> Java Server Page
5. J2SE -> Java 2 Platform Standard Edition
6. JSDK -> Java Served Development Kit
7. JIT -> Just in time compiler
8. JDBC -> Java database connectivity
9. AWT -> Abstract Window Toolkit
10. API -> Application Programming Interface

Que. What is WWW? What is contribution of java to www?

WWW means World Wide Web.


WWW is a repository of information linked together from points all over the world.
The www has a unique combination of flexibility, portability and user friendly future that
distinguish it from other services provided by the internet.
Applets are small Java programs that are primarily used in Internet computing. They can
transport over the internet from one computer to another and run using the Applet Viewer or any
web browser that supports Java.
An applet, like any application program, can do many things for us. It can perform arithmetic
operations, display graphics, play sounds, accepts user input, create animation and play
interactive games.
Java has revolutionized the way the Internet users retrieve and use documents on the world wide
network.
Java has enabled them to create and use fully interactive multimedia Web documents.
A web page can now contain not only a simple text or a static image but also a Java applet
which, when run, can produce graphics, sounds and moving images. Java applets therefore have
begun to make a significant impact on the World Wide Web.

Que. Why java is important to the internet?


Java is strongly associated with the Internet because of the fact that the first application program
written in Java was HotJava, a Web browser to run applets on Internet.
Internet users can use Java to create applet programs and run them locally using a “Java –enable
browser” such as HotJava.

Riddhi Mehta(ICT Department) Page 6


Unit -1 Introduction To Java

They can also use a Java-enabled browser to download an applet on a computer anywhere in the
Internet and run it on his local computer (see fig.)
In fact, Java applets have made the Internet a true extension of the storage system of the local
computer.

Internet users can also set up their Web sites containing Java applets that could be used by other
remote users of Internet.
The ability to hitch a ride on the Information Superhighway has made Java a unique
programming language for the Internet. In fact, due to this, Java is popularly known as Internet
language.

Que. Explain the structure of Java Program with example.

Documentation Section Suggested


Package Statement Optional
Import statements Optional
Interface statements Optional
Class Definition Optional
Main Method Class
{
Essential
Main Method Definition
}

 Documentation Section
The documentation section comprises a set of comment lines giving the name of the
program, the author and other details, which the programmer would like to refer to at a later
stage. We use comment for documentation.

 Package Statement

Riddhi Mehta(ICT Department) Page 7


Unit -1 Introduction To Java

The first statement allowed in a Java file is a package statement. This statement declares a
package name and informs the compiler that the classes defined here belong to this package.
The package statement is optional. That is, our classes do not have to be part of a package.

 Import Statements
The next thing after a package statement (but before any class definitions) may be a number
of import statements. This is similar to the# include statement in c.

 Interface Statement
An interface is like a class but includes a group of method declarations. It is new concept in
java. This is also an optional section and is used only when we wish to implement the
multiple inheritance features in the program.

 Class Definitions
A Java program may contain multiple class definitions. Classes are the primary and essential
elements of a Java program. These classes are used to map the objects of real-world
problems. The number of classes used depends on the complexity of the problem.

 Main Method Class


Since every Java stand –alone program requires a main method as its starting point, this
class is the essential part of a Java program.

Example:

// First.java

class First
{
public static void main ( String arg [ ] )
{
System.out.println (“Simple Java Program”);
}
}

Que. Explain the task of main () method in Java Programming Language.


OR
Que. Explain the public, static, void keyword.

Public: main ( ) is the first method called by java environment when a program is executed so it
has to accessible from java environment. Hence the access specifies has to be public.

Static: Java environment should be able to call this method without creating an instance of the
class, so this method must be declared as static.

Void: main function does not return anything so the return type must be void.

Riddhi Mehta(ICT Department) Page 8


Unit -1 Introduction To Java

The argument String indicates the argument type which is given at the command line and args is
an array for string given during command line.

Que. Why pointers are not used in java?

If a language supports pointers, then it gives the programmer the ability to reference any part of
the program's memory. This means that a program can reference variables, and read and write
them freely, by passing any kind of type safety or memory management.
Some kinds of programming need the ultimate flexibility of pointer. For example, low-level
device drivers need to access memory directly. Most programs do not need that much power,
and the risks imposed by pointers are very serious.

In Java all inside the class which not essentially requires pointer mechanism and by using
pointers we directly access the memory of the system, which cause some security problems and
JAVA doesn't allow it because everything should be under JVM. Another Fact is pointers are not
platform independent

Que. What is Operator? Explain different types of operators.


Definition: Operator is a symbol that tells the computer to perform certain mathematical or
logical manipulations. Operators are used in programs to manipulate data and variables.
Java Operators can be classified as:
1) Arithmetic Operator
2) Relational Operator
3) Logical Operator
4) Assignment Operator
5) Increment and Decrement Operator
6) Conditional Operator
7) Bitwise Operator
8) Special Operator
1. Arithmetic Operators
The four operators ‘+’, ‘-’, ‘*’ and ‘/’ are known as arithmetic operators. They are used to
perform simple arithmetic calculation in programming as in mathematics. Certain operators
require only one operand (Example- ‘+, - ’) and are hence known as Unary operator. Other
operators that are used with two operands are known as Binary Operator (‘*, / , +, -’).

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo
Division(Remainder)
Example:
class Arith{
public static void main(String args[]) {
float no1, no2;

Riddhi Mehta(ICT Department) Page 9


Unit -1 Introduction To Java

float sum, product, difference;


float quotient;

no1=17;
no2=02;

sum = no1 + no2;


product = no1 * no2;
quotient = no1 / no2;
difference = no1 - no2;

System.out.println("Two Numbers are "+no1+" "+no2);


System.out.println("Sum is " +sum);
System.out.println("Product is "+product);
System.out.println("Quotient is "+quotient);
System.out.println("Difference is "+difference);
}
}

2. Relational Operators
As the name clear illustrates the meaning, the relational operators show the relations between
the two variables.
Relational Operators are –

Operator Meaning
> Greater Than
< Less Than
>= Greater Than Equal To
<= Less Than Equal To
== Equal To
!= Not Equal To

Example:

int a=10, b=12 and c=10;

Operation Meaning Output

a<b True
a less than b?

a <= b True
a less than or equal to b?

a>b False
a greater than b?

Riddhi Mehta(ICT Department) Page 10


Unit -1 Introduction To Java

Operation Meaning Output

a<b True
a less than b?

a >= b False
a greater than or equal to b?

a==c True
a equal to c

a!=b True
a not equal to b

3. Logical Operator
Logical operators are generally used with conditional statements and are used to combine
two conditions.
The three Logical Operators---

If both the conditions are true then only &&


&& Logical AND
block is successfully executed
Even if one condition out of the many is true
|| Logical OR
then also the block is successfully executed.
If the condition is wrong then only the
! Logical NOT
statements inside the block are executed.

Riddhi Mehta(ICT Department) Page 11


Unit -1 Introduction To Java

Example:

if(a= =1 && b==2)

Op-1 Op-2 Op-1 && Op-2 Op-1 | | Op-2 Output

True True True True True

True False False True True

False True False True False

False False False False False

4. Assignment Operator
A compound assignment operator has the following syntax:
<variable> <op> = <expression>
Here <op>= can be any of the compound assignment operators. The compound assignment
operators have the lowest precedence of all the operators in Java, allowing the expression on
the right-hand side to be evaluated before the assignment. The following Table defines the
arithmetic compound assignment operators.

Assignment Operator Shorthand Operator


a = a+1 a += 1
a = a-1 a -= 1
a = a*1 a *= 1
a = a/1 a /= 1
a = a%1 a %= 1

5. Increment and Decrement Operator


Variable increment (++) and decrement (--) operators come in two flavors: prefix and
postfix. These unary operators have the side effect of changing the value of the arithmetic
operand, which must evaluate to a variable. Depending on the operator used, the variable is
either incremented or decremented by 1.
These operators are very useful for updating variables in loops where only the side effect of
the operator is of interest.

Increment Operator ++

In Prefix increment operator first it will increment the value of variable and then assign.

Riddhi Mehta(ICT Department) Page 12


Unit -1 Introduction To Java

a = 2;

b = ++a; // a=3 and b = 3

In Postfix increment operator first it will assign the value to the variable and then increment.

a = 5;

b = a++; // b = 5 and a=6

Decrement Operator –

In Prefix decrement operator first it will decrement the value of variable and then assign.

a = 2;

b = --a; // a=1 and b = 1

In Postfix decrement it operator first it will assign the value to the variable and then
decrement.

a = 5;

b = a--; // b = 5 and a=4

6. Conditional Operator
The character pair ?: is a ternary operator available in java.

exp1 ? exp2 : exp3 (where exp1,exp2 and exp3 are expressions.)

Expression exp1 is evaluated first. If exp1 is true the exp2 is evaluated and if it is false then
exp3 is evaluated. In this only one of the expression is evaluated not both. It is equivalent to
if else statement.

a = 10, b = 15;
x = (a>b) ? a : b; ( output: x=15)

7. Bitwise Operator
The Bitwise operator when used with any operand works at its individual bits. It does not
work with float and double data type.

Riddhi Mehta(ICT Department) Page 13


Unit -1 Introduction To Java

Operator Meaning Returns true if

bitwise AND a and b are both true, always evaluates a and b


a&b

bitwise OR either a or b is true, always evaluates a


a|b and b

bitwise NOT a is false


!a

bitwise exclusive OR if a and b are different--that is if one or


a^b the other of the operands is true but not both

One’s complement
~a

Shift left shift bits of a right by distance b


a << b

Shift right shift bits of a left by distance b


a >> b

Shift right with zero fill shift bits of a right by distance b (unsigned)
a >>> b

8. Special Operator
Java support some special operators such as instanceof operator and member selection
operator (.).

Instanceof operator
The instanceof is an object reference operator and returns true if the object on left hand side
is an instance of the class given on the right hand side.

Example: person instanceof Student


Is true if the object person belongs to the class Student; otherwise it is false.

Dot operator
The Dot operator (.) is used to access the instance variables and methods of class objects.
person . age // reference to the variable age
person . salary() // reference to the method salary()
It is also used to access classes and sub packages from a package.

Que. What is type casting? Why it is required in programming.


The process of converting one datatype to another is called type casting.

Riddhi Mehta(ICT Department) Page 14


Unit -1 Introduction To Java

The situations where there is a need to store a value of one type into a variable of another type.
In such situations, we must cast the values to be stored by preceding it with the type name in
parentheses.

type variable1 = (type) variable2;

Example:

int a = 20;
byte b = (byte) a;
long c = (long) a;

Riddhi Mehta(ICT Department) Page 15


Unit -1 Introduction To Java

Table: Casts that results in No Loss of Information

From To
byte short, char, int, long, float, double
short int, long, float, double
char int, long, float, double
int long, float, double
long float, double
float double

Widening and Narrowing Conversions


For the primitive data types, the value of a lower data type can be converted to a value of a
higher data type without loss of information. This is called a widening primitive conversion.
For example, an int can be directly converted to a double without first having to convert it to
a long and a float.

Converting from a higher data type to a lower data type is called a narrowing primitive
conversion, which can result in loss of information. For example, a double can be converted
to an int with loss of information
Widening and narrowing conversions are also defined for reference types.

Example:
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);
}
}
output:
JAVA LANGUAGE
Conversion of int to byte.
i and b 257 1
Conversion of double to int.
d and i 323.142 323

Riddhi Mehta(ICT Department) Page 16


Unit -1 Introduction To Java

Que. Explain different Java Control Statements with example.


Control statements are used to change the flow of execution. Java’s control statements can be
classified into three categories.

1. Selection Statements (Decision Control Structure)


Selection statement allows the program to choose any one path from different set of paths of
execution based upon the outcome of an expression or the state of a variable.
a. if
b. if else
c. nested if else
d. if-else-if
e. switch

(a) Syntax of if (b) Syntax of if else


if(condition) if(condition)
{ {
statements; }
} else
{
}
(c) Syntax of Nested if else (d) Syntax of if-else-if Ladder
if(condition) if(condition1)
{ {
if (condition) }
{ else if(condition2)
} {
else }
{ else if(condition3)
} {
} }
else else
{ {
if (condition) }
{
}
else
{
}
}
(e) Syntax of switch case
switch(expression)
{
case 1:
statement 1 sequence;
break;
case 2:

Riddhi Mehta(ICT Department) Page 17


Unit -1 Introduction To Java

statement 2 sequence;
braek;
case 3:
statement 3 sequence;
break;
--
--
--
--
case n:
break;
default :
default statement sequence;
}
Example: Program to find max of two numbers (Max.Java)

class Max
{
public static void main(String args[])
{
int a,b,max;
a = 10;
b = 20;
if(a>b)
max = a;
else
max = b;
System.out.println(“Max of two numbers is “ + max); //Output

}
}
Output:
Max of two numbers is 20

2. Iteration Statements (Loop Control Structure)


Looping is a process by which we can repeat a single statement or a group of statements n
number of times.
a. for
b. while
c. do while

(a) Syntax of for loop.


for( initialize; condition; increment/decrement)
{
statements;
}

Riddhi Mehta(ICT Department) Page 18


Unit -1 Introduction To Java

(b) Syntax of while loop.


initialize;
while(condition)
{
statements;
increment/decrement;
}
(c) Syntax of do while loop.
initialize;
do
{
statements;
increment/ decrement;
}while(condition);

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 pass to the next
line of code immediately following the loop.
While and for loop are called entry controlled loop in which condition is checked first before
executing body of the loop. Do while loop is called exit controlled loop in which body of the
loop executed at least once before checking condition.

Example:
class Loop1{
public static void main(String args[]){
int n=5;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++)
System.out.print(j);
System.out.println();
}
}
}
Output:
1
12
123
1234
12345

3. Jump Statements
It allows our program to work in a non-linear fashion.
a. break
b. labeled break
c. continue
d. labeled continue

Riddhi Mehta(ICT Department) Page 19


Unit -1 Introduction To Java

e. return
 The conditional expressions used in the if, for, while and do statements
must be valid boolean expressions i.e. their values should be either true or false. We
cannot use 0 instead of false or a non-zero value instead of true.
 The break statement is used to terminate the current block in which the
break statement is written. The break statement can be used to terminate a block like
selection and iteration statements.
 The labeled break statement is used to terminate the block whose label is
specified in the break statement. Unlike simple break statement, we can terminate any
block. For example, it is possible to terminate the outermost loop from inside a deeply
nested for loop. The break statement can also be used to terminate a simple block (i.e.
the block need not be a loop or switch statement)
 The continue statement causes the loop to be continued with the next
iteration after skipping any statements in between.
 The labeled continue statement specifies the label of the enclosing loop
to continue. The label need not correspond to the closest enclosing loop.
 The return statement is used in the function to return the value to the
calling function. It returns only one value at time.

Jump Statement:-
(a) break
for (int i=1;i<=10;i++)
{
if(i == 5)
break; //terminate loop if i is 5
System.out.print( i );
}
Output: 1234

(b) labled break


boolean t =true;
first : {
second: {
third: {
System.out.println("Before the break");
if(t) break second; //break out of second block
System.out.println("This will not execute");
}
System.out.println("This will not execute");
}
System.out.println("This is after second block");
}
Output: Before the break
This is after second block

Riddhi Mehta(ICT Department) Page 20


Unit -1 Introduction To Java

(c) continue
for(int i=0;i<10;i++)
{
System.out.print( i + “ “);
if ( i % 2 = = 0 )
continue;
System.out.println();
}
Note:- This code use the % operator to check if i is even. if it is even then the loop will
continue without printing a new line.
Output:
0 1
2 3
4 5
6 7
8 9

(d) labeled continue


outer : for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if ( j > i )
{
System.out.println();
continue outer;
}
System.out.print("\t"+ ( i * j ) );
}
}
Output:
0
0 1
0 2 4
0 3 6 9
0 4 8 12 16

(e) return
int sum(int a, int b)
{
int c;
c = a+b;
return(c);
}

Riddhi Mehta(ICT Department) Page 21


Unit -1 Introduction To Java

Riddhi Mehta(ICT Department) Page 22

You might also like