Understanding Strings in Java: Moving Words Around

  • Thread starter zzoo4
  • Start date
  • Tags
    Mean
In summary: An uninitialized variable is a variable that has not been initialized by the user, or by a function. In Java and most other programming languages, an uninitialized variable is flagged with an initial value of 0.
  • #1
zzoo4
42
0
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?
 
Physics news on Phys.org
  • #2
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?
 
  • #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?
 
  • #4
zzoo4 said:
Oh What if it's like this??

String name = " ";

I am basically initializing the space right?? Like in between any words??
You are not initializing the space - you are initializing the variable name with a string that consists of one space character.
zzoo4 said:
Like I like sopa. Hello everyone

But how can I use a loop along with this?
This is pretty vague. What do you want to do?
 
  • #5
I still don't get what you mean by initializing.

What if I had to change the orders of the word?
Like
"Hello World" to "World Hello"
 
  • #6
When a variable is initialized, it is given a value for the first time.

Code:
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.
 
  • #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??
 
  • #8
zzoo4 said:
What if i want to initialize the first letter words in anything i type??
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

zzoo4 said:
Are there any good tutorials that can teach me String??
Sounds like you first need to read the opening chapters on variables.

Here's a bit from wiki :

http://en.wikipedia.org/wiki/Uninitialized_variable
 
Last edited:

1. What are strings in Java?

Strings in Java are a sequence of characters that can be used to represent text. They are a data type in Java and are commonly used to store words, phrases, or any other type of textual data.

2. How do you declare and initialize a string variable in Java?

To declare and initialize a string variable in Java, you can use the following syntax: String variableName = "string value"; This will create a string variable named variableName and assign it the value "string value".

3. How can you manipulate strings in Java?

There are various methods available in Java for manipulating strings, such as concatenation, substring, and replacing specific characters or words. These methods can be used to rearrange, modify, or extract information from a string.

4. What is the difference between a string and a string builder in Java?

A string in Java is an immutable object, meaning it cannot be changed once it is created. On the other hand, a string builder is a mutable object that allows for the modification of a string without creating a new object. This can be more efficient when working with large strings.

5. How can you compare strings in Java?

There are two ways to compare strings in Java: using the equals() method and the compareTo() method. The equals() method compares the content of two strings, while the compareTo() method compares the lexicographical ordering of two strings. Both methods return a boolean value indicating whether the strings are equal or not.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
932
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
1K
  • Beyond the Standard Models
Replies
1
Views
172
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top