OOP ..Lecture 2A
OOP ..Lecture 2A
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.
• 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.
• 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;
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.
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)
• 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.
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 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
• 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