Professional Documents
Culture Documents
Procedural Programming –
A sample program in C -
1. # include <stdio.h>
2. # include <conio.h>
3. Void main()
4. {
5. int x=10;
6. int y = 20;
7. int total = x+y;
8. printf(“%d”, total);
9. }
JDK
Java can be used to develop virtually any kind of
software applications like games that can run on
PCs and Mobiles, Desktop Applications, Web
Applications, Embedded Applications (for example
applications that can run in a watch or a pen), etc.
Convert according to
machine instructions of
JRE LINU LINUX Hello.exe
java
X
Convert according to
Source java Byte code machine instructions of
code (hello. c (hello.class) JRE MAC
MAC Hello.exe
java)
Convert according to
JRE Windows
machine instructions of
Windows Hello.exe
Java
portability
Windows ---
Int x= 10
Int y=20
Int sum = x+y ----- 1
Machine code ADD R1, M[x], M[y] -- Byte code -- .class file
1001 11 FF BB -- processor - instruct - add operation
Linux machine installed JRE - Java virtual machine
ADD – 11001 ADD R1, M[x], M[y] - not able to perform addition
Command – java --- JDK – tool (java interpreter) - activate - JVM - ADD - 11001
Platform independence feature – portability 22
Object Oriented Programming –
Key concepts –
1. Encapsulation
2. Polymorphism
3. Inheritance
1. Encapsulation – Binding of code within a class.
Class A
{
}
Definition –
Int x = 10;
Int y = 20;
class A { class B {
int a = 10; public static void main(String args[] )
} {
Instance variable
A ob1 = new A();
System.out.println(ob1.a); 10
a = 10
ob1.a = 20;
Object – ob1 a = 20 System.out.println(ob1.a); 20
(separate
memory) A ob2 = new A();
System.out.println(ob2.a); 10
Object – ob2 a = 10 }
(separate
}
memory)
Class / static variable
concept class A {
static int a = 10;
}
Object – ob1
class B {
a = 10 public static void main(String args[] )
(shared memory)
{
Object – ob2 A ob1 = new A();
System.out.println(ob1.a); 10
ob1.a = 20;
System.out.println(ob1.a); 20
A ob2 = new A();
System.out.println(ob2.a); 20
}
}
Class / static variable
concept class A {
Static variables can be accessed by class name
also.
static int a = 10;
}
class B {
public static void main(String args[] )
{
System.out.println (A.a);
A.a = 20;
System.out.println(A.a);
}
}
Class / static variable concept contd .. class A {
int a = 10 , b = 20;
static int Add() {
int sum = a + b; }
}
class B {
public static void main(String args[] )
{
System.out.println (“Sum = “ + A.
Add());
}
}
byte a = 8;
int x = a + 20;
int a = 10;
byte x = a + 20; // error: not able to convert from lower range
data type to higher range data type
short total = x + y;
However, when we try to perform an addition operation, such as given below,
we get a compilation error: Type mismatch: cannot convert from int to short.
Since x and y are of type short, they both are automatically widened to int, and the resultant
int is being assigned to sum which is of type short.
The compile-time error occurs while assigning the result which happens to be an int. To fix
it, we will have to explicitly typecast the result to a short, as shown below.