| | |
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 |
account android applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer homework html ide image inheritance integer integration intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux loop method midlethttpconnection migrate mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program property ria scanner server set sharepoint smart sms smsspam sourcelabs splash sql sqlite subclass support swing testautomation textfield threads tree trolltech unlimited utility windows






