Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+5 votes

Lets say we declared
String word = "";

What does the "" mean?

asked in CSC211_Winter2018 by (1 point)

1 Answer

+1 vote

The quotation marks used indicate that the variable word is of type String, and prevents them from being treated as any other variable type. Similarly, values of type char use single quotation marks to set them apart from other data types.

In this instance, initializing the String word as "" indicates that it is an empty string, but it has a value that can be accessed in memory to be overwritten later. Without this initialization, your program may think that the String word has not been initialized and any operations on your object may fail.

For good measure, always initialize your variables with some value -- the empty string "" will suffice in this case.

answered by (1 point)
0

Good answer, but I wouldn't say that you should always initialize your variables with some value. Sometimes it's better for Java to know that it hasn't been initialized yet, so you get a compiler or run-time error earlier, instead of getting a logic error later.

...