pooja sharma ,bca 2nd year

18
Information Technology Project Report Java Programming Topic Wrapper Class and Nesting Method Submitted By Pooja Sharma Bachelors Of Computer Applications Dezyne E’cole College www.dezyneecole.com

Upload: dezyneecole

Post on 08-Jan-2017

19 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Pooja Sharma ,BCA 2nd Year

Information Technology

Project Report Java Programming

Topic

Wrapper Class and Nesting Method

Submitted By Pooja Sharma

Bachelors Of Computer Applications Dezyne E’cole College

www.dezyneecole.com

Page 2: Pooja Sharma ,BCA 2nd Year

Project Report

On

JAVA Programming

At

Dezyne E’cole College

Ajmer

Submitted to

Dezyne E’cole College

Towards The

Partial Fulfillment on

Bachelors Of Computer Applications

By

Pooja Sharma

Dezyne E’cole College

106/10, Civil Lines, Ajmer

Tel- 0145-2624679

www.dezyneecole.com

2016-2017

Page 3: Pooja Sharma ,BCA 2nd Year

ACKNOWLEDGEMENT

I Pooja Sharma, Student of Dezyne E’cole College, am extremely

grateful to each and every individual who has contributed 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.

Thank You

Page 4: Pooja Sharma ,BCA 2nd Year

SYNOPSIS

This project is a minor project made, based on the theoretical concepts

of JAVA. This project has made our basic concept on JAVA strong.

Page 5: Pooja Sharma ,BCA 2nd Year

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

Page 6: Pooja Sharma ,BCA 2nd Year

P a g e | 2

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

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

Page 7: Pooja Sharma ,BCA 2nd Year

P a g e | 3

//Converting Primitive Numbers to Object Numbers

Page 8: Pooja Sharma ,BCA 2nd Year

P a g e | 4

//Converting Object Numbers to Primitive Numbers

Page 9: Pooja Sharma ,BCA 2nd Year

P a g e | 5

//Converting Numbers to String

Page 10: Pooja Sharma ,BCA 2nd Year

P a g e | 6

//Converting String Object to Numeric Object

Page 11: Pooja Sharma ,BCA 2nd Year

P a g e | 7

//Converting Numeric String to Primitive Numbers

Page 12: Pooja Sharma ,BCA 2nd Year

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.

Page 13: Pooja Sharma ,BCA 2nd Year

P a g e | 9

//Vector without using Autoboxing and unboxing

Page 14: Pooja Sharma ,BCA 2nd Year

P a g e | 10

//Vector with using Autoboxing and unboxing

Page 15: Pooja Sharma ,BCA 2nd Year

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.

Page 16: Pooja Sharma ,BCA 2nd Year

P a g e | 12

Page 17: Pooja Sharma ,BCA 2nd Year

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.

Page 18: Pooja Sharma ,BCA 2nd Year

P a g e | 14