Literals
Lesson:
Main Points:
- A literal is a value that may be assigned to a primitive or string
variable or passed as an argument to a method call.
- Boolean literals may be true or false.
- char literals are expressed by enclosing the desired character in single
quotes.
- Integral literals may be expressed in decimal, octal, or hexadecimal.
(The default is decimal.)
- Floating-point literals expresses a floating-point numerical value.
- A string literal is a sequence of text enclosed by double quotes.
boolean Literals
There are only two possible valid values for the boolean type: true or
false.
- boolean eatsCabbage = true;
- boolean smellsFunny = false;
char Literals
Usually you can express a char literal in single quotes like this:
char ch = 'V';
But not all possible char values are available on the keyboard and so you
may have to use Unicode hexadecimal digits to represent certain values. In
these cases you use \u followed by the Unicode digits with the whole thing
enclosed in single quotes like this:
char t = '\u14f2';
There are also several escape sequence available which represent special
characters:
- '\n' for Newline
- '\t' for Tab
- '\b' for Backspace
- '\f' for Formfeed
- '\r' for Return
- '\'' for Single Quote
- '\"' for Double Quote
- '\\' for Backslash
Integral Literals
Although by default integral literals are expressed as decimal, they may
also be expressed as octal or hexadecimal. The prefix 0 (zero) is used to
indicate octal. The prefix 0x or 0X is used to indicate hexadecimal. The hex
digits may be uppercase or lowercase. Here are six ways of expressing the
value thirty:
- 30 (default decimal form)
- 036 (octal form 3*8+6=30)
- 0x1e (hexadecimal form 1*16+14=30)
- 0X1e (hexadecimal alternative capitalization)
- 0x1E (hexadecimal alternative capitalization)
- 0X1E (hexadecimal alternative capitalization)
Floating-Point Literals
There are four ways of ensuring that a numerical expression is interpreted
as a floating-point literal:
- Use a decimal point: 4.553
- Use the letter E or e to indicate scientific notation: 4.33E+21
- Use the suffix F or f to indicate a 32-bit float literal: 233f
- Use the suffix D or d to indicate a 64-bit double literal: 344D
String Literals
A string literal is a sequence of characters enclosed in double quotes like
this:
String str = "Here is a spiffy little string.\n";
As you can see, escape sequences can be included in the string.
EXAMPLE:
import java.awt.*;
import java.applet.*;
public class ex04 extends Applet{
char c = '\u3333'; // Unicode
int hex = 0x5ff; // hexadecimal
float f = 344F; //float
boolean T = true; //boolean
String str = "She said, \"That's a string!\""; //escape code in string
public void init(){
setBackground(Color.yellow);
}
public void paint(Graphics g){
g.setColor(Color.black);
g.drawString("char value using Unicode: "+c, 10, 20);
g.drawString("hex value using 0x: "+hex,10,35);
g.drawString("float value using F: "+f,10,50);
g.drawString("boolean value: "+T,10,65);
g.drawString("String value: "+str,10,80);
}
}
Assignment:
Write an applet in which you do the following:
- Use integral literals to set values for variables which
contain the hexadecimal equivalents of the decimal values 55, 75, and
100
- Set a double value using the E for scientific notation
- Use a tab escape in a string literal
- Figure out what the unicode value is for the question mark
- Make sure you display the values in your variables in your applet
Present your code along with your applet on a web page.