Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Tolls

Thread: Literals and variables

  1. #1
    sherifhanna82 is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Question Literals and variables

    Hello everyone,

    I am "New to Java" and have a VBA background. I bought 2 books on Java and am looking forward to writting my first program/Mobile App.

    I am a bit confused about the literals and variable concept and it might have to do with my very recent introduction to VBA.

    That said, here's my dilemma:

    In VBA, once I declare a varialbe (Dim Age As Long) VBA know that this is Long and processes the variable as such. However, in Java, it looks like I have to suffix a long with an L or a 1 even though the Syntax states "Long X = 23452312;". Why do I need an L to indicate that the variable "X" in a long, when it's already declared as a long on the left side of the statement? Are variable types independent of the literal types in Java? If so, can someone please elaborate on that concept.

    I appreciate all the help you provide and I am looking forward to a rich learning experience in the Java language.

    Best regards,

    S.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Literals and variables

    The java compilers sees numbers without decimals as an integer data type unless specified differently...given an int and long are different data types, specifying the L lets the compiler know otherwise. For instance,
    Java Code:
    long myLong = 2341242341123412;//should throw a compile time error
    ...
    long myLong = 2341242341123412L;//lets the compiler know the number is a long
    The same can be said for other primitive data types (for instance float and double).

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: Literals and variables

    Quote Originally Posted by sherifhanna82 View Post
    I have to suffix a long with an L or a 1
    L or l -- NOT 1

    Repeating that in code tags:
    Java Code:
    L or l -- NOT 1
    The uppercase L is recommended to avoid just such ambiguity.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default Re: Literals and variables

    Quote Originally Posted by sherifhanna82 View Post
    Why do I need an L to indicate that the variable "X" in a long, when it's already declared as a long on the left side of the statement?
    You're not telling it that 'X' is a Long, since as you say it already knows that.
    You're telling it that the literal number on the right is a long.
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    sherifhanna82 is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: Literals and variables

    Thanks y'all. I appreciate the thorough explanation; this makes sense.

Similar Threads

  1. Replies: 3
    Last Post: 02-14-2012, 10:45 PM
  2. I can only print string literals
    By leeavital in forum New To Java
    Replies: 1
    Last Post: 12-11-2009, 01:20 AM
  3. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  4. Variables
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 12:44 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •