Unit 1: Introduction to Java


1.2 Everything about Printing

Most of this is review now. Printf() is further down this page.

print(), println()

The way to print something to the screen is System.out.println("text")

System.out.print("text") is the same as PRINTLN but does not add a "new line" or "enter" at the end of the line.

Thus System.out.print("text\n") is the same as System.out.println("text")

When you print a double Java will display at least one decimal place (eg. 5.0) and up to 16 (e.g. 1.0 divided by 3.0 = 0.3333333333333333 )
If you want to limit the number of decimal places, use printf (below).

Concatenation

Use a + sign to join two strings together as in the example above (this is called concatenation).

This can get quite messy and hard to read. It also requires that you are very careful with where you place spaces, ", and + :

System.out.println(name + " is " + age + " years old. He is " + height + " inches tall, and weighs " + weight + " pounds.");

printf() offers another way to print out text. See below.

Escape Characters

Escape characters allow you to type in things that you could not otherwise normally type.
There are five common escape characters. Make sure that you know how to use them.

  1. \n (newline)
  2. \t (tab)
  3. \\ (backslash)
  4. \" (double quotes)
  5. \' (single quote) You don't need to use \' in strings. Just type '. It's only needed in a char variable. eg. char c = '\'';

Each of these is one character,even though it takes two keystrokes to type.

Unicode Characters

You can also use escape codes to print out unicode characters (some of which look like emojis).
System.out.println("I am the \u265B \u2394"); which prints out: I am the ♛ ⎔  

printf()

Printf is useful because

The printf() function exists in almost all programming languages (C, C++, python, perl,...), however, it can get complicated ...

How it works:
(i) take the text you want to print, and write it, replacing all variable names with %
(ii) fill in the correct symbol after the % to indicate the variable types
(iii) at the end of your string, list all of the variables used in the correct order.
(iv) printf does not print a newline at the end of the string, so add one with %n

Printf() takes one string with all of the formatting in it, including % signs.
After the string comes the list of variables, one for each %, separated by commas

Example:

Old code using println :

System.out.println(name + " is " + age + " years old. He is " + height + " inches tall, and weighs " + weight + " pounds.");

New code using printf :

System.out.printf("%s is %d years old. He is %d inches tall, and weights %f pounds.", name, age, height, weight);
Note how much easier it is to read.
You would still use println for printing simple things.

Data types
%sString
%dinteger (int or long)
%ffloating (float or double)
%gscientific notation for floating
 
%nuse this instead of \n in printf
%%use this to print a %

The power of printf really shines when you realize that there are important formatting codes that you can put between the % and the letter indicating data type:

Formatting printf:

%stuff hered
The following codes go between the % and the letter: % flags width . precision type

FlagsApplies to
-left justifiedstrings or numbers
+include sign, whether positive or negativenumbers
0add leading zerosnumbers
Width
This is how wide the field will be. It will be filled with spaces (unless you want zeros in front).
It is the minimum width: big numbers or words can be more than this. So in order to line up numbers, make the width big enough for your biggest number
Precision
Used to restrict the output depending on the conversion.
It specifies the number of decimal places when outputting floating-point values.
The default number is 6 (ie. 6 decimal places)
For strings it limits the number of letters printed.
If you want to use "precision" you must put a . before this number
       
%08d 	  Eight digits in width, with leading zeroes as necessary.
%.3f 	  Three places after decimal point.
%-10.3f 	  Ten characters in width, left justified, with three places after decimal point.

Example: System.out.printf("%5d divided by %3d = %4.4f", 5, 7, 5.0/7.0);
Prints " 5 divided by 7 = 0.7143"

Google "printf cheat sheet" for more details (and also look at Wikipedia).

If you want to store the output of the printf command instead of printing it, for example, if you wanted to save it into a text file, you can use the format() function instead of println(). ***BUT*** change "System.out" to "String"

double n1=5.0, n2=7.0;
String s = String.format("%5.0f divided by %3.0f = %4.4f", n1, n2, n1/n2);
or using concatenation as for println() , but you can't control the number of decimal places you get.
String s2 = n1 + " divided by " + n2 + " = " + n1/n2;

printf quick reference (pdf)