Programming Language (PL)
• PL is an artificial language used to communicate with
computers(Machines).
• PL is used to build software.
T
Source Code
Translator Machine Code Computer
(Compiler or
or
Or Software
Program
Interpreter)
www.incapp.in Rahul Chauhan
Java Introduction & History
Java Programming Language
Created By: James Gosling (Dr. Java)
v Initiated in June 1991 and First version came in 1995.
v Developed by Sun Microsystems.
v Best suited for Security and Platform Independency.
v First name: Oak
v Second name: Green
v Final name: Java (came from Java Coffee of Indonesia).
v Syntax came form C and C++
v Oracle Corporation acquired Sun Microsystems in 2010.
www.incapp.in Rahul Chauhan
www.incapp.in Rahul Chauhan
• JRE is an environment where the java’s byte code execute.
• JVM is the machine that executes the java’s byte code.
• JIT(Just-In Time) is the interpreter used by JVM to interpret java’s byte code to
native code(machine code) that gives output.
• JAVAC is the compiler that compiles the source code and generates byte code.
• JDK is the kit which contains the JRE, JVM and JAVAC.
www.incapp.in Rahul Chauhan
Tokens
Every Individual units in a program
is TOKEN
www.incapp.in Rahul Chauhan
Keyword Token
A word created by language for particular job that can not be changed by
programmer is a keyword.
www.incapp.in Rahul Chauhan
Keywords in java
The goto and const keywords are not in use.
true, false, null are the reserved words not keyword.
www.incapp.in Rahul Chauhan
Identifier Token
Identifier is a word created by a programmer for variable-name ,
function-name, class-name and interface-name to use them further.
www.incapp.in Rahul Chauhan
Rules for Identifiers
Rules:
1. It can only be from Alphabets( a-z, A-Z ) and Digit( 0-9 ) and
underscore( _ ) and dollar ( $ ).
2. It can not start from digit.
3. Only Underscore is not allowed.
4. Keyword can not be an Identifier.
www.incapp.in Rahul Chauhan
Operator Token
Operator is a symbol used to do operation on data.
www.incapp.in Rahul Chauhan
List of Operators
Assignment =
Arithmetic + - * / %
Shorthand/Compound += -= *= /= %=
Increment-Decrement ++ --
Relational < <= > >= == !=
Logical && || !
Conditional ?:
Bitwise & | ^ >> << ~
www.incapp.in Rahul Chauhan
Separator Token
Separator is a symbol that is used to do separation in code.
www.incapp.in Rahul Chauhan
List of Separators
() Round Bracket
{} Curly Bracket
[] Square Bracket
; Semicolon
, Coma
. Dot
… Triple Dot
@ At
: Colon
:: Double Colon
www.incapp.in Rahul Chauhan
Literal Token
Literal is the constant data in a program.
www.incapp.in Rahul Chauhan
www.incapp.in Rahul Chauhan
Gapping Rule of Tokens
• In between the
keyword and keyword
identifier and identifier
keyword and identifier
identifier and keyword
gap(space or new line) is compulsory.
• In between other tokens gap is optional.
www.incapp.in Rahul Chauhan
Data & Variable
Variable is just a container that contains data.
101 int roll= 101 ;
Rahul String name= “Rahul” ;
5.9 double height= 5.9 ;
www.incapp.in Rahul Chauhan
Data Type
Data type is just a word used to specify type of data, data size and data range.
Java has two types of DataTypes: Primitive and Non-Primitive .
www.incapp.in Rahul Chauhan
Primitive Data Types
www.incapp.in Rahul Chauhan
Type Casting
Converting the type of data into another data type.
Implicit Typecasting Explicit Typecasting
Upcasting (Widening) Downcasting (Narrowing)
www.incapp.in Rahul Chauhan
TypeCasting Graph
www.incapp.in Rahul Chauhan
Operators
Operator is a symbol used to do operation on data.
Assignment =
Arithmetic + - * / %
Shorthand/Compound += -= *= /= %=
Increment-Decrement ++ --
Relational < <= > >= == !=
Logical && || !
Conditional ?:
Bitwise & | ^ >> << ~
www.incapp.in Rahul Chauhan
Assignment Operator ( = )
int a; 10 = a;
a = 10;
int a = 10;
int a = 10, b = 15;
int a, b;
a = b = 10;
www.incapp.in Rahul Chauhan
Arithmetic Operator ( + - * / % )
int a; int a; int a;
a = 10 + 2; a = 10 - 2; a = 10 * 2;
SOP(a); SOP(a); SOP(a);
int a; int a;
a = 10 / 2; a = 10 % 2;
SOP(a); SOP(a);
www.incapp.in Rahul Chauhan
Shorthand Operator ( += -= *= /= %= )
int a=10; int a=10; int a=10; int a=10;
a = a / 5; a /= 5; a =/ 5; a / = 5;
SOP(a); SOP(a); SOP(a); SOP(a);
int a=10; int a=10;
a = a + 5; a += 5;
SOP(a); SOP(a);
www.incapp.in Rahul Chauhan
Increment-Decrement Operator ( ++ -- )
int a=10; int a=10; int a=10; int a=10;
a = a + 1; a ++; a = a - 1; a --;
SOP(a); SOP(a); SOP(a); SOP(a); Postfix
int a=10; int a=10; int a=10; int a=10;
a = a + 1; ++ a; a = a - 1; -- a; Prefix
SOP(a); SOP(a); SOP(a); SOP(a);
10 ++ ; int a=10;
a+ +;
SOP(a);
www.incapp.in Rahul Chauhan
Relational Operator ( < <= > >= == != )
int a=10,b=5; int a=10,b=5; int a=10,b=5;
boolean c=a<b; boolean c=a==b; boolean c=a< =b;
SOP(c);
SOP(c); SOP(c);
int a=10,b=5; int a=10,b=5;
boolean c=a<=b; boolean c=a!=b;
SOP(c); SOP(c);
int a=10,b=5; int a=10,b=5;
boolean c=a>b; boolean c=a>=b;
SOP(c); SOP(c);
www.incapp.in Rahul Chauhan
Logical Operator ( && || ! )
int a=10,b=5;
true && true true boolean c=a>b && b<3;
SOP(c);
true && false false
int a=10,b=5;
false && true false boolean c=a<b && b<3;
false && false false SOP(c);
int a=10,b=5;
true || true true boolean c=a<b || b<3;
true || false true SOP(c);
false || true true int a=10,b=5;
boolean c=a>b || b==3;
false || false false SOP(c);
! true false int a=10,b=5;
boolean c=!(a>b) || !(b==3);
! false true SOP(c);
www.incapp.in Rahul Chauhan
Conditional Operator ( ?: )
Exp. 1 ? Exp. 2 : Exp. 3
true false
Exp. 2 Exp. 3
int a=10,b=5;
int c=a<b ? a+12/3-2 : b%5+a-7;
SOP(c);
www.incapp.in Rahul Chauhan
Bitwise Operator ( & | ^ >> << ~ )
int a=12,b=5; int a=12,b=5; int a=12,b=5;
int c=a & b; int c=a | b; int c=a ^ b;
SOP(c); SOP(c); SOP(c);
000….001100 000….001100 000….001100
000….000101 000….000101 000….000101
000….000100 000….001101 000….001001
int a=12; int a=12; int a=12;
int c=a >> 1; int c=a << 1; int c= ~ a;
SOP(c); SOP(c); SOP(c);
000….001100 000….001100 ~a = - ( a+1)
0000….00110 000….011000
www.incapp.in Rahul Chauhan
www.incapp.in Rahul Chauhan
www.incapp.in Rahul Chauhan
www.incapp.in Rahul Chauhan
www.incapp.in Rahul Chauhan
www.incapp.in Rahul Chauhan
Scanning data from Keybord
java.util.Scanner sc=new java.util. Scanner( System.in );
int age =sc.nextInt();
double height =sc.nextDouble();
String name =sc.next();
www.incapp.in Rahul Chauhan
Expression Rules
Println function is Expression based. So we have to understand expression rules.
Integer Integer
+
Character - Integer
* Integer
Integer / Character
% Character
Character
For Example:
7+2 = 9
7/2 = 3 (not 3.5)
‘a’+10 = 107
10+ ‘a’ = 107
‘h’*10 = 1040
www.incapp.in Rahul Chauhan
Expression Rules
Floating Point Floating Point
+ Integer
Floating Point
-
Floating Point * Character Floating Point
/
Character % Floating Point
Integer Floating Point
For Example:
7.0+2 = 9.0
7/2.0 = 3.5
‘a’+10.1=107.1
10.0+ ‘a’=107.0
‘h’*10.0=1040.0
www.incapp.in Rahul Chauhan
Expression Rules
Integer
Floating Point
String
String +
Character
String
Integer
Floating Point
String String
+
Character
String
www.incapp.in Rahul Chauhan
Expression Rules
For Example:
5 + “hi” = “5hi”
“hi”+5 = “hi5”;
4+2+ “hi” = “6hi”
5.8+ “hi” = “5.8hi”;
5+6+ “hi” + 5+6= “11hi56”
“hello”+ “hi” = “hellohi”
“hello”+ ‘a’= “helloa” (not hello97)
‘a’ + “hello” = “ahello”
www.incapp.in Rahul Chauhan
Expression Rules
Integer
-
Floating Point
*
String
Character /
%
String
www.incapp.in Rahul Chauhan
Decision Making
• Decision Making is used to execute a block of code on condition,
if the condition is satisfied then code will execute otherwise not.
• Types of decision making:
if If else if else if switch
www.incapp.in Rahul Chauhan
if statement
if (expression) { Here, expression must be boolean.
Boolean expression returns either true or false.
// statements
}
Expression -> true Expression -> false
int age=25; int age=15;
if (age>18) { if (age>18) {
// statements // statements
} }
// statements after if // statements after if
www.incapp.in Rahul Chauhan
If-else statement
Expression -> true Expression -> false
int age=25; int age=15;
if (age>18) { if (age>18) {
// statements of if // statements of if
} }
else { else {
// statements of else // statements of else
} }
// statements after if else // statements after if else
www.incapp.in Rahul Chauhan
If-else-if statement
int age=15;
if (age>60) {
// statements of if
}
else if (age>18) {
// statements of else if
}
else {
// statements of else
}
// statements after if else if
www.incapp.in Rahul Chauhan
Switch statement
switch (value/expression) { • Switch works with
case value1 : the byte, short, char, and int, String
class, and few wrapper classes Byte,
// statements of case Short, Character and Integer.
break; • Switch does not work with float,
case value2 : double, boolean and long.
// statements of case
break;
default :
// statements of default
}
// statements after switch
www.incapp.in Rahul Chauhan
Loop Controls
• Loop is used to execute a block of code for multiple times.
• Write the code once and execute it many times.
• Types of loop:
for while do while for-each(enhanced for)
www.incapp.in Rahul Chauhan
For Loop
Without Loop With Loop
SOP(“Rahul”);
for ( int a=1 ; a<=10 ; a++ ) {
SOP(“Rahul”);
SOP(“Rahul”);
SOP(“Rahul”);
SOP(“Rahul”);
SOP(“Rahul”);
}
SOP(“Rahul”);
SOP(“Rahul”); for ( Initialization ; TestExpression ; Update )
SOP(“Rahul”); {
SOP(“Rahul”); // statements
SOP(“Rahul”); }
www.incapp.in Rahul Chauhan
While Loop
Without Loop With Loop
SOP(“Rahul”); int a=1;
SOP(“Rahul”); while ( a<=5 ) {
SOP(“Rahul”); SOP(“Rahul”);
SOP(“Rahul”); a++;
SOP(“Rahul”); }
while ( TestExpression) {
// statements
}
www.incapp.in Rahul Chauhan
Do-While Loop
Without Loop With Loop
SOP(“Rahul”); int a=1;
SOP(“Rahul”); do {
SOP(“Rahul”); SOP(“Rahul”);
SOP(“Rahul”); a++;
SOP(“Rahul”); } while ( a<=5 ) ;
do {
// statements
} while ( TestExpression) ;
www.incapp.in Rahul Chauhan
Break keyword
for( init; Test-Exp; Update){ do{
// statements // statements
if (BreakCondition) { if (BreakCondition) {
// statements // statements
break; break;
} }
// statements // statements
} } while (Test-Exp);
while (Test-Exp){
// statements
if (BreakCondition) {
// statements
break;
}
// statements
}
www.incapp.in Rahul Chauhan
Continue keyword
for( init; Test-Exp; Update){ do{
// statements // statements
if (BreakCondition) { if (BreakCondition) {
// statements // statements
continue; continue;
} }
// statements // statements
} } while (Test-Exp);
while (Test-Exp){
// statements
if (BreakCondition) {
// statements
continue;
}
// statements
}
www.incapp.in Rahul Chauhan
Function/Method
PSVM(-){ PS void display(){
SOP( “Hello” ); SOP( “I Love My INDIA” );
SOP( “I Love My INDIA” ); SOP( “I Live in INDIA” );
SOP( “I Live in INDIA” ); SOP( “I am Happy” );
SOP( “I am Happy” ); }
SOP( “Hi” ); PSVM(-){
SOP( “I Love My INDIA” ); SOP( “Hello” );
SOP( “I Live in INDIA” ); display();
SOP( “I am Happy” ); SOP( “Hi” );
SOP( “Bye” ); display();
SOP( “I Love My INDIA” ); SOP( “Bye” );
SOP( “I Live in INDIA” ); display();
SOP( “I am Happy” ); }
}
www.incapp.in Rahul Chauhan
Function/Method Syntax
Return Type Function Name Argument
void display ( ) {
SOP( “I Love My INDIA” );
SOP( “I Live in INDIA” );
SOP( “I am Happy” );
}
Function Body
www.incapp.in Rahul Chauhan
Function/Method
Types of Functions:
• Function with No Arguments and No Return Type
• Function with Arguments but No Return Type
• Function with No Arguments but Return Type
• Function with Arguments and Return Type
• Function with simple Return keyword
www.incapp.in Rahul Chauhan
No Arguments and No Return Type
public class Demo{
public static void add(){
int a=10,b=20,r=a+b;
SOP( “Sum: ”+r );
}
public static void main(String[] s){
add();
}
}
www.incapp.in Rahul Chauhan
Arguments and No Return Type
public class Demo{
public static void add(int a, int b){
int r= a+b;
SOP( “Sum: ”+r );
}
public static void main(String[] s){
add(12,34);
}
}
www.incapp.in Rahul Chauhan
No Arguments but Return Type
public class Demo{
public static int add(){
int a=10,b=20,r=a+b;
return r;
}
public static void main(String[] s){
int r=add();
SOP( “Sum: ”+r );
}
}
www.incapp.in Rahul Chauhan
Arguments but Return Type
public class Demo{
public static int add(int a, int b){
int r= a+b;
return r;
}
public static void main(String[] s){
int r=add(12,34);
SOP( “Sum: ”+r );
}
}
www.incapp.in Rahul Chauhan
Simple Return keyword
public class Demo{
public static void xyz(int a, int b){
SOP( “Hello” );
if(a==b){
return;
}
SOP( “Hi” );
}
public static void main(String[] s){
xyz(10,20);
}
}
www.incapp.in Rahul Chauhan