OOP ..Lecture 2A

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 42

Object Oriented Programming

Lecture 2A
Java

Pytho
ABA n Department CS & IT
P
UOL ISB
Objectives
• Introduction to Java
• Java and a Typical Java Development Environment
• Primitive data types
• Wrapper Class
• Type Conversion
• Escaped Characters
• Java Keywords
• Static Keyword
• public static void main(String args[])
• Creating Objects
Introduction to Java
• The world’s most widely used computer programming language.
• Java is the preferred language for meeting many organizations’
enterprise programming needs.
• Java has also become the language of choice for implementing
Internet-based applications and software for devices that
communicate over a network
Java and a Typical Java
Development Environment
Creating a Program
• Consists of editing a file with an editor program, normally known
simply as an editor .
• You type a Java program (typically referred to as source code) using
the editor, make any necessary corrections and save the program on a
secondary storage device, such as your hard drive.
• A file name ending with the .java extension indicates that the file
contains Java source code.
Compiling a Java Program into
Bytecodes
• You use the command javac (the Java compiler) or simply your IDE
(Eclipse) to compile a program. For example, to compile a program
called Welcome.java, we’d type Javac Welcome.java In the Command
Prompt in Windows.
• If the program compiles, the compiler produces a .class file called
Welcome.class that contains the compiled version of the program.
• To run the program we have to type Java Welcome. It will take the
.class file (ByteCodes) and will run it.
Contt..
• Java’s bytecodes are portable—without recompiling the source code,
the same bytecodes can execute on any platform containing a JVM
that understands the version of Java in which the bytecodes were
compiled.
• The JVM is invoked by the java command.
• For example, to execute a Java application called Welcome, you’d type
the command – java Welcome
Loading a Program into Memory
• the JVM places the program in memory to execute it—this is known
as loading .
• The JVM’s class loader takes the .class files containing the program’s
bytecodes and transfers them to primary memory.
Execution
• Today’s JVMs typically execute bytecodes using just-in-time (JIT)
compilation to convert byte codes into machine codes.
Primitive data types
• Primitive types are the most basic data types available within the Java
language; these include boolean , byte, char , short , int
, long , float and double.

• These types serve as the building blocks of data manipulation in Java.

• Such types serve only one purpose — containing pure, simple values
of a kind.
• Examples: int a=10;
Primitive data types
Type Size Range Default
Value
boolean 1 bit true, false false
byte 8 bits -128…..127 0
char 16 bits 0 to 65,536 \u0000
short 16 bits -32,768….32,767 0
int 32 bits -2,147,483,648….2,147,483,647 0
long 64 bits -9,223,372,036,854,775,808…. 9,223,372,036,854,775,807 0
float 32 bits 3.40282347*10^38….1.40239846*10^-45 0.0
double 64 bits 1.7976931348623157*10^308…..4.9406564584124654*10^-324 0.0
Wrapper Class
• Wrapper classes are classes that allow primitive types to be accessed
as objects.

• Wrapper class is wrapper around a primitive data type because they


"wrap" the primitive data type into an object of that class.
Why do We Use Wrapper Class?
• Each of Java's eight primitive data types has a class dedicated to it.

• They are one per primitive type: Boolean, Byte, Character, Double,
Float, Integer, Long and Short.

• As primitive data types are not objects. They not belong to any class.
Sometime it is required (IN THE ADVANCE FRAMEWORKS) to
convert primitive data types into object. Wrapper classes make the
primitive type data to act as objects.
Primitive Data Types and Wrapper
Classes
Difference b/w Primitive Data Type
and
Object of a Wrapper Class
• The following two statements illustrate the difference between a
primitive data type and an object of a wrapper class:

int x = 25;

Integer y = new Integer(x); // boxing

int z = y.intValue(); // Unboxing


Variable
• The first statement declares an int variable named x and initializes it
with the value 25.

Objec
t
• The second statement instantiates an Integer object. The object is
initialized with the value of x.
• The data field in an Integer object is only accessible using the methods
of the Integer class.

• One such method is intValue() method which returns an int equal to


the value of the object, effectively "unwrapping" the Integer object:

int z = x + y.intValue();
(Data) Type Conversion
• type conversion or typecasting refers to changing an entity of one
datatype into another.
• Assigning a value of one type to a variable of anothertype is known
as Type Casting. Example : int x = 10; byte y = (byte)x

TWO TYPES:
• Implicit Conversion
• Explicit Conversion
Implicit Conversion
• Occurs when the range of values of first type is subset of range of
values of second type.
• char c=‘a’; ( Range: 0 to 65,536)
• int k=c; (Range: -2,147,483,648….2,147,483,647)

NO need for the explicit casting in that case.


Explicit Conversion (Narrowing)
• Done via casting
• Name of type to which you want a value converted is given in
parentheses in front of value.
• double d=5.6;
• int k=(int)d;
• short s=(short)d;
Escaped Characters
• Backspace is replaced with \b
• Newline is replaced with \n
• Tab is replaced with \t
• Carriage return is replaced with \r
• Form feed is replaced with \f
• Double quote is replaced with \"
• Backslash is replaced with \\
Java Keywords
Access Modifiers in Java

There are two types of modifiers in Java: access modifiers and non-


access modifiers.

• The access modifiers in Java specifies the accessibility or scope of a


field, method, constructor, or class. We can change the access level of
fields, constructors, methods, and class by applying the access
modifier on it.

• There are four types of Java access modifiers:


• Private: The access level of a private modifier is only within the class. It
cannot be accessed from outside the class.

