the double keyword

13
Use of The double Keyword:   The double is a Java keyword that may not be used as identifiers i.e. you cannot declare a variable or class with this name in your Java program. It is used to declare a variable, an expression or a method return value of type double-precision floating-point number. It is also a java primitive data type that represents 64-bit floating-point value. It ranges from 4.94065645841246544 e-324d to 1.7976931348623 1570e+308d (positive or negative). For decimal values, this data type is generally the default choice. The Java wrapper class associated with the double data type is called Double that is defined in java.lang package.  To declare a variable used to hold such a decimal number, you can use the double keyword.  The syntax of declaring a double type variable is shown as: double <variable-name> = <float-value>; For example: double d = 667.60;  The default value for the double data types is 0.0d. Here is an example: public class DoubleDemo { public static void main(String[] args) { double area = 6.15; System.out.print("Area: "); System.out.print(area); }

Upload: maddysurya

Post on 10-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

8/8/2019 The Double Keyword

http://slidepdf.com/reader/full/the-double-keyword 1/13

Use of The double Keyword:

   The double is a Java keyword that may not be used asidentifiers i.e. you cannot declare a variable or class with

this name in your Java program. It is used to declare avariable, an expression or a method return value of typedouble-precision floating-point number. It is also a javaprimitive data type that represents 64-bit floating-pointvalue. It ranges from 4.94065645841246544e-324d to1.79769313486231570e+308d (positive or negative). Fordecimal values, this data type is generally the defaultchoice. The Java wrapper class associated with the doubledata type is called Double that is defined in java.langpackage.

 To declare a variable used to hold such a decimal number,you can use the double keyword.

 The syntax of declaring a double type variable is shownas:

double <variable-name>= <float-value>;

For example:

double d = 667.60;

 The default value for the double data types is 0.0d.

Here is an example:

public class DoubleDemo {

public static voidmain(String[] args) {

double area = 6.15;

System.out.print("Area:");

System.out.print(area);

}

8/8/2019 The Double Keyword

http://slidepdf.com/reader/full/the-double-keyword 2/13

}

Use of "this" keyword in java:

The keyword this is useful when you need to refer toinstance of the class from its method. The keyword

helps us to avoid name conflicts. As we can see in theprogram that we have declare the name of instance variable

and local variables same. Now to avoid the conflictionbetween them we use this keyword. Here, this section

provides you an example with the complete code of theprogram for the illustration of how to what this keyword is

and how to use it.

In the example, this.length and this.breadth refers to theinstance variable length and breadth while length and

breadth refers to the arguments passed in the method. Wehave made a program over this. After going through it you

can better understand.

Here is the code of the program:

class Rectangle{int length,breadth;

void show(int length,int breadth)// local variables{

this.length=length; // instance variablethis.breadth=breadth; // instance variable

}int calculate(){

return(length*breadth);}

}

public class UseOfThisOperator{public static void main(String[] args){

Rectangle rectangle=new Rectangle();rectangle.show(5,6);int area = rectangle.calculate();

  System.out.println("The area of a Rectangle is : " + area);

8/8/2019 The Double Keyword

http://slidepdf.com/reader/full/the-double-keyword 3/13

}}

1. Whenever we are creating an object, the copies of theinstance variables are created for each object.For example in the following example I am creating twoobjects called td and td1.And in the class there are two instance variables called int aand int b.Now what I am saying is that there will be two separateobjects and for those separate objectsthere will be separate instance variables int a and int b

 This diagram elaborates more

Now that you got this we will move to our pointnow when we use this keyword we are referring to thecurrent objectnow look at the programUsing this keyword with constructors

view source 

8/8/2019 The Double Keyword

http://slidepdf.com/reader/full/the-double-keyword 4/13

/* A program to demonstrate the use of thiskeyword

*/

classapple

{

int a =0;

int b =0;

apple(int x, int

y)

{

this.a = x; // instancevariable 

this.b = y; // instancevariable 

}

public static void main(String []args)

{

apple td = newapple(10,12);

apple td1 = new apple(100,23);

System.out.println(td.a); // prints 10

System.out.println(td.B); // prints 12

System.out.println(td1.a);// prints 100

System.out.println(td1.B);// prints 23

8/8/2019 The Double Keyword

http://slidepdf.com/reader/full/the-double-keyword 5/13

}

}

when we compile and run the program we get the output as

Quote

F:\Java\Concepts\DeclarationAndAccessControl>java

 ThisDemo1101210023

Now we will look step by step

• When we created the object td then the constructorwas called for td

•  The respective values are passed to the copies of theinstance variables of that object (as shown in thefigure)

• In the constructor using this.a = x and this.b = y, wecopied the values into the instance variables

• using System.out.println() statements we confirmedthat those values referred to respective objects

Chaining of constructors using this keywordNow what is chaining?this is nothing but calling of one constructor from otherconstructor.

 This is possible by using this keywordNow look at the following program

8/8/2019 The Double Keyword

http://slidepdf.com/reader/full/the-double-keyword 6/13

view source 

print?

