Java: Variables: long

In this tutorial I will explain the long. To do that I will use this file.


package net.tutorial.src;

public class Tutorial 
{
public static void main(String[] args)
{
System.out.println(“Hello World!”);
}
}


Like all other variables a long has to be created somewhere inside of a class.
A long is a number variable used in Java. Just like the int it can only save whole numbers like 2, 8 or 16. Numbers like 4,5 or 6,027492 can not be saved in a long.
The reason why people want to use a long is that it can store insanely high numbers. It can store from -2^63 up to 2^63 -1. The highest number can also be written as 9,223,372,036,854,775,807 which should be enough for almost anything you will ever need in programming. A bad part about the long is that it is a lot slower than other data types like ints, bytes and shorts so you should only use it when you really have to.
If you want to use a long in java you need to code it like this.


long number = 6;


The first part in this line is the type of variable. In this case an long.
The second part in this line of code is the name of the variable. In this case number.
The last part of this line of code is the thing number will be. In this case number will be 6.
If you want to print out a number in the console you have to use a line of code like this.


System.out.println(number);


Because this bit of the code is in the main method and the main method is static you have to add the word static in front of long. Why that is and how you should use that is something for a later tutorial though.


package net.tutorial.src;

public class Tutorial 
{
static long number = 6;

public static void main(String[] args)
{
System.out.println(number);
}
}


 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">