Wednesday, January 13, 2016

Topic 4: Java Primitive Types

  • Java supports following Primitive / Simple Types:

byte, short, int, long, char, float, double, and boolean

  • Integers
  • byte, short, int, and long - All are signed, positive and negative values. Java does not support unsigned, positive-only integers.
  • In any given expression, byte and short values are promoted to int.

  • Characters 

    • In Java, the data type used to store characters is char.

    • Java uses Unicode to represent characters and hence it requires 16 bits.

    • The range of a char is 0 to 65,536. There are no negative chars.

    • char can also be thought of as an integer type. We can perform arithmetic operations like addition, subtraction, etc. on characters. Consider the following code snippet:

                char ch = 'A'; 

                System.out.println("ch was " + ch + ", modified to " +  ++ch);

                Output is shown here:

                ch was A, modified to B

  • Boolean 
  • Boolean values true / false.
  • Expressions involving relation operators result in Boolean values. For ex, 8 > 4; results in true.

  • Decimal

    • Decimal are numeric values with precision and scale. float and double are two supported types for decimal number representation.

 

PRIMITIVE TYPE USAGE INSTRUCTIONS

  • Integer Literals - are any whole number value like 1, 2, 3, 42, etc.

  • Integer literal in Java is 32-bit integer value.

  • Can be decimal(base 10) values, octal (base eight) and hexadecimal (base 16).

  • Octal values are denoted in Java by a leading zero. Normal decimal numbers cannot have a leading zero. Thus, the seemingly valid value 09 will produce an error from the compiler, since 9 is outside of octal’s 0 to 7 range.

  • Hexadecimal values are denoted in Java by a leading zero-x, (0x or 0X). The range of a hexadecimal digit is 0 to 15, so A through F (or a through f ) are substituted for 10 through 15.

  • When a integer literal value is assigned to a byte or short variable, no error is generated if the literal value is within the range of the target type.

  • To specify a long literal, we need to explicitly tell the compiler that the literal value is of type long. We do this by appending an upper- or lowercase L to the literal. For example, 0x7ffffffffffffffL or 9223372036854775807L is the largest long.

  • An integer can be assigned to a char as long as it is within range of char(0 to 65,536).

  • Floating-Point Literals - are decimal values with a fractional component. 
  • Can be expressed in either standard or scientific notation.
  • Standard notation consists of a whole number followed by a decimal point followed by a fractional component like, 12345.6789
  • Scientific notation is similar to standard representation of floating-point number plus a suffix that specifies a power of 10 by which the number is to be multiplied. The exponent is indicated by an E or e followed by a decimal number, which can be positive or negative. Examples include 6.022E23, 314159E–05, and 2e+100.
  • Floating-point literals in Java default to double precision. To specify a float literal, we must append an F or f to the constant.
  • We can explicitly specify a double literal by appending a D or d.

  •  Boolean Literals - are only two logical values, true and false.

  • The values of true and false do not convert into any numerical representation like 1 or 0.

  • In Java, they can only be assigned to variables declared as boolean, or used in expressions with Boolean operators.

  • Character Literals - are represented inside a pair of single quotes like, ‘a’, ‘z’, ‘@’, etc

    • There are several escape sequences, such as ‘\’’ for the single-quote character itself and ‘\n’ for the newline character.

    • We can directly enter the value of a character in octal or hexadecimal. For octal notation, use the backslash followed by the three-digit number. For example, ‘\141’ is the letter ‘a’. For hexadecimal, we enter a backslash-u (\u), then exactly four hexadecimal digits. For example, ‘\u0061’ is the ISO-Latin-1 ‘a’ because the top byte is zero.

  • String Literals - are specified by enclosing a sequence of characters between a pair of double quotes like, “Hello World”, “two\nlines”, “\”This is in quotes\”“

    • Java strings must begin and end on the same line. There is no line-continuation escape sequence as in some other languages.

 

Click for NEXT article

 

Please feel free to correct me by commenting your suggestions and feedback.

No comments:

Post a Comment