This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Tricky question.... plz help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Tricky question.... plz help" Watch "Tricky question.... plz help" New topic
Author

Tricky question.... plz help

Poonam Gupta
Greenhorn

Joined: Jun 02, 2005
Posts: 4
hi
please take a look at the following piece of code
class Apps
{
public static void main(String[] args)
{
boolean b= 100>99;
int s='A';
System.out.println(s);


int a=10;
double w=6.5;
System.out.println(w);
double c='A'/a;
System.out.println(c);
System.out.println(b);
}
}
the value of c come out to be 6.0 after the code is run....
my question is that the value of c should be 6.5 as the ascii value of 'A' is 65... why is it returning 6.0 instaed of 6.5
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
I think that what is happening is that 'A' is converted to an int and divided by 10, result is 6. But as the result must be a double, you then get 6.0.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Yes, that's basically it. The result of dividing one integer by another is an integer. It's then converted to a double only because you're storing it in a double variable. You might say

double c='A';
c = c / a;

to get the result you expect.


[Jess in Action][AskingGoodQuestions]
Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
Maybe you should initialize 'A' as a double also.


I'm a Hood Ornament
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
Poonam,

Infact what happens in Java arithmetic calculations is the smaller both the digits which are participating in a calculation are are converted to the datatype with the lower bits.

For example :
1) if u divide Float by int result will be int
2) if u multiply int by double again result will be int.

Cheers,
Rahul


Rahul Juneja
ThoughtClicks - http://techlabs.thoughtclicks.com
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Rahul Juneja:

Infact what happens in Java arithmetic calculations is the smaller both the digits which are participating in a calculation are are converted to the datatype with the lower bits.


Note that float and int are both 32 bits. The rule is more or less the opposite of what you've implied, though: if either argument is a floating-point type, the calculation is done using that type and the result is of that type. The result is an int only if both arguments are of integral type.



For example :
1) if u divide Float by int result will be int


No, it will be float.



2) if u multiply int by double again result will be int.


No, it will be a double.

Folks, please don't post answers to questions as if you know the answer if you're really just guessing. It doesn't help anybody. If you must guess, then please say "I don't know for sure, but I think..." or something like that. Especially given that the first two replies in this thread already answered the question correctly and completely.
Poonam Gupta
Greenhorn

Joined: Jun 02, 2005
Posts: 4
Thanks for replying to my question
Im having a great time learning java
pradeep jaladi
Ranch Hand

Joined: Nov 21, 2004
Posts: 65
Hi ,

If you want the result to be 6.5

double c=(double ) 'A' /a;

Here ( double ) do's the magic for you.


Pradeep.Jaladi
Archies Gomes
Ranch Hand

Joined: Jun 19, 2005
Posts: 34
Hi Poonam
What happens in java actually is that
Whenever any arithmatic operation is involved such as 'A'/6 then the datatype which takes the largest space for storing bits is taken.....In this case of yours both are integers so the result is integer and it is assigned to a varialbe of type double so the output you get is 6.0.....If you want the correct output ie 6.5 then either of the operand has to be of type double ...so that the other operand is also converted to the same largest type and then the operation takes place....So what basically happens is first the operands are viewed and the one which takes larger space becomes the king and rest all operands are converted to this type.
njoi learning!!!.
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Originally posted by Mithun H. Salgar:
Hi Poonam
What happens in java actually is that
Whenever any arithmatic operation is involved such as 'A'/6 then the datatype which takes the largest space for storing bits is taken.....


More precisely the operand which type is lower in the following list is converted to the type of the other operand:


If both operands are char, short or byte, they are both converted to int.

(Float has 32 bits but it is considered larger than long with 64bits.)

If both operands of / are integer types, integer division is used. So value of 65/10 is 6.
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Ah..let ME tell you what REALLY happens...


The future is here. It's just not evenly distributed yet. - William Gibson
Consultant @ Xebia. Sonny Gill Tweets
Poonam Gupta
Greenhorn

Joined: Jun 02, 2005
Posts: 4
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
So Poonam,

How are you doing in Learning Java now. You are welcome with more questions.

Cheers,
Rahul
Poonam Gupta
Greenhorn

Joined: Jun 02, 2005
Posts: 4
Hi Rahul,
G8, Thanks for ur concern. I'll definately come up with more question
Thank u
Bye
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Tricky question.... plz help