2018-09-20 Strings

posted Sep 20, 2018, 6:05 AM by Konstantinovich Samuel   [ updated Sep 20, 2018, 10:57 AM ]
Goal : Java Strings

Reminder: Google Mentorship Interest Meeting Friday (after 10th in 307)

(Note: You should be paying attention in class, not doing your coding bat homework. I can see the timestamps of when they are completed. It is short sighted of you to to try to "save time" and do this in class, and miss out on details discussed. It is also rude.)

You can't multitask efficiently. Yes even YOU.
This is important, and rather than link you to an article here is a picture. 
If you don't believe it, google it, and you will see it is based on multiple studies (by the APA iirc)

Strings are not primitives, they are Objects. I wanted to give you an example of an object before we learn how to make them. Object types start with a capital first letter.
They contain methods! (You saw python string methods!

Operations that work on strings:
+    concatenation 

You cannot use other math operations.
You cannot use == ,  >, <= etc for comparison.

In class code demo.


Strings documentation page lists all methods:

AP Exam Critical Methods:
substring  : like a python slice
equals  :  determining identical contents
compareTo : for less than/ greater than / equal  comparison

Additional Class Critical Methods:

Escape Sequences:
Special characters sometimes need to go in strings, but cannot be typed. 
We can put them in strings using the backslash to escape the character from being treated like a regular symbol. 
This works in both char and String types.

\n is the newline character, so you can write literals (both char and String) like:  '\n'  ,  "\n" ,  or "fish\nfood"
\t  tab
\'  single quote
\"  double quote
\\ backslash

System.out.println("\\why\no//\"!\'?")  would print:
It has a length of 12 (excludes the escape characters and the double quotes that designate the string.)

Each escaped character counts as 1 unit for the length of the string and charAt and substring treat them as single characters (only we see 2, the views these symbols as 1 thing each)