You are on page 1of 26

CORE JAVA

Akhilesh Chandra
Session Objectives

• OOPS CONCEPT IN JAVA


• DATA TYPES
• VARIABLES
• ARRAYS
• OPERATORS
• CONTROL STATEMENTS

Akhilesh Chandra
OOP PRINCIPLES

• ABSTRACTION
• ENCAPSULATION
• INHERITANCE
• POLYMORPHISM

Akhilesh Chandra
ENCAPSULATION

• It is the mechanism that binds code and the


data together
• Manipulates, and provide safety from
outside interference and misuse.

Akhilesh Chandra
INHERITANCE

• It is the process by which one object aquires


the properties of another object.

Akhilesh Chandra
POLYMORPHISM

• It is to perform specific action depending


on the situation

Akhilesh Chandra
Introduction to JAVA
Primary motivation behind:
• Platform neutral programming language
• Primarily developed as a cost efficient
solution for consumer Electronic devices .
• Later, came it’s importance on Internet.
• This caused the focus of Java to shift from
consumer electronics to Internet
programming.
Akhilesh Chandra
Similarities between Java and C++

• Java is influenced by C++, but not an


enhanced version of C++
• Java was not designed to replace C++.
• Java is to Internet programming what
C or C++ , was to System
programming.

Akhilesh Chandra
Importance of Java on Internet
• Two broad categories of objects those are
transmitted over Net:
– Passive information.
– Dynamic/active programs.
• Java addresses the security and portability
concerns that are involved in downloading
Dynamic programs over Internet , in the
form of Applets.

Akhilesh Chandra
How Java is Platform
Independent

Java Compiler Interpreter(JVM)

WINDOWS UNIX

BYTE CODE
UNIX WINDOWS

SOLARIS SOLARIS

Akhilesh Chandra
Byte Code

• Bytecode is a highly optimized set of


instructions designed to be executed by Java
run time system,which is called JVM.
• Java Virtual Machine is an interpreter for
byte code.
• Byte Code is platform independent.
• JVM is platform dependent.

Akhilesh Chandra
JVM Architecture

Byte Code Verifier

Class Loader

Interpreter Just In Time Compiler

Native Machine Code

Akhilesh Chandra
Features of Java
• Simple
• Object Oriented
• Portable
• Robust
• Multi Threaded
• High Performance
• Distributed
• Dynamic
Akhilesh Chandra
// Example 1
class ex1
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}

Akhilesh Chandra
DATA TYPES

• 4 INTEGER type ( byte, short, int, long)


• 2 FLOATING POINT type (float, double)
• 1 CHARACTER type (char)
• 1 BOOLEAN type (boolean)

Akhilesh Chandra
Integer types

NAME BYTE RANGE


• byte 8 -128 TO 127

• short 16 -32,768 to 32,767

• int 32 -2,147,483,648 to 2,147,483,647

• long 64 -9,223,372,036,854,775,808 to

9,223,372,036,854,775,807

Akhilesh Chandra
FLOATING-POINT

• NAME BYTES RANGE

• float 32 3.4e-038 to 3.4e+038


• double 64 1.7e-308 to 1.7e+308

Akhilesh Chandra
class ex2
{
public static void main(String args[])
{
int num;
num = 100;
System.out.println("Number is " +num);
num = num * 2;
System.out.println("Number after change is " + num);
}
}
Akhilesh Chandra
/* To understand the scope of variable Example 3 */
class ex3{
public static void main(String args[])
{ int x;
x = 10;
if(x == 10)
{ int y=20;
System.out.println("X and Y are :"+ x + " " + y);
x = y * 2;
}
y =100; //Is it Accessible here
System.out.println("X is " +x);
Akhilesh Chandra
/* To demonstrate the life time of variables
Example 4 */
class ex4 {
public static void main(String args[])
{
int x;
for(x=0;x<3;x++)
{
int y =-1;
System.out.println("Y is : " +y);
y =100;
System.out.println("Y is now :" +y);
Akhilesh Chandra
} } }
/* To demonstrate some kind of error
Example 5*/
class ex5 {
public static void main(String args[])
{
int num =1;
{
int num =2; //Compile time error
System.out.println(num);
}
}
Akhilesh Chandra
Type Conversion

• Java’s Automatic type conversions:


– When one type of data is assigned to another
type of variables ,an automatic type conversion
takes place ,provided following conditions are
met:
– Two types are compatible
– The destination type is larger than the source
type.

Akhilesh Chandra
Points regarding type
Compatibility

• An integer and floating-point variables are


compatible with each other.
• Numeric types are not compatible with char
and boolean.
• char and boolean are not compatible with
each other.

Akhilesh Chandra
Type Casting
• To have a conversion between two incompatible
types , we use casting.
• A cast is an explicit type conversion.
• General form :
– (target type) value;
Ex: Converting an int to byte
int a;
byte b = a; //gives an error
byte b= (byte)a; //resolves the error.

Akhilesh Chandra
Automatic Type Promotion
• In an Expression, automatic type promotion takes
place.
• Java automatically promotes each of the operands
to int , while evaluating an expression.
Ex: byte a = 40;
byte b = 50;
byte c = a * b; // Gives an error
int d = a * b; // This is valid.
Ex: byte b =50;
b = b* 2; // Is there any error?Try to resolve.
Akhilesh Chandra
Type Promotion Rules

Operand 1 Operand 2 Promoted to


byte short int
long any long
float any float
double any double

Akhilesh Chandra

You might also like