Java Programming I on YouTube:
https://www.youtube.com/playlist?list=PLLIqcoGpl73iaXAtS_-V_Xdx3mhTzPwb5
What is Java String ?
String is the data type to manipulate strings of text:
Sequence of zero or more characters
Enclosed in double quotation marks
Null or empty strings have no characters
Length is the number of characters in a string
String variableName = value;
Statements that declare and initialize a string
String message1 = "Invalid data entry.";
String message2 = "";
String message3 = null;
message3 = message1 + "Please retry."
Note that Strings are objects -- not primitive data types!
Understanding the + operator to join, or concatenate Strings is important.
Previously we saw + operator when adding numeric variables
Using + operator with Strings is an example of an overloaded operator in Java.
Operator overloading means that an operator does different things depending on the context in which it is used.
Operator + concatenates two strings or a string and a numeric value or character:
"The sum = " + 12 + 26
After this statement the text assigned to the str becomes:
"The sum = 1226";
Is this what you want? Maybe the intent was
"The sum = " + (12 + 26)
which yields
"The sum = 38"
String class is one of many Java classes stored in the Java Application Interface (commonly called the API).
We can use these API classes to create other objects and build our Java applications.
String firstName = "Bob"; // firstName is Bob String lastName = "Smith"; // lastName is Smith String name = firstName + " " + lastName; // name is Bob Smith
double price = 14.95; String priceString = "Price: " + price;
How to append one string to another with the + operator:
firstName = "Bob"; // firstName is Bob lastName = "Smith"; // lastName is Smith name = firstName + " "; // name is Bob followed by a space name = name + lastName; // name is Bob Smith
How to append one string to another with the += operator
firstName = "Bob"; // firstName is Bob lastName = "Smith"; // lastName is Smith name = firstName + " "; // name is Bob followed by a space name += lastName; // name is Bob Smith
Character literals are character constants representing individual characters: 'a' and '5'
Some character literals are common escape sequences:
'\n' // new line (line feed) '\t' // tab '\r' // carriage return '\"' // double quote '\\' // backslash
Here is a string with a new line:
"Code: JSPS\nPrice: $49.50"
The resulting text is
Code: JSPS Price: $49.50
A string with tabs, like
"Joe\tSmith\rKate\tLewis"
returns result
Joe Smith Kate Lewis
A string with quotation marks
"Type \"x\" to exit"
returns result
Type "x" to exit
A string with backslashes
"C:\\java\\files"
returns result
C:\java\files
The syntax to create an object from a class:
ClassName objectName = new ClassName(arguments);
For example,
Scanner sc = new Scanner(System.in); // creates a Scanner object named sc Date now = new Date(); // creates a Date object named now
Syntax to call a method from an object:
objectName.methodName( arguments )
For example,
double subtotal = sc.nextDouble(); // get a double entry from the console String currentDate = now.toString(); // convert the date to a string
The syntax of the import statement is
import packagename.ClassName;
or
import packagename.*;
For example,
import java.text.NumberFormat; import java.util.Scanner; import java.util.*; import javax.swing.*;
Java classes are grouped in what we call packages (more about that later).
In order to use a Java API class in your .java application file, you must import that particular class.
To import a class or a complete package, the program uses the import statement.
Your import statements must be the first statements in your .java file.
The import with * indicates that all classes in the package should become available in your .java file.
The java.lang package contains classes that are used in most applications and this package is imported automatically.
Primitive data types and the class String are part of the Java language package -- don't need to be imported.
If you use a java API class, it's important to know
Which package is it coming from?
Is an import statement required?
Packages, Classes, Methods, and the import Statement summary:
Package: collection of related classes
Class: consists of methods and data attributes
Method: designed to accomplish a specific task
The System.out is an object that represents console output.
It has two methods to display text: println( ) and print( ).
The difference between the two methods is where they move the cursor after displaying the argument string.
The System.out object also has printf() method for formatting primitives and Strings.
The Scanner object takes care of keyboard input from the console device.
The Scanner class comes from the java.util package and must be imported.
The constructor method is needed to create a Scanner object.
You can use any valid Java identifier to name your object.
import java.util.Scanner;
To create a Scanner object,
Scanner sc = new Scanner(System.in);
java.lang fundamental parts of Java
java.text handling text, dates, numbers
java.util event model, collections, date and time facilities, and more
java.io system input/output, data streams, serialization, and file I/O
java.sql database access
java.applet applet framework
java.awt graphical user interface, common controls, images
java.awt.event user input from graphical user interface
javax.swing "lightweight" GUI components
How to use the Scanner class to create an object:
With an import statement:
Scanner sc = new Scanner(System.in);
Without an import statement:
java.util.Scanner sc = new java.util.Scanner(System.in);
Use Google to search for the Scanner class documentation.
The Scanner object takes input (for example, from the keyboard,) and converts it to the internal format of an int, double, or a String. The common Scanner methods are:
Scanner sc = new Scanner(System.in); String name = sc.next(); int count = sc.nextInt(); double subtotal = sc.nextDouble(); String cityName = sc.nextLine();
See also Scanner samples for more details.
The guidelines for creating objects using Java API classes are:
Every class that describes objects has a constructor method.
Program creates an object by calling the constructor method and passing required arguments.
Once the object is created, the program can call any of the public methods in the object.
Some methods of a Java class may be declared static.
Such methods become common to all objects of the class:
a static method performs the same operation for all objects of the class.
The static methods can be called without object reference, but they can also be called from an actual object.