Model 1: Basic Java Programming - Text values with String and char Flashcards
Text values with String and char #
The string data itself, when typed in quotes in code
is called a string literal
Java should interpret it as literal string data, and not as code.
String is a class#
A class in Java has two purposes:
- A class is a collection of methods that you may call.
- A class defines a custom data type and can be used to make objects of that class.
The String class serves both purposes: it provides some methods that are useful for Strings, and it defines a String data type that you can use to make string objects.
Strings are objects#
An object is a structure in memory that
stores values.
Strings are objects#
A String object stores data
the value of each character in the string, as well as the length of the string.
Strings are objects#
Some things you can do with objects are:
- Declare a variable to store a reference to an object of a class: String s.
- Create a new object of that type:
s = “Rumplestiltskin”;
Strings are somewhat special in Java; other types of objects are created using special method calls. - Read or modify data from the object, using dot notation to get at instance variables. In Java, the String method has no directly useful instance variables, so we’ll see examples of this with other classes.
- Call non-static methods on an object; these methods have access to the data within an object:
System.out.println(s.toUpperCase());
Static vs non-static methods of the String class
To access static methods of the String class
recall that you use both the name of the class and the method name:
Static methods are just like functions in Javascript, C, or Python.
Java just uses classes to organize these functions.
Non-static methods require a string as input, and
instead of passing that string as a parameter, dot-notation is used: s.toUpperCase().
Strings are in Java#
although there is a way to get a character from a String using the charAt() method
there is no way to change a character within a string.