none
What is String Literal RRS feed

  • Question

  • Can you tell me what the difference between a string literal and a normal string (Data Type)
    Sunday, December 28, 2014 9:34 PM

Answers

  • A string literal is someting in the code that is in quotes likw "hello world"

    Consider the line

    data = "hello world"

    data is a variable that contains the string "hello world"

    "hello world" is a string literal - it cannot change it is fixed in the code, but data is a string variable (it is variable - can be changed) for examle

    data = "hello world"

    data = data+" everyone"

    data is now equal to "hello world everyone"

    Sunday, December 28, 2014 9:47 PM
    Moderator
  • Usually (always) a string literal will be inside quotes - depending on the language I would need to see the example where it isn't to see exactly what it is.  Basically a literal is constant in the code and a variable can be changed as the program runs.

    If you put a variable name inside quotes then it becomes a literal, not a variable - so you cannot just put a variable in quotes.  You cannot assign anyting to a literal.

    data = "hello world"  - this makes sense (set the variable data to "hello world")

    "data" = "hello world" - this is wong and shouldn't compile at all.

    Sunday, December 28, 2014 10:59 PM
    Moderator

All replies

  • A string literal is someting in the code that is in quotes likw "hello world"

    Consider the line

    data = "hello world"

    data is a variable that contains the string "hello world"

    "hello world" is a string literal - it cannot change it is fixed in the code, but data is a string variable (it is variable - can be changed) for examle

    data = "hello world"

    data = data+" everyone"

    data is now equal to "hello world everyone"

    Sunday, December 28, 2014 9:47 PM
    Moderator
  • HI, That was really useful, thanks,

    But one thing, How come some Strings are not in " "  I have seen some string in the past but the variable did not contain " "  . would it make a difference if I did not add " " 

    Sunday, December 28, 2014 10:05 PM
  • Usually (always) a string literal will be inside quotes - depending on the language I would need to see the example where it isn't to see exactly what it is.  Basically a literal is constant in the code and a variable can be changed as the program runs.

    If you put a variable name inside quotes then it becomes a literal, not a variable - so you cannot just put a variable in quotes.  You cannot assign anyting to a literal.

    data = "hello world"  - this makes sense (set the variable data to "hello world")

    "data" = "hello world" - this is wong and shouldn't compile at all.

    Sunday, December 28, 2014 10:59 PM
    Moderator