Com 123 Java (I)
Com 123 Java (I)
(09079447627)
What is Java ?
It is general-purpose, high level, object oriented programming language.
It is developed by James Gosling.
It is developed at Sun Microsystems in 1995
History of Java ?
James Gosling and his team started to work on java for a client's set top box project
in 1991.
The first version of java (java 1.0) is released in 1995.
First name of java is Oak, next goes to Green and finally becomes JAVA.
Java is the name of Coffee Seed.
Features of Java ?
Platform Independent: Java is called platform independent because a java program
can be run on different kind of platform for example Window OS, Linux OS etc.
Object Oriented: Java supports object oriented programming structure so it is called
object oriented.
Security: Java is more secure language as compare to other programming
language.
Flexible: An application developed in java can be modified as per user requirement
so it is called flexible programming language.
Portable: A java program written in one system can be run in any other system. In
simple a java program can be transferred from one system to another.
Multithreading: In java we can perform more than one task simultaneously so it is
called Multithreading.
Simple: Java is very simple and easy to learn.
Application of Java ?
It is used to develop Window Application.
It is used to develop Web Application.
It is used to develop Android/Mobile Application.
It is used to develop Embedded System for example SIM card, Television etc.
It is used to develop Scientific Application for example MATLAB.
It is used to develop Games.
It is used for Database Connectivity.
6. Now in next window there are two sections: Variable Name and Variable
Value. Now in Variable Name put PATH and in Variable Value paste the copied
URL in the Step 1 then click ok.
Now open command prompt window and run java code.
Syntax of Java ?
class Easy
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
1. It is a keyword.
static
2. It can be used with a variable or function or block. we will discuss it soon
1.It is a keyword.
2.It indicates that there is no value is returning by the function.
void
3.If we use any other keyword like int, float, character in place of void then we will
use return keyword.
1.It is the function which is called the entry point of any program.
main() 2.The execution of any program starts from the main function.
3.If in a program there is only one function then it should be main function.
Case sensitivity Java is case sensitive e.g. “Hello” is different from “hello” due to upper/lower cases
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
JAVA VARIABLES
Variable Declaration
int rollNo;
float marks;
char grade;
Here rollNo is a variable of type int, marks is a variable of type float and grade is a
variable of type char.
Variable Initialization
We can initialize a variable at the time of declaration of variable or after declaration.
int rollNo=201;
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
float marks=85.6;
char grade='A';
Here 201 is the value of rollNo, 85.6 is the value of marks and A is the value of
grade. Character value is always written in single quotes.
int rollNo;
float marks;
char grade;
rollNo=201;
marks=85.6;
grade='A';
Types of Variable
Local Variable
Instance Variable
Static Variable
Local Variable
A variable declared inside the body of the method or constructor or block is called
local variable.
Local variable can be used only inside that method/function in which it is declared.
A local variable can be a static variable.
class Easy {
public void add()
{
int x, y=10, z=20;//declaring local variable
x=y+z;
System.out.println("Add="+x);
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
}
public static void main(String[] args)
{
Easy obj=new Easy();
obj.add();
}
}
/*
### Output ###
Add=30
*/
Instance Variable
A variable which is declared inside a class but outside the body of the method or
constructor or block is called instance variable.
Instance variable can be used anywhere in the program.
class Easy
{
//these instance variabe can be used
//inside all the function of this class
int x,y=30,z=20;
public void add()
{
x=y+z;//accessing instance variable
System.out.println("Add="+x);
}
public void sub()
{
x=y-z;//accessing instance variable
System.out.println("Sub="+x);
}
public static void main(String[] args)
{
Easy obj=new Easy();
obj.add();
obj.sub();
}
}
/*
### Output ###
Add=50
Sub=10
*/
Static Variable
A variable which is declared with static keyword, inside a class but outside the body
of the method or constructor or block is called static variable.
Static variable is stored in the static memory.
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
Static variables are created when the program starts and destroyed when the
program stops.
Static variable can be called by class name directly.
class Easy
{
//declaring static variable
static String text="You are a smart guy.";
public static void main(String[] args)
{
System.out.println(Easy.text);
}
}
/*
### Output ###
You are a smart guy.
*/
Example
JAVA CONSTANTS
KEYWORDS IN JAVA
volatile while
JAVA OPERATORS
Operator
It is a special symbol which is used to perform logical or mathematical operation on
data or variable.
Operand
It is a data or variable on which the operation is to be performed.
Types of Operator
⇒Arithmetic Operators
⇒Relational Operators
⇒Logical Operators
⇒Assignment Operators
⇒Bitwise Operators
⇒Increment/Decrement Operators
⇒Conditional Operators
Arithmetic Operators
These refers to all the mathematical operators such as plus, division, etc.
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
class Easy
{
public static void main(String[] args)
{
int a=5,b=3;
System.out.println("Add="+(a+b));
System.out.println("Sub="+(a-b));
System.out.println("Multi="+(a*b));
System.out.println("Div="+(a/b));
//Note:-modulus(%) always holds remainder value
System.out.println("Mod="+(a%b));
}
}
/*
### Output ###
Add=8
Sub=2
Multi=15
Div=1
Mod=2
*/
Relational Operators
Symbol Operation Example
Logical Operators
(x>y)&&(x>z
Here this expression returns true if both conditions are true.
)
(x>y)||(x>z) Here this expression returns true if any one or both conditions are true.
Not operator reverses the state means if the condition is true it returns false
!(x>y)
and if the condition is false it returns true.
class Easy
{
public static void main(String[] args)
{
int a=10,b=60,c=40;
if(a>b&&a>c)
System.out.println("a is greatest");
if(b>a&&b>c)
System.out.println("b is greatest");
if(c>a&&c>b)
System.out.println("c is greatest");
}
}
/*
### Output ###
b is greatest
*/
Assignment Operators
These operators are mainly used in order to assign or apportion values to designated
variables in the program. Below are some of the assignment operators.
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y x=x%y
class Taxy
{
public static void main(String[] args)
{
int x1=5,y1=3;
x1+=y1;//x1=x1+y1
System.out.println(x1);
int x2=5,y2=3;
x2-=y2;//x2=x2-y2
System.out.println(x2);
int x3=5,y3=3;
x3*=y3;//x3=x3*y3
System.out.println(x3);
int x4=5,y4=3;
x4/=y4;//x4=x4/y4
System.out.println(x4);
int x5=5,y5=3;
x5%=y5;//x5=x5%y5
System.out.println(x5);
}
}
/*
### Output ###
8
2
15
1
2
*/
Bitwise Operators
Bitwise operators operate on bits
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
| Bitwise OR x|y
^ X-OR x^y
class Easy
{
public static void main(String[] args)
{//variable declaration
int a=5,b=3,c;
c=a&b;//AND Operation
System.out.println("a&b="+c);
c=a|b;//OR Operation
System.out.println("a|b="+c);
c=a>>2;//Shift right Operation
System.out.println("a>>2="+c);
c=a<<2;//Shift left Operation
System.out.println("a<<2="+c);
c=a^2;//X-OR Operation
System.out.println("a^2="+c);
}
}
/*
### Output ###
a&b=1
a|b=7
a>>2=1
a<<2=20
a^2=7
*/
Increment/Decrement Operators
Symbol Name Function Example
class Easy
{
public static void main(String[] args)
{//variable declaration
int a=5,b=10;
System.out.println(++a);
System.out.println(--b);
}
}
/*
### Output ###
6
9
*/
Conditional Operators
Syntax:
Condition? If condition is true: if condition is false;
1 ? 2 : 3 ;
If the condition is true second part will execute otherwise third part will execute.
class Easy
{
public static void main(String[] args)
{//variable declaration
int a=5,b=10,max;
max=a>b?a:b;
//don't be confused, here + is separator in java
System.out.println("Greater value is "+max);
}
}
Integer Type
Float Type
Boolean
Keyword boolean
Syntax boolean x;
Value True/False
Default False
Character
Keyword char
Syntax char x;
Value 'a','@','9'
Range 0 to 65536
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
COMMENTS IN JAVA
It is used to explain the code to make it more readable.
It is not considered as a part of program, in other words we can say that compiler
ignores the comment.
java comments are statements that are not executed by the compiler.
Multiline comment
Multiline comment is used to comment a block of code.
Multiline comment starts with /* and ends with */.
The text between /* and */ is not executed by the compiler.
class Easy {
public staticvoid main(String[] args)
{
/*
This is multiline comment
Write a program to add
two number and store it
in third number
*/
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
intx,y=10,z=30;
x=y+z;
System.out.println("Add="+x);
}
}
If statement in Java
Syntax: if(condition){
//body of codes
}
Example 1:
class Easy
{
public static void main(String[] args)
{
int x=10;
if(x>5)
{
System.out.println("x is greater than 5");
}
}
}
}
/*
### Output ###
Enter any number
-5
number is negative
*/
Example 1:
class Easy
{
public static void main(String[] args)
{
int a=10,b=20;
if(a>b)//a greater than b(false)
{
System.out.println("a is greater");
}
else
{
System.out.println("b is greater");
}
}
}
/*
### Output ###
b is greater
*/
import java.util.Scanner;
class Easy
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int no;
System.out.println("Enter any number");
no=in.nextInt();
//number exactly devided by 2 is called even
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
if(no%2==0)
System.out.println("number is even");
else
System.out.println("number is odd");
}
}
/*
### Output ###
Enter any number
5
number is odd
*/
Syntax:
In the switch statement a value/number is passed in the place of parameter and the
case from which the parameter is matched is executed.
If no case matched with parameter then default case will execute.
Example 1:
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
class Easy
{
public static void main(String[]args)
{
int day=2;
switch(day)
{
case1:
System.out.println("Monday");
break;
case2:
System.out.println("Tuesday");
break;
case3:
System.out.println("Wednesday");
break;
case4:
System.out.println("Thrusday");
break;
case5:
System.out.println("Friday");
break;
case6:
System.out.println("Saturday");
break;
case7:
System.out.println("Sunday");
break;
default:
System.out.println("No case matched");
}
}
}
Exercise:
Run the above program using all the cases and print out the result.
import java.util.Scanner;
class Easy
{
public static void main(String[]args)
{
Scanner in=new Scanner(System.in);
char ch;
System.out.println("Enter any alphabet");
ch=in.next().charAt(0);
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
switch(ch)
{
case'a':
case'e':
case'i':
case'o':
case'u':
System.out.println("Vowel");
break;
default:
System.out.println("Consonent");
}
}
}
Loop in Java
Loop is a part of control flow statements.
To run the particular block of code continuously until a required condition is fulfilled is
what we call looping.
Loop is used when there is a need to execute a part of program for multiple times.
Example 1:
class Easy
{
public static void main(String[]args)
{
for(inti=1;i<5;i++)
{
System.out.println(i);
}
}
}
/*
### Output ###
1
2
3
4
*/
Explaination:
In the above example initialization part initialize the variable i with 1, condition is i less than 5 and at the
increment place there is an increment by 1 ,so the output will be 1 to 4.
Example 2:
/*
Example 1 can also be written
like the code below
*/
class Easy
{
public static void main(String[]args)
{
int i=1;
for(;i<5;)
{
System.out.println(i);
i++;
}
}
}
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
Example 3:
class Easy
{
public static void main(String[]args)
{
//all parts of for loop are optional
for(;;)
System.out.println("Hello");
}
}
/*
### Output ###
Hello
Hello
Hello
Hello
-----
-----
Infinite time Hello
*/
Syntax:
while(condition)
{
//body
}
It's body will execute until the given condition is evaluated to FALSE..
Example 1:
class Easy
{
public static void main(String[]args)
{
int x=1;
while(x<10)
{
System.out.println(x);
x++;
}
}
}
Explaination:
In the above example the body of while will execute again and again as long as variable x is less than
10.
Example 1:
class Easy
{
public static void main(String[]args)
{
int x=1;
do
{
System.out.println(x);
x++;
}
while(x<10);
}
}
Explanation:
In the above example the body of while will execute again and again as long as variable x is less than
10.
Exercise:
Using the do..while loop, develop a factorial program.
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
Syntax:
for(variableName : ArrayName)
{
//body;
}
int ar[]={10,50,60,80,90};
for(int i=0; i<ar.length; i++)
System.out.print(ar[i]+" ");
}
}
/*
### Output ###
10 50 60 80 90
*/
Break Statement
It is used to transfer the control out of the body of loop.
In other word we can say that it terminates the current loop.
break statements are mostly used with loop(for,while,do while) and switch statement.
/*
### OUTPUT ###
1 2 3 4 5 6 7 8 9 10
*/
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
class Easy
{
public static void main(String[]args)
{
for(int i=1; i<=10; i++)
{
System.out.print(i+" ");
if(i==5)//terminate the loop when i=5
break;
}
}
}
/*
### OUTPUT ###
1 2 3 4 5
*/
Continue Statement
It is used to skip the next statement and continue the loop.
Continue statements are mostly used with loop (for,while,do while).
Example 1:
class Easy
{
public static void main(String[]args)
{
for(int i=1; i<=5; i++)
{
if(i==3)//skip the next statement when i=3
continue;
else
System.out.print(i+" ");
}
}
}
/*
### OUTPUT ###
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
1 2 4 5
*/
Example 2:
class Easy
{
public static void main(String[]args)
{
for(inti=1;i<=5;i++)
{
if(i>=3)//skip the next statement when i>=3
continue;
else
System.out.print(i+" ");
}
}
}
Exercise:
Run the code in example 2 above and produce the results, print and
submit.
Example 3:
class Easy
{
public static void main(String[]args)
{
for(int i=1; i<=5; i++)
{
if(i<=3)//skip the next statement when i<=3
continue;
else
System.out.print(i+" ");
}
}
}
Example 4:
class Easy
{
public static void main(String[]args)
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
{
for(int i=1; i<=5; i++)
{
if(i!=3)//skip the next statement when i!=3
continue;
else
System.out.print(i+" ");
}
}
}
Exercise:
1. Using examples 3 & 4 above; change the boundary to any figure
above 5.
2. Run the program, print out the results and submit
Return Statement
return statement terminates the current function and transfer the control to the calling
function.
we can also use return statement to trasfer value from one function to another.
class Easy
{
int add()
{
int x=10, y=30;
return(x+y);//returning x+y=40
}
public static void main(String[]args)
{
Easy obj=new Easy();
int rs=obj.add();//passing returning value to rs
System.out.println("Add="+rs);
}
}
COM 123 JAVA(1); java programming is sweet Mr. Iorlaha S.K. (09079447627)
ARRAYS IN JAVA
It is a collection of data of same data type.
It is used to store group of data simultaneously.
It can store data of same data type; i.e. an integer array can store only integer
value ,character array can store only character value and so on.
We can not fetch data from array directly therefore we use index point.
The indexing of array always starts with 0.
Index value is always an integer number.
Array may be of any data type like int,char,float etc.
Syntax:
dataType arrayName[] = new dataType[arraySize];
E.g. int a[] = new int[5];
0 1 2 4 5
Memory Representation:
System.out.println("Value at ar[1]="+ar[1]);
System.out.println("Value at ar[2]="+ar[2]);
System.out.println("Value at ar[3]="+ar[3]);
System.out.println("Value at ar[4]="+ar[4]);
}
}
/*
### Output ###
Value at ar[0]=10
Value at ar[1]=50
Value at ar[2]=80
Value at ar[3]=40
Value at ar[4]=60
*/
import java.util.Scanner;
class Payback{
public static void main(String[]args)
{
Scanner in=new Scanner(System.in);
//array declaration
int ar[]=newint[5];
int i;
for(i=0; i<=4; i++)
{
System.out.println("Enter element at "+(i+1));
ar[i]=in.nextInt();
}
for(i=0; i<=4; i++)
System.out.println("Value at ar["+i+"]="+ar[i]);
}
}
Exercise
Run the programs above and print out their results for submission.