0% found this document useful (0 votes)
5 views26 pages

Java Unit 1

The document outlines a course on Object Oriented Programming using Java, detailing the syllabus divided into five units covering topics such as Java basics, classes, interfaces, exception handling, applet programming, and graphics programming. It includes a blueprint for the question paper pattern, specifying the structure and marks distribution for the examination. Additionally, it provides a list of textbooks and reference materials for further study.

Uploaded by

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

Java Unit 1

The document outlines a course on Object Oriented Programming using Java, detailing the syllabus divided into five units covering topics such as Java basics, classes, interfaces, exception handling, applet programming, and graphics programming. It includes a blueprint for the question paper pattern, specifying the structure and marks distribution for the examination. Additionally, it provides a list of textbooks and reference materials for further study.

Uploaded by

vinod.pq
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BCA504T: OBJECT ORIENTED PROGRAMMING USING JAVA

Total Teaching Hours: 60 No of Hours / Week: 04

Unit - I [12 hours]

Introduction to JAVA: JAVA Evolution: Java History, Java Features, How Java Differs from C and C++,
Java and Internet, Java and World Wide Web, Web Browsers, Hardware and Software
Requirements, Java Support Systems, Java Environment. Overview of JAVA Language: Introduction,
Simple Java program, More of Java Statements, Implementing a Java Program, Java Virtual
Machine, Command Line Arguments, Programming Style. Constants, Variables, and Data Types:
Introduction, Constants, Variables, Data Types, Declaration of Variables, Giving Values to Variables,
Scope of Variables, Symbolic Constants, Type Casting, Getting Values of Variables, Standard Default
Values, Operators and Expressions: Introduction, Arithmetic Operators, Relational Operators
Logical Operators, Assignment Operators, Increment and Decrement Operators, Conditional
Operators, Bitwise Operators, Special Operators, Arithmetic Expressions, Evaluation of Expressions,
Precedence of Arithmetic Operators, Type Conversion and Associativity, Mathematical Functions.
Decision Making and Branching: Introduction, Decision Making with if Statement, Simple if
Statement, The if…..else Statement, Nesting of if………Else Statements, The else if Ladder, The
Switch Statement, The ?: Operator. Decision Making and Looping: Introduction. The while
Statement, The do Statement, Thefor Statement, Jumps in Loops Labeled Loops.

Unit -II [12 Hours]

Classes, Arrays, Strings and Vectors: Classes, Objects and Methods: Introduction, Defining a Class,
Adding Variables, Adding Methods, Creating Objects, Accessing Class Members, Constructors,
Methods Overloading, Static Members, Nesting of Methods, Inheritance: Extending a Class
Overriding Methods, Final Variables and Methods, Finalizer methods, Abstract Methods and
Classes, Visibility Control. Arrays, Strings and Vectors: Arrays, One-dimensional Arrays, Creating an
Array, Two -Dimensional Arrays, Creating an Array, Two – dimensional Arrays, Strings, Vectors,
Wrapper Classes.

Unit - III [12 Hours]

Interfaces, Packages, and Multithreaded Programming: Interfaces: Multiple Inheritance:


Introduction, Defining Interfaces, Extending Interfaces, Implementing Interfaces, Accessing
Interface Variables. Packages: Putting Classes together: Introduction, Java API Packages, Using
System Packages, Naming Conventions, Creating Packages, Accessing a Package, Using a Package,
Adding a Class to a Package, Hiding Classes. Multithreaded Programming: Introduction, Creating
Threads, Extending the Thread Class, Stopping and Blocking a thread, Life Cycle of a thread, Using
Thread Methods, Thread Exceptions, Thread Priority, Synchronization, Implementing the
‘Runnable’ Interface.
Unit – IV [12 Hours]

Managing Exceptions, Applet Programming: Managing Errors and Exception: Introduction, Types of
Exception Handling Code, Multiple Catch Statements, Using Finally Statement, Throwing Our Own
Exceptions, Using Exceptions for Debugging. Applet Programming: Introduction, How Applets Differ
from Applications, Preparing to Write Applets, Building Applet Code, Applet Life Cycle, Creating an
Executable applet, Designing a Web Page, Applet Tag, Adding Applet to HTML File, running the
Applet, More About HTML Tags, Displaying Numerical Values, Getting Input from the User.

Unit – V [12 Hours]

Graphics Programming, Input/Output: Graphics programming: Introduction, The Graphics Class,


