Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- //inefficient as it creates a new string location in memory each time you "add" to the original
- String info = "";
- info += "My name is Bob.";
- info += " ";
- info += "I am a builder.";
- System.out.println(info);
- //more efficient, as it edits the original rather than creating new instances of the variable
- StringBuilder sb = new StringBuilder("");
- sb.append("My name is Sue.");
- sb.append(" ");
- sb.append("I am a lion tamer.");
- System.out.println((sb.toString()));
- StringBuilder s = new StringBuilder();
- s.append("My name is Rodger.")
- .append(" ")
- .append("I am a skydiver.");
- System.out.println((s.toString()));
- // Formatting ///
- System.out.println("Here is some text.\tThat was a tab.\nThat was a new line.");
- //formatting characters allow more control
- System.out.printf("Total cost %-10d; quantity is %d\n", 5, 120);
- for(int i=0; i<20; i++){
- System.out.printf("%-2d: %s\n", i, "Here is some text");
- }
- //formatting floating point values
- System.out.printf("Total value: %.2f\n", 5.68784);
- System.out.printf("Total value: %10.2f\n", 343.47649);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement