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

+32 votes

When looking at the Hannaldous code I began testing and I found empty strings (null strings?) to be fine when checking if it is within a - z and A - Z on the ascii table. this doesn't make much sense as null is 0 on the ascii table. any thoughts on this?

asked in CSC285_Fall2018 by (1 point)

2 Answers

+15 votes
 
Best answer

There are three different things you must carefully distinguish between:

  1. A null reference. String s = null; Although the variable s is declared of type String, it doesn't point to any actual String (or Object) in memory. If you were to pass in null for the String, you will get a NullPointerException.

  2. An empty String. String s = ""; This is a valid String object that contains ZERO characters in it. If you iterate through it, you will find that it does not contain any characters that are outside of the valid range... Because you are done iterating through it before you begin! There are 0 characters to look at in it.

  3. A String that contains a null byte character (a char with value 0 from the ASCII table). String s="\u0000"; or String s=""+(char)0; This is unusual, but you can create one if you really want to. If you print s, it won't appear in the terminal, since it is an unprintable character. However, if you print s.length() you will get 1, because this IS a valid string that contains one character -- that character just happens to be a special character that has value 0, and is often used to represent an invalid character.

answered by (3.4k points)
selected by
+14 votes

I think it is because you use a String since you can only compare a char to a-z or A-Z on the ascii table.

answered by (1 point)
...