Daksh Sharma ,BCA 2nd Year

17
INORMATION TECHNOLOGY Project Report Java Programming TOPIC Wrapper Class and Nesting Method Submitted by Daksh Sharma Bachelor of computer application Dezyne E’cole College www.dezyneecole.com

Transcript of Daksh Sharma ,BCA 2nd Year

INORMATION TECHNOLOGY

Project Report Java Programming

TOPIC Wrapper Class and Nesting Method

Submitted by Daksh Sharma

Bachelor of computer application

Dezyne E’cole College

www.dezyneecole.com

P a g e | 1

Project Report

On

Java programming

At

Dezyne Ecole College

Towards the

Partial fulfillment of

Bachelor of computer application

By

Daksh Sharma

Dezyne E’cole College

106/10, civil lines, Ajmer

Tel- 0145-262479

www.dezyneecole.com

2016

ACKNOWLEDGEMENT

I Daksh Sharma, student of Dezyne E’cole College,

an extremely grateful to each and every individual

who has contribute in successful completion of my

project. I express my gratitude towards Dezyne

E’cole College for their guidance and constant

supervision as well as for providing the necessary

information and support regarding the completion

of project.

SYNOPSIS

This project is a minor project made based on

the theoretical concepts of java this project has

made our base concepts on JAVA strong

P a g e | 1

Wrapper Class As pointed out earlier, vectors cannot handle primitive data types like int, float, char and double.

Primitive data types may be converted into object types by using the wrapper classes contained

in the java.lang package. Following table shows the simple data types and their corresponding

wrapper class types.

Wrapper Classes for Converting Types Simple Types Wrapper Class

Boolean Boolean

Char Character

Double Double

Float Float

Int Integer

Long Long

The Wrapper class have a number of unique methods for handling primitive data types and

objects. They are listed in the following tables.

Converting Primitive Numbers to Object Number Using Constructor

Method Constructor Calling Conversion Action

Integer IntVal=new Integer(i); Primitive integer to Integer Object

Float FloatVal=new Float(f); Primitive float to Float Object

Double DoubleVal=new Double(d); Primitive double to Double Object Long LongVal=new Long(l); Primitive long to Long Object

Converting Object Numbers to Primitive Numbers Using typeValue()

Method Method Calling Conversion Action

int i=IntVal.intValue(); Object to Primitive Integer

float f=FloatVal.floatValue(); Object to Primitive float

long l=LongVal.longValue(); Object to Primitive long

double d=DoubleVal.doubleValue(); Object to Primitive double

Converting Numbers to String Using toString() Method Method Calling Conversion Action

P a g e | 2

Str=Integer.toString(i); Primitive integer to string

Str=Float.toString(f); Primitive float to string

Str=Double.toString(d); Primitive double to string

Str=Long.toString(l); Primitive long to string

Converting String Objects to Numeric Objects Using the Static Method

ValueOf() Method Calling Conversion Action

DoubleVal=Double.ValueOf(str); Converts string to Double object

FloatVal=Float.ValueOf(str); Converts string to Float object

IntVal=Int.ValueOf(str); Converts string to Int object

LongVal=Long.ValueOf(str); Converts string to Long object

Converting Numeric String to Primitive numbers Using Parsing Methods Method calling Conversion Action

Int i=Integer.parseInt(str) Converts String to Primitive Integer

Long l=Long.parseInt(str) Converts String to Primitive Integer

//Converting Primitive Numbers to Object Numbers

P a g e | 3

//Converting Object Numbers to Primitive Numbers

P a g e | 4

P a g e | 5

//Converting Numbers to String

//Converting String Object to Numeric Object

P a g e | 6

//Converting Numeric String to Primitive Numbers

P a g e | 7

P a g e | 8

Autoboxing and Unboxing The Autoboxing and Unboxing feature, introduced in J2SE 5.0, facilitates the process of

handling primitive data types in collections. We can use this feature to convert primitive data

types to wrapper class types automatically. The Compiler generates a code implicitly to convert

primitive data types to the corresponding wrapper class type and vice versa.

For example, consider the following statement

Double d=98.42;

Double dbl=d;

How, the java compiler provides restrictions to perform the following conversions:

Convert from null type to any primitive type.

Convert to the null type other than the identify conversion.

Convert from any class type c to any array type if c is not object.

P a g e | 9

//Vector without using Autoboxing and unboxing

P a g e | 10

//Vector with using Autoboxing and unboxing

P a g e | 11

Nesting of Methods: We discussed earlier that a method of a class can be called only by an object of that class(or

class itself, in the case of static methods) using the dot operator. However, there is an

exception to this. A method can be called by using only its name by another method of the

same class. This is known as nesting of methods.

Program illustrates the nesting of methods inside a class.

The class nesting defines one constructor and two methods, namely largest() and display(). The

method display() calls the method largest() to determine the largest of the two numbers and

then display the result.

P a g e | 12

P a g e | 13

Another Example:

A method can call any number of methods. It is also possible for a called method to call another

method. That is, method1 may call method2, which in turn may call method3.