| | |
why long variable is taking integer value?
![]() |
•
•
Join Date: Jun 2008
Posts: 34
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Dec 2008
Posts: 53
Reputation:
Solved Threads: 6
If you want a literal to be a long, you need to suffix it with L:
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.)
Java Syntax (Toggle Plain Text)
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.
Try this: Integer
int is 'smaller' than long so it can fit inside a long. If you write this:
Also from the link provided you can find the max integer value that can fit in an int variable:
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
![]() |
Similar Threads
- e-learning (PHP)
- Knights Tour Problem (C++)
- int(variable) giving illogical numbers (C++)
- Pointers (archived tutorial) (C++)
- Help With syntax (Visual Basic 4 / 5 / 6)
- Integer overflow in vb.net 2005 (VB.NET)
- {Urgent} need help with file compression and decompression (C++)
- Pointers (C++)
Other Threads in the Java Forum
- Previous Thread: please someone help me
- Next Thread: youngest person to pass the SCJA certification?
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui health html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list login loops mac main map method methods mobile netbeans notdisplaying number online printf problem program project properties qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor






