Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Literals: What's the point?

  1. #1
    Member
    Join Date
    Jul 2013
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Literals: What's the point?

    So, I just came upon the concept of "literals," and I'm not getting it. Apparently it's the "source code representation of a primitive data type." In other words, the number you typed in the program. I won't ask why that even needs a name; here's what I'm not getting:

    It says that you can make a number be a long-type, for instance, by adding an L after it. So 30000 is an int, 30000L is a long.

    But if you're declaring a variable, you can already type "long x = 30000." Why would you need the extra letter? Even if you're feeding a truly long number to the program through input, you would have to feed it to a long-type field if it were too big to fit in an int. So why is the L ever needed at all?

    As a matter of fact, if I type "long x = <really big number>" into my IDE, it gives me an error that the "integer value" (even though I just declared it as a long) is too big, until I append an L to the end. And if I declare a huge number appended with L to an int var, it tells me "possible loss of precision" (nevermind the fact that it should be too big for an int var anyway, which it only spits at me if I drop the ending L).

    I know this has to be a stupid question, I can feel it - the answer is going to be very de-mystifying I'm sure, but right now, I don't get the point of all this.

    Thanks a lot,
    -summit45


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Literals: What's the point?

    Literals aren't limited to only numbers. They can be booleans, characters, strings, numbers, null, etc. (not a complete list).

    They're called literals because they represent an absolute value which cannot be interpreted as anything other else. We call them literals because we need a name to refer to what these things are.

    There is a big difference between these:

    int val = '4'; // not actually the integer literal 4, but the character literal '4' which in ASCII is equivalent to 52
    int val2 = 4; // this is the integer literal 4
    int val3 = (int) 4f; // this is the floating point literal 4f. Because floats are not implicitly castable to int, we must explicitly cast it.
    // ...

    There are complicated reasons (some of which is historical) why integer literals and long literals are treated differently, and I won't pretend to know/understand all of it. My guess is that it has something to do with implementation details of the compiler (especially on older 32-bit systems), and possibly has historical reasons coming from C/C++ where ints originally held 16-bit values and longs held 32-bit values.

Similar Threads

  1. Can Anyone Point me in the right direction?
    By ImyMTD in forum AWT / Java Swing
    Replies: 8
    Last Post: May 4th, 2013, 02:29 AM
  2. what are integer literals & where can they come in handy
    By DanielJamesCollier in forum Java Theory & Questions
    Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  3. How to get SQL literals after parsing in mySQL Connector/J ?
    By amughost in forum JDBC & Databases
    Replies: 0
    Last Post: June 21st, 2012, 06:26 AM
  4. [SOLVED] Someone point me to the right direction..
    By ineedhelp in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: June 30th, 2011, 10:03 PM
  5. how do i use point 3d?
    By antrax in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 6th, 2010, 05:13 PM