why long variable is taking integer value?

Reply

Join Date: Jun 2008
Posts: 34
Reputation: ravikiran032 has a little shameless behaviour in the past 
Solved Threads: 0
ravikiran032 ravikiran032 is offline Offline
Light Poster

long variable is taking integer value

 
0
  #1
Jan 2nd, 2009
i have written the following code and it is not executing due to the following error
dec.java:7:integer number is too large :10000000000

code

import java.util.*;
class dec{
public static void main(String args[]){
long dig,store,use,temp,temp1,end;
int count=0,count1=0,ch;
use=1000000000;
end=10000000000;
while(use<end){
dig=use;
for(int i=0;i<10;i++){
temp=dig%10;
dig=dig/10;
ch=(10-i)%10;

store=use;
for(int k=0;k<10;k++){

temp1=store%10;
store=store/10;
if(temp1==ch)
count++;
}
if(count!=temp)
break;
else count1++;
}
if(count1==10){
System.out.println("the number is "+use);
break;
}
use++;
}

}
}

i can't understand why long variable is taking integer value.
Last edited by ravikiran032; Jan 2nd, 2009 at 10:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 53
Reputation: neilcoffey will become famous soon enough neilcoffey will become famous soon enough 
Solved Threads: 6
neilcoffey neilcoffey is offline Offline
Junior Poster in Training

Re: why long variable is taking integer value?

 
0
  #2
Jan 3rd, 2009
If you want a literal to be a long, you need to suffix it with L:

  1. use=1000000000L;

If the literal is small enough to fit in an int, then you can write it as an int (without the "L") and it will automatically be converted to a long. But, as here, if the number is too long for an int, you need to explicitly put the "L". (The general rule is that conversions will happen automatically when the type being converted to uses the same number of bytes or more, and is NOT going from a floating point type to an integer type.)
Last edited by neilcoffey; Jan 3rd, 2009 at 12:47 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,633
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 222
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: why long variable is taking integer value?

 
0
  #3
Jan 3rd, 2009
Try this: Integer

int is 'smaller' than long so it can fit inside a long. If you write this:
double d = 1; wouldn't be correct? But it is like putting an int inside a double. It will 'fit'

Also from the link provided you can find the max integer value that can fit in an int variable:
Integer.MAX_VALUE
Last edited by javaAddict; Jan 3rd, 2009 at 3:23 am.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC