Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: What does this mean?

  1. Dec 7, 2011 #1
    This isn't really a homework but
    We are learning about Strings in Java. And I don't get it? (was absent for a day and teacher is not available due to family issues)

    What does this mean or do??
    String name= ""; //???

    and if I have a 3 words like "I like pie"
    Why do I use String to move it those words around?? What is String?
  2. jcsd
  3. Dec 9, 2011 #2


    User Avatar
    Gold Member

    strings are a type of variable that stores a number of letters in a specific order.

    looking at

    String name = "";

    String - specifies the type of variable you are declaring
    name - you are calling this variable 'name'
    equal sign - the stuff after the equal sign will be assigned to the the variable 'name' and that stuff will be a string
    "" - this presents the letters that will be stored to your variable. In order for the compiler to determine a string (the date you are storing) from a variable name, all strings have these quotation marks around them. In this case there is nothing in these quotations marks so you are storing an empty string (0 characters) to the variable you called "name."

    does this make sense?
  4. Dec 9, 2011 #3
    Oh What if it's like this??

    String name = " ";

    I am basically initializing the space right?? Like in between any words?? Like I like sopa. Hello everyone

    But how can I use a loop along with this?
  5. Dec 10, 2011 #4


    Staff: Mentor

    You are not initializing the space - you are initializing the variable name with a string that consists of one space character.
    This is pretty vague. What do you want to do?
  6. Dec 11, 2011 #5
    I still don't get what you mean by initializing.

    What if I had to change the orders of the word?
    "Hello World" to "World Hello"
  7. Dec 11, 2011 #6


    Staff: Mentor

    When a variable is initialized, it is given a value for the first time.

    Code (Text):

    int num;
    int val = 5;
    Here the variable num is uninitialized, and val has an initial value of 5.

    Since you are working with the String class, you should look to see the methods that are available - see http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html.

    One way to switch the order of words in a string would be to:
    1) Find the position of the space character in the string - the indexOf could be used.
    2) Use the substring method to extract the first word of the string and the second word.
    3) Create a new string built up from the substrings you found in the previous step.
  8. Dec 11, 2011 #7
    What if i want to initialize the first letter words in anything i type??

    Are there any good tutorials that can teach me String??
  9. Dec 11, 2011 #8


    User Avatar
    Gold Member

    You don't initialize letters or words, you initialize variables. Varaibles are just buckets to hold things. You are telling the program to allot some memory space and assign it a label (num), though you have not told it what to put in that space yet.

    "int num" says:

    - set aside some memory for a value
    - label this memory 'num' so I can refer to it later
    - the value it will contain is yet to come
    - but it will be an integer

    Sounds like you first need to read the opening chapters on variables.

    Here's a bit from wiki :

    Last edited: Dec 11, 2011
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook