HOW YOUR TEXTUAL CODE IS READ(3)
HOW YOUR TEXTUAL CODE IS READ(3):
(Continued from 2)
LITERALS:
Literals are the source code representation of the values of primitive types, the String type and the null type. Primitive type literals are the Integer literal, Floating-point literal, Boolean literal, and Character literals.
INTEGERLITERALS:
Integer literals are either expressed as decimals (base 10), hexadecimals (base 16) or octals (base 8).
An integer literal suffixed with the ASCII letter L or l becomes of type long, else it is of type integer (integer types are 32 bit numerals while long are 64 bit.)
DECIMAL NUMERALS: either ASCII character O (rep. integer 0) or it consists of the ASCII digits from 1 through 9, optionally followed by one or more ASCII digits from 0 through 9, rep. the positive integers.
HEXADECIMAL NUMERALS: consists of the leading ASCII character Ox or OX followed by one or more ASCII hexadecimal digits (0123456789ABCDEF) or (0123456789abcdef) where numbers 10 to 15 are rep. by the ASCII letters A through F or a through f. It can rep. a positive, zero or negative integer.
OCTAL NUMERALS: consists of an ASCII digit 0 followed by one or more ASCII digits 0 through 7 and can rep. a positive, zero, or negative integer.
package examples;
public class IntegerLiteralExamples {
public static void main(String[] args) {
//What is the largest decimal literal of integer type
System.out.println("The largest decimal literal of type int
is: "+ l_decimal);
//This is how it will be in binary string
System.out.println("The above in binary string is: "+
Integer.toBinaryString(l_decimal));
//It is thus in hexadecimal:
System.out.println("The above in hexadecimal string is: " +
Integer.toHexString(l_decimal));
//Finally, in octal literals
System.out.println("In octal literals for the largest
decimal is: "+ Integer.toOctalString(l_decimal));
System.out.println();
//What is the smallest value of decimal literal
//of integer type?
int m_decimal = (int)Integer.MIN_VALUE;
System.out.print("The minimum decimal literal of type int
is: ");
System.out.println(m_decimal);
//this is how it will be in binary strings
System.out.print("The above in binary string is: ");
System.out.println(Integer.toBinaryString(m_decimal));
//Assignment: do the hexadecimals and octals yourself, svp.
}
}
FLOATING POINT LITERALS:
A floating point literal has the following parts: a whole number part, a decimal or hexadecimal part (represented by the ASCII period(.) character), a fractional part, an exponent and a type suffix. A floating point literal may be written as a decimal or hexadecimal value which is denoted by the exponent indicated in ASCII letter e or E for decimals and p or P for hexadecimals followed by an option signed integer. A floating point literal is of type float if suffixed with ASCII letter f or F, otherwise it is of type double and can be suffixed with ASCII letter d or D. The types of float and double are IEEE 754 32-bit single-precision and 64-bit double precision types respectively.
How to rep. a decimal float:
234.005e22f equivalent to .234005e25f equivalent to 234005e19f.
Representing a hexadecimal float:
Ox5004.a76p+10f equivalent to OX5004.a76p+10F equivalent to Ox5004.a76P+10f.
package examples;
class FloatLiteralExamples {
public static void main(String[] args) {
//checking for the input conversion from a unicode
//string to IEEE 754 binary floating point representation
String stringvalue = "234.005e22f";
Float checker = Float.valueOf(stringvalue); //a float
System.out.println("The IEEE representation:
"+checker.toString());
String stringvalue2 = "234005e19f";
Float checker2 = Float.valueOf(stringvalue2);
System.out.println(checker2.toString());
//let's make sure the two values are equal
if (checker2.equals(checker)){
System.out.println("Equal. Just wasn't necessary,
monkey do!");
}else {
System.out.println("Not equal. In the beginning was a
monkey.");
}
}
}
Another example:
class FloatLiteralExamples {
public static void main(String[] args) {
System.out.println("The largest finite float literal is: "
+Float.MAX_VALUE);
System.out.println("The smallest finite nonzero float
literal is: "+Float.MIN_VALUE);
System.out.println("The largest positive finite double
literal is: "+Double.MAX_VALUE);
System.out.println("The smallest positive finite nonzero
double literal is: "+Double.MIN_VALUE);
}
}
BOOLEAN LITERALS:
A boolean type has 2 values, represented by the literals true or false from ASCII letters.
CHARACTER LITERALS:
Character literals are expressed as a character or an escape sequence, enclosed in ASCII single quotes i.e ‘ SingleCharacter ’ or ‘ EscapeSequence ’. The single quote or apostrophe character is ‘\u0027’. Character literals can only represent UTF-16 code units i.e limited to \u0000 and \uffff range. Representation of supplementary characters are either as a surrogate pair within a character sequence or as an integer, depending on API used.
STRING LITERALS:
A string literal consists of zero or more characters enclosed in double quotes. Characters may be represented by escape sequences. A string literal is always of type String and always refers to the same instance of type String.
Use a string concatenation separator + to bring pieces of string literals together to form a single String literal.
In Unicode, \u000a represents the single linefeed (LF) but you cannot use it in character or string literals because of the lexical translation process which translates the unicode escape sequence above to a line terminator in step 2 and therefore it is not available as a string literal. Instead use “\n” for newline and “\r” for returns.
package examples;
class HelloFirst {
static String hello = "Hello", benthello="Hello";
}
class HelloSecond {
static String hello = "Hello";
}
class HelloMain {
public static void main(String[] args) {
// Literal strings within the same class
//in the same package refer to the same
//String object.
if (HelloFirst.hello == HelloFirst.benthello){
System.out.println("Same String object");
}else {
System.out.println("Not same String object");
}
//Literal strings within different classes in the same
//package represent references to the same String object
if(HelloFirst.hello == HelloSecond.hello){
System.out.println("Same String object.");
}else {
System.out.println("Not same String object.");
}
//Strings computed by constant expressions are computed
//at compile time and treated as literals
String compute = "345";
System.out.println("This constant is a string, remember:
"+String.valueOf(compute));
}
}
ESCAPE SEQUENCES FOR CHARACTER AND STRING LITERALS:
Escape sequences for character and string literals allows for the representation of some nongraphic characters as well as the single quote, double quote and backslash characters in character and string literals.
NULL LITERAL:
A null literal is always of the null type and has one value, null, from ASCII characters.
SEPARATORS:
The following nine ASCII characters are recognized java separators(punctuators):
( ) { } [ ] ; , .
OPERATORS:
The 37 tokens below are the operators for the java language from ASCII characters:
= > < ! ~ ? :
== <= >= != && || ++ --
+ - * / & | ^ %
+= -= *= /= &= |= ^= %=
<< >> >>> <<= >>= >>>=
No comments:
Post a Comment