Part 04 - Java Basics 4 - Built in Objects
Part 04 - Java Basics 4 - Built in Objects
name.substring(2,9)
receiver
parameters
message
int a = 1;
int b = 1;
if (a == b)... // true
implementation
◼ If building a simple String, just concatenate
myHomePage.extension());
System.out.println("Filename = " +
myHomePage.filename());
System.out.println("Path = " +
myHomePage.path());
}
} Extension = html
Filename = index
Path = /home/mem
Khoa CNTT – Trường ĐH Nông Lâm TP. HCM
FileName
public class Filename {
private String fullPath;
private char pathSeparator, extensionSeparator;
36
Formatted Output: printf
◼ Takes a variable number of arguments
◼ System.out.printf("Formatting String", arg1, arg2, …);
◼ Advantages
◼ Lets you insert values into output without much clumsier
String concatenation.
◼ Lets you control the width of results so things line up
◼ Lets you control the number of digits after the decimal point
in numbers, for consistent-looking output
◼ Very similar to C/C++ printf function
◼ If you know printf in C/C++, you can probably use Java's
printf immediately without reading any documentation
◼ Although some additions in time formatting and locales
◼ Use String.format to get the equivalent of C's sprintf
Khoa CNTT – Trường ĐH Nông Lâm TP. HCM
printf vs. println
◼ General idea
◼ Each %s entry in formatting string is replaced by next
argument in argument list. %n means newline.
◼ Example
public static void printSomeStrings() {
String firstName = "John";
String lastName = "Doe";
int numPets = 7;
String petType = "chickens";
System.out.printf("%s %s has %s %s.%n",
firstName, lastName, numPets, petType);
System.out.println(firstName + " " + lastName +
" has " + numPets + " " + petType + ".");
}
SALARIES:
Steve Jobs: $ 3.12
Scott McNealy: $ 45.57
Output Jeff Bezos: $ 567.98
Larry Ellison: $6,789.00
25 Bill Gates: $78,901,234,567,890.12