• Default: The access level of a default modifier is only within the package. It
cannot be accessed from outside the package. If you do not specify any access
level, it will be the default.

• Protected: The access level of a protected modifier is within the package and
outside the package through child class. If you do not make the child class, it
cannot be accessed from outside the package.

• Public: The access level of a public modifier is everywhere. It can be accessed


from within the class, outside the class, within the package and outside the
package.
Access Modifier Table
Example of Private Identifier

class A{  
private int data=40;  
private void msg(){System.out.println("Hello java");}  
}  
  
public class Simple{  
public static void main(String args[]){  
A obj=new A();  
System.out.println(obj.data);//Compile Time Error  
obj.msg();//Compile Time Error  
}  
}  
Example of Default Identifier

class A{  
int data=40;  
void msg(){System.out.println("Hello java");}  
}  
  
public class Simple{  
public static void main(String args[]){  
A obj=new A();  
System.out.println(obj.data);//Ok
Output
obj.msg();//Ok
}   40
}   Hello Java
Example of Default Class Identifier
package pack;  
class A{  
void msg(){System.out.println("Hello");}  
}  
  
package mypack;  
import pack.*;  
class B{  
public static void main(String args[]){  
A obj = new A();//Compile Time Error  
obj.msg();//Compile Time Error  
 }  
}  
Example of Public Class Identifier
package pack;  
Public class A{  
void msg(){System.out.println("Hello");}  
}  
  
package mypack;  
import pack.*;  
Public class B{  
public static void main(String args[]){  
A obj = new A();//Ok   Output
obj.msg();//Ok  
Hello
 }  
}  
Example of Protected Identifier
  
package pack;  
public class A{  
protected void msg(){System.out.println("Hello");}  
}  
  
package mypack;  
import pack.*;  
  
class B extends A{  
public static void main(String args[]){   Output
B obj = new B(); // OK 
obj.msg();  // Ok Hello
 }  
}  
Types of Variable in Java
• There are two main types: Static and No-Static.
• Static variables are declared static by using the Keyword Static, such
as:

Static int 1=10;

• If a variable is not declared as static then it is a no-static variable. All


instance variables are no-static and they are normally defined in a
class outside of every method.
Static variables in Class
• Class level instead of instance level
• Refers to common property of all objects.
• Shared by all objects of the class.
• Can be accessed directly from class itself
• Gets memory only once in class area at the time of class loading.
• Memory efficient
Static methods in Class
• A static method belongs to the class rather than object of a class.

• static method can access static data member and can change the value of it.

• A static method can access only static data. It can not access non-static data (instance
variables)

• A static method can call only other static methods and can not call a non-static method
from it.

• A static method can be accessed directly by the class name and doesn’t need any object

• A static method cannot refer to "this" or "super" keywords in anyway


Final in java
• Final Variable
• Cannot be changed
• Its value is constant through out the execution of application
• Final Methods
• Cannot be overridden by methods of subclass.
• Final Class
• Cannot be sub classed (Cannot be inherited)
Example of Final Variable
1.class Bike9{  
2. final int speedlimit=90;//final variable  
3. void run(){  
4.  speedlimit=400;  
5. }  
6. public static void main(String args[]){  
7. Bike9 obj=new  Bike9();  
8. obj.run();  
9. }  
10.
}//end of class  

Output: Compile Time Error

Final Variable cannot be change.


Example of Final Method
1. class Bike{  
2.  final void run(){System.out.println("running");}  
3.}  
4.  
5.class Honda extends Bike{  
6.   void run(){System.out.println("running safely with 100kmph");}  
7.     
8.   public static void main(String args[]){  
9.   Honda honda= new Honda();  
10.   honda.run();  
11.   }  

Output: Compile Time Error

If you make any method as final, you cannot override it.


Example of Final Class
1.final class Bike{}  
2.  
3.class Honda1 extends Bike{  
4.  void run(){System.out.println("running safely with 100kmph");}  
5.    
6.  public static void main(String args[]){  
7.  Honda1 honda= new Honda1();  
8.  honda.run();  
9.  }  
10.}  

Output: Compile Time Error

If you make any class as final, you cannot extend it.


Public static void main(String args[])
• Public
• It means that you can call this method from outside of the class you are currently in. This is
necessary because this method is being called by the Java runtime system which is not
located in your current class.
• Static

• When the JVM makes call to the main method there is no object existing for the class being
called therefore it has to have static method to allow invocation from class.

• Anything which is declared in class in Java comes under reference type and requires object to
be created before using them but static method and static data are loaded into separate
memory inside JVM called context which is created when a class is loaded. If main method is
static then it will be loaded in JVM context and are available to execution)
• void
• Java is platform independent language and if it will return some value then
the value may mean different things to different platforms. Also there are
other ways to exit the program on a multithreaded system.
• main
• It's just the name of method. This name is fixed and as it's called by the JVM
as entry point for an application.
• String args[]
• These are the arguments of type String that your Java application accepts
when you run it.
Example
public class PrintArgs{
public static void main(String args[]){
for(int i=0;i<args.length;i++){
System.out.println(args[i]);
}
}
}
Write To Console
• System.out.println(“Hello World”);
• System: class in java API(Application Programming Interface)
• out: a static field in class system of type PrintStream
• println: method of class PrintStream
Creating Objects
• Obtaining objects include declaring a variable of class type and
acquiring an actual physical copy of an object and assign its address to
variable.
• Three steps while creating object
• Declaration: Variable declaration with a variable name with an object type
• Instantiation: ‘new’ keyword is used to create object
• Initialization: ‘new’ keyword is followed by call to constructor

You might also like