01/* A Program to demonstrate the use of This

keyword02 Developed By : Prasad kharkar

03 */

04 class ThisDemo

05 {

06public

 ThisDemo()

07 {

08this(10)

;

09System.out.println("First

Constructor");

10 }

11 public ThisDemo(int a) // overloaded constructor

12 {

13 this(10,20);

14System.out.println("Second

Constructor");

15 }

16

17 public ThisDemo( int a, int B) // another overloadedconstructor

18 {

19 this("Prasad");

20 System.out.println("Third

8/8/2019 The Double Keyword

http://slidepdf.com/reader/full/the-double-keyword 7/13

Constructor");

21 }

22 public ThisDemo(String s) // and still another

23 {

24System.out.println("Fourth

Constructor");

25 }

26

27

28 public static void main(Stringargs[])

29 {

30ThisDemo first = new ThisDemo(); // one object should

be created so that we can access other constructosthrough the constructor which is called first

31 }

32 }

 This is interestingNow this is the output of the program

Quote

F:\Java\Concepts\DeclarationAndAccessControl>java ThisDemo

Fourth Constructor Third ConstructorSecond ConstructorFirst Constructor

now what happened here?let us go step by step

8/8/2019 The Double Keyword

http://slidepdf.com/reader/full/the-double-keyword 8/13

• When the object is created then the respectiveconstructor(No argument constructor) gets called

•  The control comes to the first line in the constructorwhere it encounter with "this(10)" statement.

Hence it calls the constructor that accepts one integerargument.• Again the control comes to the this(10,20) statement

and again it jumps to the constructor which takes twointeger arguments.

• Again the same thing, I need not explain it anymore,you will get bored.

• Now the control is in the constructor which takes stringargument.

• here there is no this keyword .• hence the constructor gets executed and control goes

back to the calling constructor•  This process continues till the first this keyword was

encountered.

Use of super keyword in java:

when extending a class, lets call it Parent class.

Using the "extends" keyword, youcreate a new subclass, lets call it Child class, of the extendedclass.

Note that I will use These Parent and Child classes for therestof my tutorial. so pay attention!View source 

1public class Parent{

2 //the "superclass"

3 }

8/8/2019 The Double Keyword

http://slidepdf.com/reader/full/the-double-keyword 9/13

4public class Child extendsParent{

5 //the subclass

6 }

Using super inside overridden methods

Suppose Parent class had a method to print somethinginside it,called printMethod()View source 

1public class Parent{

2 public void m1(){

3System.out.println("I am aParent");

4 }5 }

After extending the Parent class, Child class automaticallygets them1() of Parent class.However, you decided that you want to override the m1()of Parent, and assign it a new functionality in Child class.view source 

print?

1public class Child extends Parent{

2 public void m1(){

3 System.out.println("I am a

8/8/2019 The Double Keyword

http://slidepdf.com/reader/full/the-double-keyword 10/13

Child");

4 }

5 }

Ok! everything is fine now!if you turn this program:view source 

print?

1 //inside the child class add

2public static void main(Stringargs[]){

3Child v = newChild();

4 v.printMethod();

5 }

the output of this program would be: The print Method of Child.

Great!what is the problem? it works fine!actually, there is no problem. only, what happens if you decided that you dont want to change completely theoriginal printMethod() of Parent.you actually want to Add it a new functionality.so in order to use the original print method of Parent.

 You will have to create a new instance of Parent. right?

Not really. i mean, you could, but it would be a bad design.

Instead, you could use the keyword super!

ok. to be able to use the printMethod of Parent inside theChild classwe simply have to add the super keyword before the method

8/8/2019 The Double Keyword

http://slidepdf.com/reader/full/the-double-keyword 11/13

name. just like that:

view source 

1super.printMethod();

ok. How it is seen inside the code?View source 

01 //the new child class

02 public class Child extends Parent{

03 public void printMethod(){

04 super.printMethod();//here is the super keyword

05System.out.println("The print method of Child");

06 }

07 public static void main(Stringargs[]){

08 Child v = new Child();

09v.printMethod();

10 }

11 }

the output of this code will be:

 The print method of Parent The print method of Child

 That's it. As simple as that.

8/8/2019 The Double Keyword

http://slidepdf.com/reader/full/the-double-keyword 12/13

Using super inside Constructors

suppose the Parent class i mentioned before had that

constructor:View source 

1public class Parent{

2 private int x;

3public Parent(int newX){

4 this.x = x;

5} 

6 }

 To invoke the constructor of Parent inside Child class,we will use the super keyword again as following.View source 

1 public class Child extendsParent{

2 public Child(int x){

3 super(x);//using the Parent class constructor.

4 }

Important!

1.in the constructor, super must be located the first line!2.the number of parameters super gets depend on theparameters that the constructor of the Parent class gets.if it had an empty constructor, super will be used as:super();if it had 2 parameters, you use it that way:super(parameter1, parameter2);

8/8/2019 The Double Keyword

http://slidepdf.com/reader/full/the-double-keyword 13/13

and so on.