Java Unit II
Java Unit II
favorite subject
JAVA - UNIT II
OOP Java is the easiest, scoring and my
favorite subject
Constructor
Constructor
1. class Cube{
🞂 A constructor in Java is a special type of method that is used to
OOP Java is the easiest, scoring and my
Construct
OOP Java is the easiest, scoring and my
favorite subject
or
Default Constructor
Default Constructor: [Link]
1. class MyConst {
OOP Java is the easiest, scoring and my
no longer used.
🞂 The default constructor automatically initializes all instance
[Link] to zero.
class MyAccount{ 8. class MyBank {
2. int accNo;
Example: 9. public static void main(String[] args){
3. double balance; 10. MyAccount ma= new MyAccount();
4. MyAccount(){ 11.
[Link]("balance="+[Link]);
5. //default constructor
12. }//main()
6. }
13.}//class
7. }//class
Outp
ut
balance=0.0
OOP Java is the easiest, scoring and my
favorite subject
No-Argument
Constructor
No-Argument Constructor: [Link]
[Link] MyMain{
1. class Cube { OOP Java is the easiest, scoring and my
9. height = 10;
10. depth = 10;
11. }//Cube()
12.}//class
OOP Java is the easiest, scoring and my
favorite subject
Parameterized
Constructor
Parameterized Constructor: [Link]
[Link] MyMain{
1. class Cube { OOP Java is the easiest, scoring and my
Copy Constructor
Copy Constructor
🞂 It is a special type of constructor that is Constructor
used to create a new
2(Constructor
OOP Java is the easiest, scoring and my
Constructor 1()
favorite subject
Constructor 1()
favorite subject
various parameters. 1)
{
{
🞂 Whenever we need to add all the field ……of a class to another
object, then just send the reference of } previously created
}
object.
Copy
🞂 One of the most importance of copy constructors is that there is
Constructor
no need for any typecasting.
🞂 Using a copy constructor, we can have complete control over
object creation.
🞂 With Copy Constructor, we can pass object of the class as a
parameter(pass by reference).
OOP Java is the easiest, scoring and my
favorite subject
Constructor
Overloading
Constructor Overloading
1.🞂class
In addition
Balance{ to overloading normal methods,
1. class Account{ you can also
OOP Java is the easiest, scoring and my
favorite subject
creation.
🞂 Used for the initialization of instance variables.
🞂 To assign the default value to instance variables.
🞂 To initializing objects of the class.
Constructor() vs. Method()
Constructor() Method()
OOP Java is the easiest, scoring and my
Naming Constructor name must be same as class Method name can be anything.
favorite subject
name.
Return types Constructor does not have any return type, Method must have return types, at least
not even void. void.
Call Constructor can be invoked implicitly when Method is called by the programmer.
object is created. Invoked explicitly.
Purpose To initialize an object To execute the code
Inheritance Constructor cannot be inherited by Method can be inherited by subclass.
subclass.
OOP Java is the easiest, scoring and my
favorite subject
Destructor
Destructor
🞂 Destructor is the opposite to the constructor. Constructor is
OOP Java is the easiest, scoring and my
favorite subject
⮩ When we create an object of the class(using new), it occupies some space in the
memory. If we do not delete these objects, it remains in the memory and occupies
unnecessary space.
⮩ To resolve this problem, we use the destructor.
🞂 Remember that there is no concept of destructor in Java.
🞂 Instead of destructor, Java provides the garbage collector that
works the same as the destructor.
🞂 The garbage collector is a program (thread) that runs on
the JVM. It automatically deletes the unused objects (objects
that are no longer used) and free-up the memory.
🞂 The programmer has no need to manage memory, manually.
Working of garbage collector(destructor) in java
Static
Static in java
Static in Java
🞂 The static keyword is used for memory management. OOP Java is the easiest, scoring and my
favorite subject
🞂 We can apply static keyword with variables, methods, blocks and nested classes.
🞂 The static keyword belongs to the class than an instance of the class.
🞂 The static can be:
1. Variable (also known as a class variable)
2. Method (also known as a class method)
3. Block
4. Nested class
Static Variable
🞂 Static variables have a property of preserving their value even after they are out of their scope.
OOP Java is the easiest, scoring and my
favorite subject
🞂 The static variable gets memory only once in the class area at the time of class loading.
🞂 Advantage of static variable: It makes program memory efficient (static variable saves memory).
🞂 Characteristics of static variable:
⮩ It is initialized to zero when the first object of its class is created. No other initialization is allowed.
⮩ Only one copy of that member is created for the entire class and is shared by all the objects of that class, no
matter how many objects are created.
⮩ It is visible only within the class, but its lifetime is the entire program.
⮩ Static variables are normally used to maintain values common for all objects.
⮩ The class constructor does not initialize static variable.
Static vs Non-Static Function
[Link](Non-static [Link](static
function) function)
OOP Java is the easiest, scoring and my
🞂 A static method can call only other static methods and can not call a non-static method from it.
OOP Java is the easiest, scoring and my
favorite subject
🞂 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
Static Method: WAP using class Rectangle and calculate area
[Link] MyRectangle {
OOP Java is the easiest, scoring and my
🞂 static keyword is mainly used for memory management. OOP Java is the easiest, scoring and my
favorite subject
🞂 It can be used with
⮩ Variables
⮩ Methods
⮩ Blocks
⮩ Nested classes
🞂 Basically, static is used for a constant variable or a method that is same for every instance of a class.
🞂 The static variable can be used to refer to the common property of all objects.
🞂 The static variable gets memory only once in the class area at the time of class loading.
🞂 It makes your program memory efficient.
🞂 Syntax
static type variablename;
class Student {
int rollNo; OOP Java is the easiest, scoring and my
static String
String collegecollege
= =
favorite subject
student1 student2 student3
.
"DIET";
. rollN
. o
} nam
e
colleg DIET DIET DIET
class StudentDemo { e
public static void main(String
args[]) { colleg DIET
Student student1 = new e
Student();
Student student2 = new
Student();
Student student3 = new
Student();
.
.
.
}
}
static method
🞂 If you apply static keyword with any method, it is knownOOPas static method.
Java is the easiest, scoring and my
favorite subject
🞂 A static method belongs to the class rather than the object of a class.
🞂 A static method can be invoked without the need for creating an instance of a class.
🞂 A static method can access static data member and can change the value of it.
🞂 Restrictions
1. The static method can not use non static data member or call non-static method directly.
2. this and super cannot be used in static context.
class Student {
int rollno;
String name;
static String college="abc";
OOP Java is the easiest, scoring and my
favorite subject
🞂 Static block is executed exactly once, when the class is first loaded.
OOP Java is the easiest, scoring and my
favorite subject
🞂 It is used to initialize static variables of the class.
🞂 It will be executed even before the main() method.
🞂 Syntax
static {
//initialisation of static
variables…
}
Example: Static Block, Method and Variable
1. class StaticDemo {
2. static int a = 4; //static variable declared & initialized
OOP Java is the easiest, scoring and my
favorite subject
1. When we declare a field static, exactly a single copy of that field is created and shared among all
OOP Java is the easiest, scoring and my
instances of that class. favorite subject
2. Static variables belong to a class, we can access them directly using class name. Thus, we don't need
any object reference.
3. We can only declare static variables at the class level.
4. We can access static fields without object initialization.
5. Static methods can't be overridden.
6. Abstract methods can't be static.
7. Static methods can't use this or super keywords.
8. Static methods can't access instance variables and instance methods directly. They need some object
reference to do so.
9. A class can have multiple static blocks.
Mutable and Immutable Objects
🞂 The content of mutable object can be changed, while content of immutable objects can not be changed.
OOP Java is the easiest, scoring and my
favorite subject
class Time{
🞂 In order to understand howint
andhour;
why we need to pass OOP
object as an argument in methods, lets see the
Java is the easiest, scoring and my
below example. int minute; favorite subject
int second;
public Time(int hour, int minute, int
second) {
[Link] = second;
[Link] = minute;
[Link] = hour;
} public class TimeDemo {
void add(Time t) { public static void main(String[]
[Link] += [Link]; args) {
if([Link]>=60) { Time t1 = new Time(11,59,55);
[Link]++; Time t2 = new Time(0,0,5);
[Link]-=60;
} [Link](t2);
[Link] += [Link];
if([Link]>=60) { [Link]([Link] + ":"
[Link]++; + [Link] + ":" + [Link]);
[Link]-=60; }
} }
[Link] += [Link];
}
}
Array of Objects
🞂 We can create an array of object in java. OOP Java is the easiest, scoring and my
favorite subject
🞂 Similar to primitive data type array we can also create and use arrays of derived data types (class).
Nested Class
Nested Class
⮩ E.g. class B is defined within class A, then B is known to A, but not outside of A.
🞂 A nested class has access to the members, including private members of the class in which it is nested.
🞂 However, the enclosing class does not have access to the members of the nested class. i.e. Class B can access private member
of class A, while reverse is not accessible.
A: Outer
Class
B:
Nested
Class
Nested Class
Nested Class
A: Outer
Class
B:
Nested
Class
OOP Java is the easiest, scoring and my
favorite subject
thi
s
‘this’ keyword
class Box {
double length;
double breadth;
double height;
Box(double l, double b, double h) { OOP Java is the easiest, scoring and my
favorite subject
[Link]("Constructing Box");
length = l;
breadth = b;
height = h;
}
void volume() {
double volume = length * breadth * height;
[Link]("Volume is " + volume);
}
}
class BoxDemo {
public static void main(String args[]) {
Box myBox1 = new Box(10,20,30);
Box myBox2 = new Box(3,6,9);
[Link]();
[Link]();
}
}
Box(double length, double breadth, double
height) { = length;
[Link]
length = length; length is instance
Creates ambiguity for
[Link] = breadth;variable as well as length
breadth = breadth; compiler of
[Link] = height;
height = height; is formal parameter
method
‘this’ Keyword
🞂 this is a reference variable that refers to the current object.
OOP Java is the easiest, scoring and my
favorite subject
Thi
s
OOP Java is the easiest, scoring and my
favorite subject
Inheritance
Inheritance
Derived
Class
Inheritance
class
Person is
OOP Java is the easiest, scoring and my
favorite subject
class Person
Attributes: called
Age, Height, Base
Weight class
Methods:
Talk() , Walk(),
Eat()
class
class Doctor class Footballer
Businessman
Methods: Methods: Methods:
Diagnose() Playfootball() Runbusiness()
These classes
are called
Derived
class
Inheritance
OOP Java is the easiest, scoring and my
favorite subject
class Vehicle
Attributes:
Engine_no,
color
Methods:
apply_breaks()
⮩ When an existing code is reused, it leads to less development and maintenance costs.
🞂 It is used to generate more dominant objects.
🞂 Avoids duplicity and data redundancy.
🞂 Inheritance makes the sub classes follow a standard interface.
Base Class
Derived
Class
OOP Java is the easiest, scoring and my
favorite subject
Implementing
Inheritance
Subclass-Superclass Relationship
Introduction
🞂 Inheritance is the process, by which a class can acquire(reuse)
OOP Java is the easiest, scoring and my
favorite subject
Attributes:
Engineno, color
Methods:
applybreaks()
class A
OOP Java is the easiest, scoring and my
favorite subject
{
Class A
//SuperClass or ParentClass or Base Class
BaseClass
} the keyword “extends” is used to create a
subclass of A
class B extends A
Class B
{ Derived Class
//SubClass or ChildClass or
DerivedClass
}
Implementing Inheritance in java
Person
1. class Person OOP Java is the easiest, scoring and my
favorite subject
name:String
2. { Contact:long
3. String name;
4. long contact;
5. }
2. { name:String
3. String name; Contact:long
4. long contact; dispContact():void
5. public void dispContact()
6. { [Link](“num=”+contact);
7. }
8. }
9. class Employee extends Person
10.{ Employee Customer
11. int empID; empID:int customerID:int
12. String designation; Designation:Stri InvoiceNo:int
13.} ng
[Link] Customer extends Person
15.{
16. int customerID;
17. int invoiceNo;
18.}
OOP Java is the easiest, scoring and my
favorite subject
Property of Inheritance
Property of Inheritance
A class member that has been declared
as private will remain private to its
OOP Java is the easiest, scoring and my
favorite subject
class. It is not
Base class A can’t access accessible by any code outside its class,
attributes and methods of Class A including subclasses.
Derived Class B Base Class
Class B
Derived Class Derived Class B can access
attributes and methods of
Base class A
OOP Java is the easiest, scoring and my
favorite subject
Inheritance by Example
Example1: InheritanceDemo1
A OOP Java is the easiest, scoring and my
favorite subject
#i:int
~j:int
showij():void
B
~k:int
showk():void
add():void
Example1: [Link]
[Link] InheritanceDemo{
1. class A{ [Link] static void main(String[]
OOP Java is the easiest, scoring and my
favorite subject
2. protected int i;
3. int j; args)
4. void showij(){ 18. {
5. [Link]("i="+i+" j="+j);
19. A superObjA= new
6. } A();
7. } 20. superObjA.i=10;
21. superObjA.j=20;
8. class B extends A{ //inheritance
9. int k; 22. B subObjB= new
10. void showk(){ B();
11. [Link]("k="+k); 23. subObjB.k=30;
12. }
13. void add(){ 24. Outp
14. [Link]("i+j+k="+(i+j+k)); [Link](); ut
15. } i=10 j=20
25. [Link]();
k=30
16.} 26. [Link]();
i+j+k=30
27. }
Types of Inheritance in Java
1. Single Inheritance 2. Hierarchical 3. Multilevel Inheritance
InheritanceOOP Java is the easiest, scoring and my
favorite subject
A A A
B C B
B
Single Inheritance B
Single Inheritance: [Link]
[Link] InheritanceDemo{
1. class A{ [Link] static void main(String[]
OOP Java is the easiest, scoring and my
favorite subject
2. protected int i;
3. int j; args)
4. void showij(){ 18. {
5. [Link]("i="+i+" j="+j);
19. A superObjA= new
6. } A();
7. } 20. superObjA.i=10;
21. superObjA.j=20;
8. class B extends A{ //inheritance
A
9. int k; 22. B subObjB= new
10. void showk(){ B();
11. [Link]("k="+k); 23. subObjB.i=100 B
12. } 24. subObjB.j=100;
13. void add(){ 25. Outp
subObjB.k=30;
14. [Link]("i+j+k="+(i+j+k)); ut
15. } i=10 j=20
26. k=30
16.} [Link](); i+j+k=230
27. [Link]();
OOP Java is the easiest, scoring and my
favorite subject
Hierarchical A
Inheritance B C
1. class A{ [Link] C extends A{
2. protected int i; 15. int m;
3. int j; 16. void showm(){
OOP Java is the easiest, scoring and my
favorite subject
27. superObjA.j=20;
28. [Link]();
Multilevel Inheritance
1. class A{ [Link] C extends B{
2. protected int i; 15. int m;
3. int j; 16. void showm(){
OOP Java is the easiest, scoring and my
favorite subject
27. superObjA.j=20; A
28. [Link]();
CubeWeight
# weigth: double
CubeWeight(double, double, double,
double):double
1. class Cube{ [Link] CubeInherit{
2. protected double 23. public static void main(String[]
args) {
OOP Java is the easiest, scoring and my
height,width,depth; 25.
3. Cube(){ CubeWeight(10,10,10,20.5);
4. [Link] cw2= new
[Link]("inside 27.
5. default CubeWeight(100,100,100,200.5);
Constructor:CUBE"); [Link]("[Link]()="
6. }
7.
[Link] double volume(){
CubeWeight extends Cube{ +[Link]());
8. return
[Link] weigth; [Link]("[Link]()="
height*width*depth;
[Link](double h,double w,double d,
9. double m)} +[Link]());
10.}
14.{ 30. }} Outp
ut
CubeInheri
15. [Link]("inside Constructor:
16.
[Link]
inside default
Constructor:CUBE
CUBEWEIGTH"); inside Constructor:CUBEWEIGTH
17. height=h; inside default
18. width=w; Constructor:CUBE
19. depth=d; inside Constructor:CUBEWEIGTH
OOP Java is the easiest, scoring and my
favorite subject
Super Keyword
Super Keyword
🞂 Whenever a subclass needs to refer to its immediate superclass,
OOP Java is the easiest, scoring and my
favorite subject
Cube CubeInherit
# height: double cw1:CubeWeight
# width: double cw2:CubeWeight
# depth: double
main(): void
Using
Cube(double,
“super” volume(): double
double, double)
CubeWeight
# weigth: double
CubeWeight(double, double,
double, double)
1. class Cube{ 22. class CubeInheritSuper{
2. protected double 23. public static void main(String[]
args) {
OOP Java is the easiest, scoring and my
3. } i; main(String[]
args)
4. class B extends A{ 17. {
5. int i,k; 18. B b= new B(12,56);
6. B(int a,int b){ 19. [Link]();
7. super.i=a 20. }
8. ; this.i=b; 21.}
9. }
10. void show(){
11.
Outp
[Link]("super.i="+super.i); ut
12. super.i=12
[Link]("this.i="+this.i); this.i=56
13. }
Using super to access members: [Link]
1. class A{ [Link] SuperMemberDemo{
2. int i=33; 20. public static void
OOP Java is the easiest, scoring and my
favorite subject
3. void show(){ main(String[]
4. [Link]("inside A:i="+i); args)
5. } 21. {
6. } 22. B b= new
B(12,56);
7. class B extends A{ 23. [Link]();
8. int i,k; 24. }
9. B(int a,int b){ 25.}
10. [Link]();
11. super.i=a;
12. this.i=b;
13. }
14. void show(){ Outp
15. [Link]("super.i="+super.i); ut
16. [Link]("this.i="+this.i); inside
17. } A:i=33
18.} super.i=12
B.i=56
Points to remember for super
🞂 When a subclass calls super( ), it is calling the constructor of its
OOP Java is the easiest, scoring and my
favorite subject
immediate superclass.
🞂 This is true even in a multileveled hierarchy.
🞂 super( ) must always be the first statement executed inside a
subclass constructor.
🞂 If a constructor does not explicitly call a superclass constructor,
the Java compiler automatically inserts a call to the no-
argument constructor of the superclass.
🞂 The most common application of super keyword is to eliminate
the ambiguity between members of superclass and sub class.
Why Inheritance
1. Reusability of code OOP Java is the easiest, scoring and my
favorite subject
Class B
Derived Class
OOP Java is the easiest, scoring and my
favorite subject
Access Control
Access Control
Access Description
Modifier
OOP Java is the easiest, scoring and my
favorite subject
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 Control
Access Same Class Same Sub Class
OOP Java is the easiest, scoring and my
favorite subject Universal
Modifier Package
Private
Default
Protected
Public
Exercise
1. Why multiple and Hybrid inheritance is not supported in java.
OOP Java is the easiest, scoring and my
favorite subject
Abstraction
Abstraction
Abstraction Encapsulation
It means act of removing/ It is act of binding code and
OOP Java is the easiest, scoring and my
favorite subject
Implementing
Abstraction
OOP Java is the easiest, scoring and my
favorite subject
Abstract class
Abstract class
🞂 Abstraction is a process of hiding the implementation details from the user, only the functionality will be
OOP Java is the easiest, scoring and my
provided to the user. favorite subject
🞂 In other words, the user will have the information on what the object does instead of how the object will
do it.
🞂 Abstraction is achieved using Abstract classes and interfaces.
🞂 A class which contains the abstract keyword in its declaration is known as abstract class.
⮩ Abstract classes may or may not contain abstract methods, i.e., methods without body ( public void get(); )
⮩ But, if a class has at least one abstract method, then the class must be declared abstract.
⮩ If a class is declared abstract, it cannot be instantiated.
⮩ To use an abstract class, we have to inherit it to another class and provide implementations of the abstract
methods in it.
Abstract class (Example)
3. } favorite subject
🞂 Sometimes, we need to define a superclass that declares the structure of a given abstraction without
OOP Java is the easiest, scoring and my
providing a complete implementation. favorite subject
🞂 The superclass will only define a generalized form, that will be shared by all the subclasses.
🞂 The subclasses will fill the details of every method.
🞂 When a superclass is unable to create a meaningful implementation for a method.
Points to remember for Abstract Class
🞂 To declare a class abstract, you simply use the abstract keyword in front of the class keyword at the
OOP Java is the easiest, scoring and my
beginning of the class declaration. favorite subject
🞂 There can be no objects of an abstract class. That is, an abstract class cannot be directly instantiated
with the new operator. Such objects would be useless, because an abstract class is not fully defined.
🞂 Cannot declare abstract constructors, or abstract static methods.
🞂 Any subclass of an abstract class must either implement all of the abstract methods in the superclass, or
be itself declared abstract.
OOP Java is the easiest, scoring and my
favorite subject
Interface
Interface
public or not
used(default)
Methods are without body(no
OOP Java is the easiest, scoring and my
🞂 If a class includes an interface but does not fully implement the methods defined by that interface, then
OOP Java is the easiest, scoring and my
that class must be declared as abstract. favorite subject
9. [Link]("MyStack1=");
10. for(int i=0;i<5;i++)
11. [Link]([Link]());
12. [Link]("MyStack2=");
3. void method2(); 4. }
4. } 5. public void method2(){
5. interface B extends A{ 6. [Link]("inside
6. void method3(); MyClass1:method2()");
7. } 7. }
8. interface C extends A{ 8.
9. void method4(); 9. public void method3(){
10.} 10. [Link]("inside
1. class InterfaceHierarchy{ 1. MyClass1:method3()");
2. public static void main [Link] MyClass2
} implements C{
2. public void method1(){
3. (String[] 12.}
3. [Link]("inside
args) {
4. MyClass1 c1=new MyClass2:method1()");
MyClass1(); 4. }
5. MyClass2 c2=new 5. public void method2(){
MyClass2(); 6. [Link]("inside
6. c1.method1(); MyClass2:method2()");
7. c1.method2(); 7. }
8. c1.method3(); 8. public void method4(){
9. c2.method1(); 9. [Link]("inside
10. c2.method2(); MyClass2:method4()");
Interface: Points to Remember
🞂 Any number of classes can implement an interface. OOP Java is the easiest, scoring and my
favorite subject
🞂 One class can implement any number of interfaces.
🞂 To implement an interface, a class must create the complete set of methods defined by the interface.
However, each class is free to determine the details of its own implementation.
Abstract class vs. Interface
Abstract class can have abstract and non- Interface can have only abstract methods.
abstract methods.
Abstract class can have final, non-final, Interface has only static and final variables.
static and non-static variables.
An abstract class can extend another Java An interface can extend another Java interface
class and implement multiple Java interfaces. only.
A Java abstract class can have class members Members of a Java interface are public by
like private, protected, etc. default.
OOP Java is the easiest, scoring and my
favorite subject
Packages
Package
🞂 A Package can be defined as a grouping of related types providing access protection and name space
OOP Java is the easiest, scoring and my
management. favorite subject
🞂 Programmers can define their own packages to bundle group of classes/interfaces, etc.
🞂 Packages are used in Java in order to
prevent naming conflicts
control access,
make searching/locating of classes/interfaces easier.
🞂 It is a good practice to group related classes implemented so that a programmer can easily determine
that the classes, interfaces are related.
🞂 Built-in Packages
⮩ Provided by Java API, e.g.:
▪ [Link].*
▪ [Link].*
▪ [Link].*
🞂 User-Defined Packages
⮩ Created by developers to structure project components logically.
import keyword
🞂 import keyword is used to import built-in and user-defined packages into your java source file so that
OOP Java is the easiest, scoring and my
your class can refer to a class that is in another packagefavorite
by subject
directly using its name.
🞂 There are 3 different ways to refer to class/interface that is present in different package
⮩ import the class/interface you want to use.
⮩ import all the classes/interfaces from the package.
⮩ Using fully qualified name.
🞂 We can import a class/interface of other package using a import keyword at the first line of code.
🞂 Example :
import [Link];
🞂 We can import all the classes/interfaces of other package using a import keyword at the first line of code
OOP Java is the easiest, scoring and my
with the wildcard (*). favorite subject
🞂 Example :
import [Link].*;
public class DemoImport {
public static void main(String[] args)
{
Scanner s = new Scanner([Link]);
Date d = new Date();
// Code
}
}
🞂 It is possible to use classes from other packages without importing the class using fully qualified name
of the class.
🞂 Example :
[Link] s = new [Link]([Link]);
Static Import
🞂 The static import feature of Java 5 facilitate the java programmer to access any static member of a class
OOP Java is the easiest, scoring and my
directly. favorite subject
🞂 Advantage : Less coding is required if you have to access any static member of a class more frequently.
🞂 Disadvantage : If you overuse the static import feature, it makes the program unreadable and
unmaintainable.
🞂 To create a package you need to write package statement followed by the name of the package.
OOP Java is the easiest, scoring and my
favorite subject
🞂 Syntax : package package_name;
🞂 Example :
package DS_student;
class Student {
// code
}
🞂 The package statement should be the first line in the source file.
🞂 There can be only one package statement in each source file, and it applies to all types in the file.
🞂 If a package statement is not used then the class/interfaces will be put into an unnamed package.
Package (Example)
package myPackage;
public class Animal { OOP Java is the easiest, scoring and my
favorite subject
WRAPPER CLASS
What is Wrapper Class
int i = 5;
Integer intobj = new Integer(i);
Converting Object Number to Primitive
Numbers
Method Calling Conversion Action
Or
int i = 5;
String str = new String();
str = [Link]();
Converting String Objects to Numeric Objects, using
the Static method ValueOf()
Strings
Strings in Java
• Strings represent a sequence of characters.
• In Java, strings are class objects and implemented using two classes: String and
StringBuffer.
• We can also create and use arrays that contain strings, e.g:
String nameArray[ ] = new String[5];
Methods in String class
Method call Task performed
s2= [Link] Converts string s1 to all lowercase
s2 = [Link] Converts the string s1 to all Uppercase
s2 = [Link](‘x’, ‘y’); Replace all appearances of x with y
s2 = [Link](); Remove white spaces at the beginning and end of the
string s1
[Link](s2) Returns true if s1 is equal to s2
[Link](s2) Returns true is s1 = s2, ignoring the case of characters.
[Link](‘x’) Gives the position of the first occurrences of ‘x’ in the string
s1.
[Link](‘x’, n) Gives the position of ‘x’ that occurs after nth position in the
string s1
String s3 = [Link]();
[Link]("\n After converting string " + s1 + " into lower case, i.e. s3 = " + s3);
String s4 = [Link]();
[Link]("\n After converting string " + s3 + " into upper case, i.e. s4 = " + s4);
Boolean b = [Link](s2);
[Link]("\n strings " + s1 + " and " + s2 + " are equal: " + b);
String s6 = [Link](s2);
[Link]("\nThe strings " +s1+ " and" +s2+ " after concatenation is:" + s6);
String s7 = [Link](3);
[Link]("\n The substring of " + s1 + " after 3rd position is: " + s7);
String Buffer
StringBuffer class