1

I have read a section about literals and how they are used. For example you would add l or L at the end of a number to denote that the int is a long type. Same goes with the floating point numbers (f or F and d or D). What is the point of distinguishing this character at the end of a number. When we give the type does it not already know why specify again? So, what exactly is the point of adding this character to the end of a number? Where is it being used?

I have tried searching these questions on google and stack overflow already, but have not been able to receive a well developed answer to it.

user2522055
  • 159
  • 2
  • 13

2 Answers2

3

The compiler knows how to treat the final result of an expression, but the individual literals (or variables) in an expression each have their own types, as do the intermediate values in the expression. For example:

static final double HALF = 1 / 2;

will actually have the value zero, because the literal 1 and 2 are int-typed, the division is an integer division that produces an int value of zero, and then Java turns that into a double. In contrast, in the expression

static final double HALF = 1d / 2;

the compiler knows to treat the first value as a double. The second value is promoted, Java does floating-point division, and the result is 0.5 as expected.

There are other situations, like autoboxing, where the compiler may treat equivalent values differently depending on their types; when passed to a method that takes an Object, 0 is an Integer, while 0L is a Long. This sort of situation occurs frequently in persistence/DAO code. Also, when compiling, the compiler evaluates compile-time constant expressions and puts their values directly into the code. The values of float and double expressions may be different because of the differing precisions of the types, and so you need to specify if you want a floating-point expression treated as just a float instead of as a double (the default).

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • Thank you for response. If I am interpreting your answer correctly you are saying that for the first example the purpose is instead of saying 1.0 / 2 you can use the d or D literal instead of actually changing to 1.0? Also in your second description you noted that the main purpose of using literals is to define them in code based on your requirements of the program so when you want them either as a float or a double you can specify them directly so that the compiler will know what kind of precision of the value to return as your answer? – user2522055 Dec 17 '13 at 05:25
  • @user2522055 ...your phrasing is a little unclear, but I think yes to both. – chrylis -cautiouslyoptimistic- Dec 17 '13 at 05:31
1

You can not use a value greater than 2147483647 anywhere in java unless you have appended L or D to it. l makes the literal long type and d makes the literal double type.

e.g. System.out.println(2147483648); will result in an error as well as long num=2147483650; will also result in an error as these literals are no longer int type which the default type for the integers. So by appending l or d to a number you can use it as int or double type.

Vijay
  • 213
  • 1
  • 9
  • If you knew that the number was going to be larger than 2147483647 could you not just write long x = 2147483648 ? I see why they have the literal, but I don't understand the use. If there was no way of doing my example that I showed by just assigning the variable x with type long then it makes sense when we assign the type int to x we have to denote that x will be a long if greater. @vijay – user2522055 Dec 17 '13 at 05:45
  • long x=2147483648; will result in error as 2147483648 is neither int nor long. you will get the error as integer number too large. so by appending l to it you are able to assign it to a long variable. – Vijay Dec 17 '13 at 05:49
  • long x=2147483648; long type does not actually work then correct? The only way long numeric int type works is then by inserting the literal to the end of the number? – user2522055 Dec 17 '13 at 05:59
  • long x=2147483647 ( any number in the range of int) will work as in this case type promotion is taking place. where as when the number exceeds int limit then it is neither int nor long and it must be marked as long by appending l to it so that it can be assigned to a long type variable. – Vijay Dec 17 '13 at 06:07