Google+

40. 'long' data type








Lets first compute the distance light travels using the 'int' data type to find out its drawbacks.

Computing the distance light travels using 'int' data type: 

class Light
{
    public static void main(String args[])
    {
         int lightspeed, days, seconds, distance;
     
         lightspeed = 186000;  // Approximate speed of light in miles per second
         days = 1000;

         seconds = days*24*60*60; // days x 24 hours x 60 minutes x 60 seconds
         distance = lightspeed*seconds; // compute distance

         System.out.print("In " + days);
         System.out.print(" days light will travel about ");
         System.out.println(distance + " miles.");
     }
}

Output of this program:

In 1000 days light will travel about -1367621632 miles. (Wrong output)

Reason for wrong output:

The int data type can hold that values of lightspeed, days and seconds variables (You can checkout by printing their values in the program and verify them using your system calculator), but int data type was not long enough to hold the value of distance variable. 




Lets try the above program with 'long' data type and find out how it resolves the above problem

Computing the distance light travels using 'long' data type: 

class Light
{
    public static void main(String args[])
    {
        long lightspeed, days, seconds, distance;
     
         lightspeed = 186000;  // Approximate speed of light in miles per second
         days = 1000;

         seconds = days*24*60*60; // days x 24 hours x 60 minutes x 60 seconds
         distance = lightspeed*seconds; // compute distance

         System.out.print("In " + days);
         System.out.print(" days light will travel about ");
         System.out.println(distance + " miles.");
     }
}

Output of this program:

In 1000 days light will travel about 160700000000 miles (Correct output)








Please comment below to feedback or ask questions.

How to use the 'float' data type will be explained in the next post.




12 comments:

Unknown said...

in line7 (lightspeed = 186000;)
";" is missed

Arun Motoori said...

@Raghavan V - Updated. Thank You very much :)

Unknown said...

In post#39 , in comment section Arun mentioned to we need to add the letter L at the end of the literal.when ever variable data type is long (i.e. long count = 9223372036854775807L).

But when I am seeing the example given my him in this post #40 he has not used Literal L as the end of value. I feel literal "L" should be used at the end of value assigned to Lightspeed and days variable ? Eg ( int lightspeed, days, seconds, distance;

lightspeed = 186000; // Approximate speed of light in miles per second
days = 1000 )

Arun Motoori said...

@Preeti - First of all long type variable can be assigned with either integer value (i.e. without L ) or long values (with L). Hence long var1 = 1234; is a valid statement and also long var1 = 1234L; is also a valid statement. 1234 is treated as integer literal which can be assigned to long type variable and also 1234L is treated as a long type literal which can be assigned to long type variable. But when you are assigning a value say 9223372036854775807 without L, the compiler tries to treat it as a integer but unfortunately its out of range of integer data types range i.e. –2,147,483,648 to 2,147,483,647. Hence an error will be displayed as 'Integer number too large: 9223372036854775807'. To overcome that we can add L at its end to tell the compiler that it is a long type value who's range is –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 and as 9223372036854775807L is in the range of long data types range, the compiler wont give any error like 'Long numer too large'. Hope you understood :)

Unknown said...

Thanks Arun!! , needless to say .. your blog is one of the best till date and it has given me a hope that i'll learn Java and selenium :)

Unknown said...

Hi Arun,
I have tried this program using long data type only for distance and used int for all other variables but still the output is wrong. Can you please tell me why?

Unknown said...

@ Ravi,
Try below code.. its working

public class Light {

public static void main(String[] args) {
long lightspeed,days,seconds,distance;
lightspeed=186000;
days=1000;
seconds= 24*60*60;
distance=lightspeed*seconds;
System.out.print("In "+ days);
System.out.print(" days light will travel about");
System.out.println(distance+ " miles");

}

}

Unknown said...

@Ashwini: Ravi's query is diiferet:

He want to use 'long' data type only for variable 'distance' and for others 'int' is sufficient.

I have tried this logic and getting wrong output.

Asha said...

@Ravi- we should use long for seconds variable also. Then we get correct output.

Arun Motoori said...

Yes, as Asha said we have to declare seconds variable as long type.

This logic you can understand after understanding the Java literals concepts.

The below will happen if we don't declare the seconds variable with long data type:

seconds variable will hold the integer value 86400 and lightspeed variable will also hold the integer value 186000. Integer value 86400 gets multiplied with integer value 186000 and the result of this will result in a number which is out of range of int type value. With multiplication you wont get out of range errors, hence some value say -1109469184 is getting assigned to the distance variable which is of long type.

Hence declaring seconds variable as long type, the result of multiplication will be long * int and will result in long type value which will be in range and wont be converted to negative values like above and hence will get correct output.

This concept may be confusing. Please refer posts on Java literals in this blog before to get more clarity on this comment.

PratikshaK said...

Hi Arun - Your blog is very helpful for new learner.
Just had a small query, is JAVA case sensitive? Do we need to use 'String arg[]' with upper case S only or can we use it as with lower case s as well (i.e 'string args[]') ?

Arun Motoori said...

@pratishak yes, java is case sensitive.