Lines and rectangles, circles, and Ellipses, Drawing Arcs, Drawing Polygons, Lines Graphs, Using
Control Loops in Applets, Drawing Bar Charts. Managing Input/Output Files in JAVA: Introduction,
Concept of Streams, Stream Classes, Byte Stream Classes, Character Stream Classes, Using Streams,
Other Useful I/O Classes, Using the File Class, Input / Output Exceptions, Creation of Files, Reading /
Writing Characters, Reading / Writing Bytes, Handling Primitive Data Types, Concatenating and
Buffering Files, Interactive Input and output, Other Stream Classes.

Text Books:

1. [Link], “Programming with JAVA”, A Primer, TMH, 1999.

Reference Books:

1. Thomas Boutel, “CGI programming in C and Perl”, Addison – Wesley, 1996.

2. Jefry Dwight et al, Using CGI, Second Edition, Prentice Hall, India, 1997.

3. Patrick Naughton & Herbert Schildt, JAVA 2: The Complete Reference, THM, 1999.

4. Schildt, “JAVA The Complete Reference”, 7th Edition.


BCA504T: OBJECT ORIENTED PROGRAMMING USING JAVA

BLUE PRINT

Question paper pattern for theory has two sections:

Section – A: Contains 12 questions, out of which a student has to answer 10 questions.

Each question carries 2 marks (10 x 2 = 20)

Section – B: Contains 8 questions, out of which a student has to answer 5 questions. Includes sub-
question as (a) & (b).

Each question carries 10 marks (5 x 10 = 50)

Section – A Section – B
UNIT CHAPTERS
(2 Marks) (10 Marks)
Introduction to java 1 -
Overview of Java Language - 1
I Constants, Variables and Data Types 1 1
Operators and Expressions 1 1
Decision Making and Branching - -
Classes, Objects and Methods 1 1
Inheritance - 1
II
Arrays, strings and Vectors 1 1
Wrapper Class - -
Interfaces 1 1
III Packages 1 1
Multi-Threaded Programming 1 2
Managing Exceptions 1 1
IV
Applet Programming 1 2
Graphics Programming 1 2
V
Managing Input/output Files in JAVA 1 1
TOTAL 12 16
Answer any 10 Answer any 5
TOTAL MARKS 20 50
UNIT-I

SECTION A (2 Marks Questions)


1. What do you mean by command line argument?
Command line arguments are the parameters/arguments that are supplied at the time of
running the java program.
2. What are the two ways of giving values to the variable?
The two ways of giving values to the variable are:
 By using an assignment statement.
Ex: int a=10;
 By using a “BufferedReader” class which is a part of ‘io’ Sub package
Ex : InputStreamReader inp = new Inputstream([Link]);
BufferedReader br = new BufferedReader(inp);
3. Write the default values of primitive data types?

Data Type Default Value Default size

Byte 0 1 byte

Short 0 2 byte

Int 0 4 byte

Long 0L 8 byte

Float 0.0f 4 byte

double 0.0d 8 byte

Char '\u0000' 2 byte

Boolean False 1 bit

4. Why java is simple? Mention any two reasons?


 Its coding style is very clean and easy to write, compile & debug.
 It doesn’t use complex and difficult features of other languages like C and C++ are:
o Preprocessors and header files
o Pointers
o Storage classes
o Multiple Inheritance
 It is object oriented & platform independent.
5. What are string literals?
Sequence of characters enclosed within/inside a pair of double quotes.
Ex: String str1 = “Vinod”;

6. Difference between String literals and String objects.


STRING LITERAL STRING OBJECT
Sequence of characters enclosed within/ Sequence of characters that is created using
inside a pair of double quotes. the new() operator.
If the string already exists, the new Even the string already exists or not, a new
reference variable will be pointing to the String object will be created.
already existing literal
String s = “Vinod”; String S = new String(“Vinod”);

7. Give the general form of ‘switch’ statement?


