Strings and Chars Flashcards
What are Strings?
A data type used for storing text; a collection of characters that maintain their order
What characters are valid in a string?
All characters are valid, but to get certain characters you must use escape sequences (eg \n)
What are the escape sequences?
”: "
:
new line: \n
tab: \n
and more
In Java, what types can be turned into a String?
All types
What is concatenation?
Adding up strings to make a longer string using the + or += symbols
How does the compiler decide when to do addition or concatenation?
The operation a + b only means addition if both a and b are numbers of some kind. Otherwise, if the other side is not a String, it is converted to one
What are objects?
Data types that begin with a capital letter; larger chunks of data made up of the primitive data types
What is stored on the stack when you use objects? Why?
A reference to the data’s location on the heap. At compile-time, the computer doesn’t know how much space an object will need.
What is the function in processing that lets you draw text on the canvas?
text(String, x, y);
(draws a string with its lower left corner at (x, y). Characters are mostly above the y value, but characters with descenders will go lower.)
What are the other text functions in processing?
textSize(p): sets the size of the text, in pixels
fill(): controls the text colour
textWidth(String): how many pixels wide will it be (based on textSize)
textAscent(): how many pixels “above the line”
textDescent(): how many pixels “below the line”
How can you get user input in your processing programs?
Using JOption Pane.
Add import javax.swing.JOptionPane; at the very top of your program, and use JOptionPane.showInputDialog(“prompt”) to get a String from the user
How can you turn something else into a String?
use str(x) (this is done automatically for functions that have strings as arguments)
How can you change a String containing characters that form a valid int or float literal, how can you obtain an actual int or float?
int() and float() ((int)string and (float)string don’t work)
What happens if you try to convert a string that is not a valid number into a number data type?
it will print NaN
Describe the structure of a string
A string is made up of a number of characters. Each character has a specified “index” (box or slot). The index of the first character in the String is 0