Wednesday 31 July 2013

08:Identify Java keywords, Java literals, and primitive data types

Java Keywords
  Keywords are predefined identifiers reserved by Java for a specific purpose.
  You cannot use keywords as names for your variables, classes, methods, and so on. 

A list of Java keywords are as follows:
Java Literals
Literals are tokens that do not change. They are constant in value.
The different types of literals in Java are:
  Integer Literals
  Floating-Point Literals
  Boolean Literals
  Character Literals
  String Literals
Java Literals: Integer
Integer literals come in different formats:
  decimal (base 10)
  hexadecimal (base 16)
  octal (base 8)
Special notations in using integer literals in Java programs:
Decimal:
  No special notation
  Example: 12
Hexadecimal:
  Precede by 0x or 0X
  Example: 0xC
Octal:
  Precede by zero
  Example: 014
Java Literals: Floating point
Represents decimals with fractional parts:
Example: 3.1416
Can be expressed in standard or scientific notation:
Example: 583.45 (standard), 5.8345e2 (scientific)
Java Literals: Boolean
Boolean literals have only two values, true or false.
Java Literals: Character
Character Literals represent single Unicode characters.
Unicode character:
  A 16-bit character set that replaces the 8-bit ASCII character set
  Unicode allows the inclusion of symbols and special characters from other languages
Java Literals: Character
  To use a character literal, enclose the character in single quote delimiter.
  For example: The letter a, is represented as ‘a’.
  Special characters such as a newline character, a backslash is used followed by the
character code. For example, ‘\n’ for the newline character, ‘\r’ for the carriage return,
and ‘\b’ for backspace.
Java Literals: String
  String literals represent multiple characters and are enclosed by double quotes.
  An example of a string literal is, “Hello World”.
Primitive Data Types
The Java programming language defines eight primitive data types:
  boolean (for logical)
  char (for textual)
  byte
  short
  int
  long (integral)
  double
  float (floating point)
Primitive Data Types: Logical-boolean
  A boolean data type represents two states of true and false.
  An example is boolean result = true;
  The earlier example shown, declares a variable named result as boolean type and
assigns it a value of true.
Primitive Data Types: Textual-char
  A character data type (char), represents a single Unicode character.
  It must have its literal enclosed in single quotes(’ ’).
  For example the following code:
‘a’ //The letter a
‘\t’ //A tab
To represent special characters like ' (single quotes) or " (double quotes), use the escape
character \. For example the following code:
'\'' //for single quotes
'\"' //for double quotes
Important points to remember:
  A string represents a data type that contains multiple characters. It is not a primitive
data type, rather it is a class.
  It has its literal enclosed in double quotes(“”). For example, 
String message=“Helloworld!”; 

Primitive Data Types: Integral – byte, short, int, and, long
Integral data types in Java uses three forms of decimal, octal, or hexadecimal.
Examples are:
  2 // The decimal value 2
  077 // The leading 0 indicates an octal value
  0xBACC // The leading 0x indicates a hex value
Integral types has int as default data type.
You can define its long value by appending the letter l or L.
For example: 10L
Integral data type have the following ranges:
Integer Length  Name or Type  Range
8 bits  byte  -27 to 27-1
16 bits  short  -215 to 215-1
32 bits  int  -231 to 231-1
64 bits  long  -263 to 263-1
Coding Guidelines:In defining a long value, a lowercase L is not recommended because it is
hard to distinguish from the digit 1.
Primitive Data Types: Floating Point-float and double
Floating point types has double as default data type.
Floating-point literal includes either a decimal point or one of the following,
  E or e //(add exponential value)
  F or f //(float)
  D or d //(double)
Examples are:
  3.14 //A simple floating-point value (a double)
  6.02E23 //A large floating-point value
  2.718F //A simple float size value
  123.4E+306D //A large double value with redundant D
Floating-point data types have the following ranges:
Try It Out
Problem Statement:
Write a program that contains the literal values for the following statements:
1.  Declare an int named sizeand assign it the value 32
2.  Declare a char named initialand assign it the value ‘j’.
3.  Declare double named dand assign it the value 456.709
4.  Declare a boolean named isAvailable(no assignment)
5.  Assign the value true to the isAvailable that is declared earlier
6.  Declare an int named y, and assign it the value thatis the sum of whatever xand 456.
Code:
class LiteralValues {
public static void main(String[] args) {
int size = 32;
char initial = 'j';
double d = 456.709;
boolean isAvailable;
isAvailable = true;
int y = size + 456;
}
}
Refer File Name: LiteralValues.javato obtain soft copy of the program code
How It Works:
You can assign a value to a variable in one of the following several ways including:
  Type a literal value after the equals sign (x = 12, isGood = true, and so on)
  Assign the value of one variable to another (x = y)
  Use an expression combining the two (x = y + 43)
For the given problem statement, the literalvalues are shown in bold italics:
int size = 32;
char initial = ‘j’;
double d = 456.709;
boolean isAvailable;
isAvailable = true;
int y = x + 456;
Tips and Tricks
List out few important points while using identifiers, literals, and keywords in Java.
Solution:
  An identifier must begin with a letter, dollar sign($), or underscore (_). Subsequent
characters may be letters, $, _ (underscore), or digits.
  An identifier cannot have a name of a Java keyword. Embedded keywords are
accepted. true, false, and null are literals (not keywords), but they cannot be used as
identifiers as well.
  const and goto are reserved words, but not used.
  All numeric data types are signed. char is the only unsigned integral type.
  A number is by default an int literal. A decimal number is by default a double literal.
  1E-5d is a valid double literal and E2d is not (since it starts with a letter, compiler
thinks that it’s an identifier).
Summary
  Integer literals can be decimal, octal (e.g. 013), or hexadecimal (e.g. 0x3d).
  Literals for longs end in L or l (lowercase of the alphabet L).
  Float literals end in F or f, double literals end in a digit D or d.
  The boolean literals are true and false.
  Literals for chars are single character inside single quotes: ‘d’.

No comments:

Post a Comment