Академический Документы
Профессиональный Документы
Культура Документы
1st Unit
Mr. R. M. Patil
SY CO, JPR
2019-2020
1.1
Features
of
JAVA
or
JAVA
buzzwords
1. Simple
Java is very easy to learn, and its syntax is simple, clean
and easy to understand.
No explicit pointer
8. High Performance
Java is faster than other traditional interpreted programming
languages because Java bytecode is "close" to native code.
It consists of :
- javac : JAVA Compiler
- java : JAVA interpreter
- javadoc : Creating HTML Documents
- javah : Produces Header Files
- jdb : JAVA Debugger
-appletviewer : Execute Applet
- javap : Convert byte code to program description
JAVA Programming Environment : 1. JDK
Text Editor
Commenting Source Program
javac Compilation
Java class
javah Header Files
file
Debugging
java jdb
Execution
Java Prog.
Output Fig. Execution Process of application program
JAVA Programming Environment : 2. JVM
The Java Virtual Machine (JVM) is a software and
program components.
JAVA Programming Environment : 2. JVM – How Works?
Comparison Index C++ Java
Platform-
C++ is platform-dependent. Java is platform-independent.
independent
• Keywords
• Identifiers
• Constants
• Special Symbols/Separators
• Operators
JAVA Tokens : 1. Keywords
2) Instance Variable
- A variable declared inside the class but outside the body of the
method, is called instance variable.
3) Static variable
- A variable which is declared as static is called static variable.
- You can create a single copy of static variable and share among all
the instances of the class.
- Memory allocation for static variable happens only once when the
class is loaded in the memory.
Example to understand the types of variables in java
class A
{
int data=50; //instance variable
static int m=100; //static variable
void method()
{
int n=90; //local variable
}
} //end of class
Dynamic Initialization in JAVA
System.out.println(a); // Outputs 9
System.out.println(b); // Outputs 9.0
}
}
Type Casting in JAVA – 2. Narrowing Casting
OR
type[] var-name;
Array in JAVA
• String[] cars;
• String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
• int[] myNum = {10, 20, 30, 40};
System.out.println(cars[0]);
// Outputs Volvo
Change an Array Element
Array Length
You can loop through the array elements with the for
loop, and use the length property to specify how many
times the loop should run.
Example:
1. * : Multiplication
2. / : Division
3. % : Modulo
4. + : Addition
5. – : Subtraction
Arithmetic Operators
class OperatorExample
{
public static void main(String args[])
{
int a=10;
int b=5;
System.out.println(a+b); //15
System.out.println(a-b); //5
System.out.println(a*b); //50
System.out.println(a/b); //2
System.out.println(a%b); //0
}
}
Unary Operators
Unary operators need only one operand. They are used to increment,
decrement or negate a value.
1. – :Unary minus, used for negating the values.
2. + :Unary plus, used for giving positive values. Only used when
deliberately converting a negative value to positive.
3. ++ :Increment operator, used for incrementing the value by 1.
There are two varieties of increment operator.
Post-Increment : Value is first used for computing the result and
then incremented.
Pre-Increment : Value is incremented first and then result is
computed.
4. — : Decrement operator, used for decrementing the value by 1.
There are two varieties of decrement operator.
Post-decrement : Value is first used for computing the result and
then decremented.
Pre-Decrement : Value is decremented first and then result is
computed.
Assignment Operators
‘=’ Assignment operator is used to assign a value to any variable.
It has a right to left associativity,
• In many cases assignment operator can be combined with other
operators to build a shorter version of statement called Compound
Statement. For example, instead of a = a+5, we can write a += 5.
+=, for adding left operand with right operand and then assigning it to
variable on the left.
-=, for subtracting left operand with right operand and then assigning
it to variable on the left.
*=, for multiplying left operand with right operand and then assigning
it to variable on the left.
/=, for dividing left operand with right operand and then assigning it
to variable on the left.
%=, for assigning modulo of left operand with right operand and then
assigning it to variable on the left.
Relational Operators
Relational operators are used to compare two values:
class OperatorExample
{
public static void main(String args[])
{
int a=2;
int b=5;
int min = (a<b)?a:b;
System.out.println(min);
}
}
Bitwise Operators
These operators are used to perform manipulation of
individual bits of a number.
General format-
For example :
c=a+b*c //Expression with arithmetic operators
Math.function_name()
Example :
double y = Math.sqrt(x);
1.4 Mathematical Functions
Functions Actions
sin(x) Returns the sign of the angle x in radius
cos(x) Returns the cosine of the angle x in radius
tan(x) Returns the tangent of the angle x in radius
pow(x,y) Returns x raised to y (xy )
exp(x) Returns e raised to x (ex )
log(x) Returns the log of x
sqrt(x) Returns the square root of x
round(x) Returns the integer closest to the argument
abs(a) Returns the absolute value of a
max(a,b) Returns the maximum of a and b
min(a,b) Returns the minimum of a and b
1.4 Mathematical Functions
x = Math.max(22,33);
x = Math.sqrt(64);
x = Math.abs(-55);
x = Math.pow(16,3);
x = Math.min(22,33);
x = Math.round(4567.9874f);
x = Math.exp(5.98);
MSBTE Que.
1. Describe any 2 relational operators and any 2 logical
operators in java with simple example. –
(S-16,Marks: 4)
Syntax :
if (condition)
{
// block of statements which will be executed if
condition is true.
}
1. If Statement
Following is the flowchart of simple if statement -
1. If Statement
class SimpleIf
{
public static void main(String args[])
{
int i=3;
if (condition)
{
//body of parent if
if (condition)
{
//body of nested if
}
}
3. Nested If…else Statement
4. Else…if Ladder
Else … if ladder is a decision-making statement which
is used to test sequence of test conditions.
else if(condition 2) }
{ else
{
//second block of statements
//Default block of statement
}
}
4. Else…if Ladder
5. Switch Statement switch(variable)
{
Switch case statement case constant 1:
works same as else … if //body of case 1
break;
ladder but it checks only case constant 2:
one variable. //body of case 2
break;
case constant 3:
Else … if ladder can
//body of case 3
check multiple break;
variables. case constant n:
//body of case n
break;
In switch single default:
variable is checked for //body of default
multiple values. block
}
switch(variable 1) 6.Nested Switch
{ Statement
case constant1:
//body of case 1
switch(variable 2)
A switch case statement
{ which consists of another
//body of nested switch switch case within its
case constant 1: block is known as nested
//body of case 1 switch case statement.
break;
case constant 2:
//body of case 2 case constant 2:
break; //body of case 2
case constant n: break;
//body of case n case constant n:
break; //body of case n
default: break;
//default block of statement default:
} //body of default block
break;
}
5. Switch Statement – MSBTE Que.
1. In what ways does a switch statement differ from an
if statement? – W16, 4 Marks
Looping Statement
Looping statements allow the programmer to execute
some group of statement repetitively multiple times.
There are two types of loop:
...
//Assignment of counter variable
while(condition)
{
//body of loop
//increment or decrement of variable
}
...
1. While Loop
1. While Loop
class WhileLoop
{
public static void main(String args[])
{
int i=0; // Assignment of counter variable
while(i<10)
{
Syntax:
Syntax:
Syntax:
break;
The Break Statement
The Break Statement
The Break Statement
class breakDemo
{
public static void main(String args[])
{
for (int i=1; i<=10; i++)
{
if (i==5)
{
break;
}
System.out.println(i+ “”);
}
System.out.println(“Loop is terminated using break”);
}
} Output: 1 2 3 4 Loop is terminated using break
The Continue Statement
The continue keyword can be used in any of the loop
control structures.
Syntax:
continue;
The Continue Statement
The Continue Statement
class continueDemo
{
public static void main(String args[])
{
for(int i=0; i<=10; i++)
{
if(i==5)
{
continue;
}
System.out.println(i + “”);
}
}
}
Output: 1 2 3 4 6 7 8 9 10
MSBTE Que.
1. Write a program to check whether an entered number
is prime or not.– (W-15,17, S-16, Marks: 4)
The return Statement
The return statement is used to return from a function.
That means using return statement the control goes back to
the caller function.
return;
The return Statement
class continueDemo
{
public static void main(String args[])
{
int a=10;
System.out.println(“This statement is executed before
execution of return..”)
if (a==10)
return;
System.out.println(“This statement is will not be
executed”)
}
}
MSBTE Que.
1. Write a program to print the following output.–
11111
2222
333
44
5
(W-17, Marks: 4)
break label;
The Labeled Loops
Here, label is an identifier.
When break statement executes, it terminates the
labeled statement, and control of the program jumps to
the statement immediately following the labeled
statement.
The Labeled Loops
2. Labeled Continue:
Syntax:
continue label;
2. Labeled Continue:
The for – Each Version of the for loop
The for – each element is used to access each element of
an array.
Syntax: ….
int [] numbers = {10, 20, 30, 40, 50};
for(declaration : expression) for(int x : numbers )
{ {
// Statements System.out.print( x );
} System.out.print(",");
}
….
MSBTE Que.
1. Write a general syntax of any 2 decision making
statements and also give its example. –(S-16,Marks: 4)
2. Explain break and continue statements with example.
– (S-16, Marks: 4)
3. State syntax and describe working of ‘for-each’
version of for loop with one example. – (W-16, Marks:
4)
4. Illustrate with example the use of switch case
statement. – (W-17, Marks:4)