The Java switch statement executes one statement from multiple conditions.
Syntax: switch(expression)
{
case value1:
Statement 1:
break;
case value2:
Statement 2;
break;

default:

8. What is byte code? Justify how java is platform independent?


 Byte code is an intermediate code generated by the compiler (Javac) after the
successful compilation of the source file(Java program).
 Byte code also called as Class file.
 Byte code are the machine level language of the JVM.

Java is platform Independent because java compiler converts source code into byte code
and it can be executed on any platform(OS) using JVM.
9. What is ‘labelled break’ and ‘labelled continue’?
 Labelled break: the break statement breaks out the closest loop or switch
Statement.

 Labelled Continue: the continue statement transfers the control to the closest
enclosing loop.
10. Mention the data types in java?
Data types: Data types specifies which type of data/ value a variable can store.
1. Primitive Data types: it is a built-in data type specifies the size and type of variable
values.
2. Non-primitive Data types: These data types are not predefined in java.

11. What are important elements of internet architecture?


• ISP
• Router
• Internet
• Backbone
• TCP/IP
• Network access points or NAPs
• Domain name service (DNS) and DNS Servers
12. What is difference between JDK, JRE and JVM?

Parameter JDK JRE JVM


Full-Form Java Development Kit. Java Runtime Java Virtual Machine.
Environment.
Definition It is a software It is a type of software It is a platform
development kit used to package that provides dependent virtual
develop java applications. class libraries of Java, machine that has
Along with JRE, the JDK also JVM, and various other three notions in the
consists of development components for running form of specifications.
tools like Java Debugger, the applications written This document
JavaDoc, compilers, etc.) in Java programming. describes the
requirement of JVM
implementation.
Functionality JDK is primarily used for JRE is responsible for JVM specifies all the
code execution & providing an implementations. And
development. environment for the responsible for
execution of code. providing these
implementations to
JRE.
Platform The JDK is platform- Just like JDK JRE is also The JVM is platform-
Dependency dependent. It means that Platform Dependent. . independent.
for every different platform,
you require a different JDK.
Tools JDK is responsible for the It contains various JVM does not include
development, it consists of supporting files for JVM, software development
various tools for debugging, and the class libraries tools.
monitoring, and developing that help JVM in running
java applications. the program.
Implementation JDK = Development Tools + JRE = Java Virtual JVM = Only the
JRE (Java Runtime Machine) + Libraries to runtime environment
Environment) run the application. for executing the Java
bytecode.
13. What are the different types of comments in Java?
It is a statements in program that are not executed by compiler or interpreter.
 Single Line: Used to comment only one line of the code. (//)
Ex: //this is single line comment.
 Multi line: Used to comment multiple lines of code. ( /* ………*/ )
Ex: /*
This
Is
Multi line
Comment
*/
 Documentation: Used to write large programs for a project or Application.
Ex: /**
*
* heading 1
* Heading 2
*/

14. What are the reserved literals in Java?


 Reserved Literals are reserved words which only represents value.
 Java has 3 reserved literals
Ex: true, false, null

15. What is [Link], [Link] and [Link]?


Or
Explain 3 streams in java.

Stream: Stream is a sequence of data and it is composed of bytes.

 [Link]: It is the input stream used to read input from the keyboard or user.
Ex: InputStreamReader inp = new InputStreamReader([Link]);
 [Link]: It is the output stream means to print the output on the console.
Ex: [Link](“Hello world”);
 [Link]: It works same as the [Link] used to output error texts.
Ex: [Link](“Invalid Output”);
SECTION B ( 5 Marks Questions)

1. Explain the features of java?


 Simple: Easy to learn. Because java inherits the c/c++ syntax and many of the object
oriented features of c++.
 Secure: Java provides a firewall between a networked application and user
computer.
 Portable: Translating a java program into bytecode makes it much easier to run a
program in a wide variety of environments.
 Object oriented: Java enhances and refines the object oriented paradigm used by
C++. The object model in java is simple and easy to extend.
 Robust: Java eliminates memory management problems by managing memory
allocation and deallocation.
 Multithreaded: Java supports multithreaded programming which allows you to
write programs that do many things simultaneously.
 Dynamic: Java programs have run time information that is used to verify and resolve
accesses to objects at run time.
 Distributed: Java is designed for the distributed environment of the internet,
because it handles TCP/IP protocols.
 Interpreted and Performance: Java bytecode can be interpreted on any system that
provides a high JVM. Java is well designed to perform well on very low power CPUs.
 Platform Independent/Architecture Neutral: Java programs can be executed on any
processors like Pentium, Celeron, dual core, AMD and so on, hence it is called
architecture Neutral.

2. Write a note on scope of variables?


 Scope of variable defines how a variable is declared and defined inside a class, method or
block. i.e. The visibility or accessibility of a variable.
 In java, there are three types of variables based on their scope:
1. Member Variables (Class level scope)
 These variables must be declared within/inside a class so that all the methods
inside a class can access these variables.
 Also called as instance variables.
 We can declare instance variables anywhere in the class, but outside methods.
Ex: public class Demo
{
Int a; //member variable
Private string str; //member variable
Void method1()
{
…….
}
}
2. Local Variables (Method level Scope)
 These Variables are declared within/inside a method.
 These variables can’t be accessed outside the method.
Ex: public class Demo
{
Void method1()
{
Int x ; //local variable
}
}
3. Loop Variables (Block Level Scope)
 Any variables declared in a for loop condition is not accessible after the loop.
Ex: public class test
{
Public static void main(Strings args[])
{
for( int i = 0 ; i < 4 ; i++)
{
[Link](“welcome”);
}
}
}

3. Write a program to display all prime numbers between two limits using command line
Argument.
public class PrimeNum
{
public static void main (String[] args)
{
int lower = 1, upper = 20;
for (int i = lower; i <= upper; i++)
if (isPrime (i))
[Link] (i);
}
static boolean isPrime (int n)
{
int count = 0;
// 0, 1 negative numbers are not prime
if (n < 2)
return false;

// checking the number of divisors b/w 1 and the number n-1


for (int i = 2; i < n; i++)
{
if (n % i == 0)
return false;
}

// if reached here then must be true


return true;
}
}
4. Explain the line “public static void main(String args*+ )”.

 Main() method: It is name of the method, it is the entry point for JVM to start
execution of java program.
 Public: public keyword is an access modifier, main() must be declared as public so
that JVM can access main() method and execute outside the class.
 Static: It is a keyword, if main() method is static, so that it can be accessed and
executed without creating the object of a Class.
 Void: It is a keyword used to define the return type of the main() method. Means
method will not return any value..
 String[] args: args is name of the string array, it is not fixed you can still change the
name from args to anything. Which is used to pass the command line argument to
the main method.
Different ways of writing main method.
 public static void main(String[] args)
 public static void main(String args[])
 public static void main(String jbt[])
 public static void main(String… args)
5. Explain the history and evolution of java.
1. James Gosling, Mike and Patrick initiated the Java language project in June 1991. The
small team of sun engineers called Green Team.
2. Initially it was designed for small, embedded systems in electronic appliances like set-
top boxes, interactive television.
3. Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt.
4. After that, it was called Oak and was developed as a part of the Green project.
5. The team gathered to choose a new name. The suggested words were "dynamic",
"revolutionary", "Silk", "jolt", "DNA", etc. They wanted something that revolutionary,
dynamic, cool, unique, and easy to spell, and fun to say.
6. According to James Gosling, "Java was one of the top choices along with Silk".
7. Java is an island in Indonesia where the first coffee was produced (called Java coffee),
java name was chosen while having a coffee nearby his office.
8. In 1995, Oak was renamed as "Java" because it was already a trademark by Oak
Technologies.
9. Initially developed by James Gosling at Sun Microsystems (which is now a subsidiary of
Oracle Corporation) and released in 1995.
10. In 1995, Time magazine called Java one of the Ten Best Products of 1995.
11. JDK 1.0 was released on January 23, 1996. After the first release of Java.

6. What are static variables and static methods?


Static variable:
 Static variables are special type of variables that are not associated with an object,
they are associated with class.
 Static variables are also called as class variables.
 Static variables are initialized only once, at the start of the execution.
 The static variables can be accessed without an object.
 A static variable can be accessed directly by the class name and does not need any
object.
 Example: class staticdemo
{
int x,y;
static int z;
[Link](staticdemo.z);
}
Static Method:

 Static method is a method which belongs to the class and not to the object.
 The static methods are also called as class members.
 A static method can access only static data.
 The most common example of a static member is main(). The main() is declared as
static because it must be called by the operating system when our program begins.
 Static methods can be accessed directly by the class name and does not need any
object.
Example:
staticdemo.method1();

Declaration:
class staticdemo
{
int x,y;
static int z;
}

7. Explain bitwise operators.


 Bitwise operator works on bits and performs bit by bit operations.
 The operations on the bits are performed on 1s and 0s only.

Operators Symbol Uses

Bitwise AND & op1 & op2

Bitwise exclusive OR (XOR) ^ op1 ^ op2

Bitwise inclusive OR | op1 | op2

Bitwise Compliment ~ ~ op

Bitwise left shift << op1 << op2

Bitwise right shift >> op1 >> op2

Unsigned Right Shift Operator >>> op >>> number of places to shift


Bitwise AND (&)

It is a binary operator denoted by the symbol &. It returns 1 if and only if both bits are 1, else
returns 0.

Bitwise exclusive OR (^)

It is a binary operator denoted by the symbol ^ (pronounced as caret). It returns 0 if both bits are
the same, else returns 1.

Bitwise inclusive OR (|)

It is a binary operator denoted by the symbol | (pronounced as a pipe). It returns 1 if either of the
bit is 1, else returns 0.

Bitwise Complement (~)

It is a unary operator denoted by the symbol ~ (pronounced as the tilde). It returns the inverse or
complement of the bit. It makes every 0 a 1 and every 1 a 0.
Bit Shift Operators

Shift operator is used in shifting the bits either right or left. We can use shift operators if we divide
or multiply any number by 2.

The general format to shift the bit is as follows:

variable << or >> number of places to shift;

For example, if a=10

a>>2; //shifts two bits

a>>4; //shifts 4 bits

Java provides the following types of shift operators:

 Signed Right Shift Operator or Bitwise Right Shift Operator


 Unsigned Right Shift Operator
 Signed Left Shift Operator or Bitwise Left Shift Operator

8. What is the role of JVM and explain its components.


 Java has two processes, compilation, and interpretation.
 The Java Compiler converts the code present in Java into byte codes.
 The Java Virtual Machine (JVM) converts the byte codes into machine code which
the machine executes directly.
 The JVM provides the environment to execute Java code.
 It interprets the bytecode and converts that into machine code so the machine
could run the Java program.

Components of JVM

Class Loader: Class loader is a subsystem of JVM. It is used to load class files. Whenever
we run the java program, class loader loads it first.

Class method area: It is one of the Data Area in JVM, in which Class data will be stored.
Static Variables,Static Methods, Instance Methods are stored in this area.
Heap: A heap is created when the JVM starts up. It may increase or decrease in size
while the application runs.

Stack: JVM stack is known as a thread stack. It is a data area in the JVM memory which
is created for a single execution thread.

Native stack: It subsumes all the native methods used in your application.

Execution Engine:

 JIT compiler
 Garbage collector

JIT compiler: The Just-In-Time (JIT) compiler is a part of the runtime environment. It helps
in improving the performance of Java applications by compiling bytecodes to machine code
at run time.

Garbage collector: As the name explains that Garbage Collector means to collect the
unused material.

Garbage collector works in two simple steps known as Mark and Sweep:

Mark – it identifies which piece of memory is in use and which are not
Sweep – it removes objects identified during the “mark” phase

9. How to define constants in Java?


 Constant is a value that cannot be changed after assigning it.
 Java does not directly support the constants.
 We define the constants in Java by using static and final keyword.
 The syntax to declare a constant is as follows:
static final datatype identifier_name=value;
 For example, price is a variable that we want to make constant.
static final double PRICE=432.78;

10. Explain operators in Java.


Operator in Java is a symbol that is used to perform specific operations.
Types of operators in Java:
 Arithmetic Operator
 Relational operator
 Logical operator
 Increment and decrement operator
 Conditional operator
 Assignment operator
 Bitwise operator
 Special operator
Arithmetic Operators

Java arithmetic operators are used to perform addition, subtraction, multiplication, and division.
They act as basic mathematical operations.

Example:

public class OperatorExample


{
public static void main(String args[])
{
int a=10;
int b=5;
[Link](a+b); //15
[Link](a-b); //5
[Link](a*b); //50
[Link](a/b); //2
[Link](a%b); //0
}
}

Relational operator: it is used to compare two values or variables.

Operator Name Example

== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y


>= Greater than or equal x >= y
to

<= Less than or equal to x <= y

Logical operators: Logical operators are used to determine the logic between variables or values

Operator Name Description Example

&& Logical Returns true if both statements are x < 5 && x < 10
AND true

|| Logical Returns true if one of the x < 5 || x < 4


OR statements is true

! Logical Reverse the result, returns false if !(x < 5 && x < 10)
NOT the result is true

Increment and Decrement operator: used to increase or decrease value of a variable by 1

Operator Name Description Example

++ Increment Increases the value of a variable by 1 ++x

-- Decrement Decreases the value of a variable by 1 --x


Conditional Operator: also called as ternary operator, This operator consists of three operands and
is used to evaluate Boolean expressions.
Syntax:
variable x = (expression) ? value if true : value if false
Example: public class Test {

public static void main(String args[]) {


int a, b;
a = 10;
b = (a == 1) ? 20: 30;
[Link]( "Value of b is : " + b );

b = (a == 10) ? 20: 30;


[Link]( "Value of b is : " + b );
}
}

Assignment operator:
 Assignment operators are used to assign values to variables.
 we use the assignment operator (=) to assign the value to a variable.

A list of all assignment operators:

Operator Example Same As

= x=5 x=5

+= 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

|= x |= 3 x=x|3

^= x ^= 3 x=x^3

>>= x >>= 3 x = x >> 3

<<= x <<= 3 x = x << 3

11. Explain Java tokens.


Java tokens are smallest elements of a program which are identified by the compiler.
Java token includes the following:
 Keywords
 Identifiers
 Literals
 Operators
 Separators
 Comments
Keyword: Keywords in Java are predefined or reserved words that have special meaning to the Java
compiler. Java supports 50 keywords.

01. abstract
02. boolean
03. byte
04. Break
05. Class
06. Float
07. For

Identifier: Identifiers are used to name a variable, constant, function, class, and array. It usually
defined by the user.

There are some rules to declare identifiers are:

 Identifiers must begin with a letter, dollar sign or underscore.


 It cannot start with digits but may contain digits.
 Identifiers in Java are case sensitive.
 Identifier name cannot contain white spaces.
 keywords can’t be used as identifiers

Example:

PhoneNumber
PRICE
radius
a
a1
_phonenumber
$circumference
jagged_array
12radius //invalid

Literals:
 literal is a notation that represents a fixed value (constant) in the source code.
 Once it has been defined cannot be changed
 Java provides five types of literals are as follows:
 Integer
 Floating Point
 Character
 String
 Boolean
Literal Type

23 int

9.86 double

false, true Boolean

'K', '7', '-' char

"javatpoint" String

null any reference type

Operators:
 Operators are the special symbol that tells the compiler to perform a special operation.
 There are eight types of operators in Java, are as follows:
 Arithmetic Operators
 Assignment Operators
 Relational Operators
 Unary Operators
 Logical Operators
 Ternary Operators
 Bitwise Operators
 Shift Operators

Operator Symbols

Arithmetic +,-,/,*,%

Unary ++ , - - , !

Assignment = , += , -= , *= , /= , %= , ^=

Relational ==, != , < , >, <= , >=

Logical && , ||

Ternary (Condition) ? (Statement1) : (Statement2);

Bitwise &,|,^,~
Shift << , >> , >>>

Separators:
 The separators in Java are also known as punctuators.
 There are nine separators in Java, are as follows:
 separator <= ; | , | . | ( | ) | { | } | [ | ]
 Square Brackets []: It is used to define array elements. A pair of square brackets represents
the single-dimensional array, two pairs of square brackets represent the two-dimensional
array.
 Parentheses (): It is used to call the functions and parsing the parameters.
 Curly Braces {}: The curly braces denote the starting and ending of a code block.
 Comma (,): It is used to separate two values, statements, and parameters.
 Assignment Operator (=): It is used to assign a variable and constant.
 Semicolon (;): It is the symbol that can be found at end of the statements. It separates the
two statements.
 Period (.): It separates the package name form the sub-packages and class. It also separates
a variable or method from a reference variable.

Comments:
 Comments allow us to specify information about the program inside our Java code.
 Java compiler recognizes these comments as tokens but excludes it form further processing.
 The Java compiler treats comments as whitespaces.

12. Explain the iterative statements in Java?


Or

Explain the looping statements in java.

 Looping/iterative statements: Used to execute block of statements repeatedly until


certain condition is reached.
 3 types of iteration statements.
 for loop
 while loop
 do-while loop

for loop: Used to execute block of statements repeatedly until certain condition is reached.

Syntax:

for(initialization; condition; increment/decrement)


{
//statement or code to be executed
}
Example:

public class ForExample


{
public static void main(String[] args)
{
for(int i=1;i<=10;i++)
{
[Link](i);
}
}
}

While Loop: While loop first check the condition and if the condition is true then it execute block of
statements and continue to execute block of statement as long as condition is true.
Syntax:
while(condition)
{
//Block Of Statement
}
Example
public class demo
{
public static void main(String[] args)
{
int i = 0;
while(i<10)
{
[Link](i);
i++;
}
}
}
Do-while loop:
 The Java do-while loop is used to iterate a part of the program repeatedly, until the
specified condition is true.
 Java do-while loop is called an exit control loop.

Syntax:
do{
/ / loop body
//update statement
}while (condition);

Example
public class DoWhileExample
{
public static void main(String[] args)
{
int i=1;
do
{
[Link](i);
i++;
}while(i<=10);
}
}

You might also like