Literals


x

Lesson:
Main Points:
  1. A literal is a value that may be assigned to a primitive or string variable or passed as an argument to a method call.
  2. Boolean literals may be true or false.
  3. char literals are expressed by enclosing the desired character in single quotes.
  4. Integral literals may be expressed in decimal, octal, or hexadecimal. (The default is decimal.)
  5. Floating-point literals expresses a floating-point numerical value.
  6. 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.

  1. boolean eatsCabbage = true;
  2. 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:
  1. '\n' for Newline
  2. '\t' for Tab
  3. '\b' for Backspace
  4. '\f' for Formfeed
  5. '\r' for Return
  6. '\'' for Single Quote
  7. '\"' for Double Quote
  8. '\\' 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:
  1. 30 (default decimal form)
  2. 036 (octal form 3*8+6=30)
  3. 0x1e (hexadecimal form 1*16+14=30)
  4. 0X1e (hexadecimal alternative capitalization)
  5. 0x1E (hexadecimal alternative capitalization)
  6. 0X1E (hexadecimal alternative capitalization)

Floating-Point Literals
There are four ways of ensuring that a numerical expression is interpreted as a floating-point literal:
  1. Use a decimal point: 4.553
  2. Use the letter E or e to indicate scientific notation: 4.33E+21
  3. Use the suffix F or f to indicate a 32-bit float literal: 233f
  4. 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); } }
x

Assignment:
Write an applet in which you do the following:
  1. Use integral literals to set values for variables which contain the hexadecimal equivalents of the decimal values 55, 75, and 100
  2. Set a double value using the E for scientific notation
  3. Use a tab escape in a string literal
  4. Figure out what the unicode value is for the question mark
  5. Make sure you display the values in your variables in your applet

Present your code along with your applet on a web page.
x