2

I'm writing a program for encryption, for fun. I ran across something odd, and I'd like some input!

Examine this code:

private static final String primeNumber = "32416190071";
private String stringKey = "";
private String hexKey = "";
private long longKey;

public void example(){
stringKey = primeNumber;
    longKey = Long.parseLong(primeNumber);
}

now, if i put longKey = 32416190071;, i get an error stating that i cant put that big of a number in a long. what am I doing wrong, or what is going on?

Thanks!

Food for thought -_-

PulsePanda
  • 1,806
  • 10
  • 33
  • 56
  • You could have figured this out with a simple search of `java primitives`. First result is [here](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html). – Sotirios Delimanolis Dec 30 '13 at 01:12
  • @SotiriosDelimanolis - The OP figured it out before your comment. And it's not a trivial thing to find, since you've no real idea what the issue is, especially if you're a relative newbie. – Hot Licks Dec 30 '13 at 01:23
  • @HotLicks I see no research effort. Any tutorial on numbers in Java gives us the answer. – Sotirios Delimanolis Dec 30 '13 at 01:26
  • 2
    @SotiriosDelimanolis - How do you even know to search for "java primitives"? How do you, as a relative novice, even realize that the error message is because the format of the (perfectly normal) decimal literal is wrong? There are many, many examples of poor research that appear here every day, but this isn't one of them. – Hot Licks Dec 30 '13 at 01:29
  • @HotLicks On a `javac` compiler, the compilation error message is `integer number too large`. Google that, you get [this](http://stackoverflow.com/questions/3757763/integer-number-too-large-error-message-for-600851475143). On eclipse, the compilation error message is `The literal X of type int is out of range`. You google that, you get [this](http://stackoverflow.com/questions/7093186/the-literal-of-int-xxxxx-is-out-of-range). – Sotirios Delimanolis Dec 30 '13 at 01:32
  • @SotiriosDelimanolis - A `long` is an "integer". The error message would seem (to someone who's not seen it a few time) to be nonsense. – Hot Licks Dec 30 '13 at 01:37
  • @HotLicks Regardless. You get an error message, what is your first reaction? "Java is stupid, doesn't know what it's talking about." or "Here's a technology that probably has been around for a while, maybe I should search this exact error message I'm getting."? – Sotirios Delimanolis Dec 30 '13 at 01:39
  • @SotiriosDelimanolis - That reaction only begins occurring to you after about 2 years of programming. – Hot Licks Dec 30 '13 at 01:40
  • @HotLicks Looking at OP's profile, questions and answers, they seem to have written quite a bit of Java. – Sotirios Delimanolis Dec 30 '13 at 01:42

2 Answers2

3

Figured it out. if I put long test = 32416190071L it works just fine! it's the L...

Thanks anyway!

PulsePanda
  • 1,806
  • 10
  • 33
  • 56
0

As you stated it is neccesary to attach L to the number, this is also the case for floats. When declaring a float you should attach a 'f' to the end of the numbee, like this: 10f. Also it doesn't matter if you use a upper or lower case f or l.

Hope I helped.

Liquid Ink
  • 521
  • 1
  • 5
  • 16