You are on page 1of 19

Core Java

Procedural Programming –

1. Execution is done in the order instructions are given in the program.


2. Functionality written for a program is not utilized by any other program.

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.

We need JDK (Java Development Kit) to develop


applications using Java.

JDK contains an important tool called compiler, which


transforms the source code written in Java to executable
code. This process is called compilation.

JRE (JRE) contains all the required tools and libraries to


execute the compiled Java code.
Java became quickly popular because the code written in Java can be run on any operating
system (Like, Windows, Linux, or Mac) provided it has the JRE (Java Runtime Environment)
installed on it.
  Operation code Binary Code

Windows ADD 1001

Linux ADD 1101

MAC ADD 11101

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
{

// Declare variables and functions


here

}
Definition –

A class contains properties in the form of member variables and member


methods.

Automobile - class Addition - class


Attributes – Attributes –
Gear, Brake, Head Light Integer: First, second
Properties of a
Functions – Functions – class
Move_straight() Add()
Move_right()
Move_left()
class A Class declaration
{
int x = 10 , y = 20; Attributes / member
variables
int Add()
{
int sum = x + y; function/ member It contains the
return sum; method properties of a class.
} For object “ob” 
} value of x = 10 and
value of y = 20.
class Use1
{ Access
public static void main(String args[]) specifier
Keyword
referenc {
e A ob = new A() ; Object
variable int output = ob. Add ();
System.out.println (“sum = “ + output );
}
}
Use dot operator to access properties of a packag
class e
A ob = new A();
class
ob . X
ob . Y concatenate
ob. Add() operator method
System.out.println(“Sum = “ + output);

Int x = 10;
Int y = 20;

System.out.println(“Sum = “ + x + y); Sum = 1020

System.out.println(“Sum = “ + (x + y)) Sum = 30 , due to


; Operator
Precedence
Object
concept

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());
}
}

Error: Not static variables are not


accessible in static function
Datatypes in Java
Data Type Default size Boolean one = false
  
boolean 1 bit byte a  = 10
char 2 byte
byte 1 byte int a  = 100000
short 2 byte
double d1 = 12.3  
int 4 byte
long 8 byte char ch  = 'A’  
float 4 byte
double 8 byte Float a = 23.456
Type casting in java

Implicit type casting

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

byte x = (byte) (a + 20) ; // Explicit type casting


public class Main{
public static void main(String[] args) {
char ch = 'a’;
int x= ch; System.out.println(x);
} 97
}

public class Main{


public static void main(String[] args) {
int y = 20;
char ch1 = y; System.out. What is the output ?
println(ch1);
}
}
public class Main{
public static void main(String[] args) {
char ch = 'a’;
int x= ch;
System.out.println( (ch + x) ) ; // output: 194
}
}
Ambiguity with short

The below statement declares two short variables x and y initialized to 3 and 4 respectively.


short x = 3, y = 4;

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.

short total = (short) ( x + y) ;

You might also like