Long
- Long is a signed 64 bit type(8 byte).
- Useful where an
int tye is not large enough to hold the desired value.
- This range is long and is useful when big, whole numbers are needed.
A Simple program to demonstrate this is given here.
Long Demo
Double
- Double precision denoted by Double Keyword.Uses 64 bits (8 bytes)
to store a value.
- All transcendental math functions such as
sin(),cos()
sqrt()return double values.
Used to maintain accuracy over many iterative calculations and manipulating large valued numbers.
This Program uses Double variables to compute the area of a circle:
Double Demo
Characters
- Unicode is used to represent characters, its range is from 0 to 65536. No negative characters.
- We can also make the character variable behave like integers.
A small program using char is given here.Char Demo
Another program using char as interger is given at char Int
More information about unicode can be fould at http://www.unicode.org.
Booleans
- Simple type for logical values.
- It can have only two possible values True,False.
- This is the type returned by all relational operators like a < b.
- This the type required by the conditional expressions that governs the constrol statements
like if and for.
A sample program that demonstrates Boolean values is given here.Boolean.
LITERALS
A constant value in Java is created by using literal representation of it.
Integer Literals
- Any Whole number value is an integer literal.
- The bases that are used in integer literals are base 10, octal(base 8), hexadecimal(base 16).
- Octal values are denoted by java using leading zero.
- Normal decimal values cannot have a leading zero.
- Programers use hexadecimal to denote numbers.
- Hexadecimal matches with mode 8 word sizes, such as 8,16,32 and 64. bits.
- Range in hexadecimal is 0 to 15 A through F for 10 to 15.
- Integer literals create a 32 bit integer value.
- When a literal value is assigned to abyte or short variable, no error is generated if
the literal value is within the range of the target type.
- Integer literal can be assigned to a long variable.
- We have to explicitly tell the compiler that the literal value is long if we want to specify a
long literal.
- We can do this by appending a lowercase l or a uppercase L to the literal.
- Example: 0x7ffffffffffffffL,9223372036854775807L is the largest long.
Floating-Point Literals
- They Represent decimal values with a fractonal component.
- They can be represent in Standard or Scientific notations.
- Standard notation consists of a whole number component followed by a decimal point followed by a fractional
component.
- Scientific notation uses a standard-notation,floating-point numbers 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 a possitive or negative.
- Floating point in Java Defaults to Double precision.
- To Specify a floating point literal we have to append a F or f to the constant.
- On the same way a double literal can also be specified.
- Double consumes about 64 bits of storage, Float consumes 32 bits of storage.
Boolean Literals
- Boolean Literals can have only true and false values. They are not equal to 0 ro 1.
- In java they can be assigned to variables decalred as boolean or used in expression with Boolean operators.
Character Literals
- Characters in java are indices into the Unicode character set.
- A literl character is represented inside a pair of single quotes.
- There is a mechanism for directly entering the value of a character in octal or hexadecimal.
String Literals
- String Literals are specified by enclosing a sequence of characters between a pair of double quotes.
- We can also use the escape sequences to represent special characters.
Variables
The variable is the basic unit of storage in a Java program. A variable is defined by the combination
if an identifier, a type, and an optional initializer.
Dynamic Initialization
The program that computes the length of the hypotenuse of a right triangle given the lengths of its two
opposite sides is given here.Dynamic Initialization
The key point is the initialization expression may use any element valid at the time of initialization,
including calls to methods, other variables or literals.
The Scope and Lifetime of variables
- A Block defines a scope.
- In java, the two major scopes are those defined by a class and those defined by methods.
- The scope defined by a method begins with its opening curly brace. However, if that method
has parameters, they too are included within the method's scope.
- As a general rule, variables declared inside a scope are not visible(that is accessible)
to code that is defined outside the scope.Thus when you declare a variable within a scope,
you are localizing that variable and protecting it from unauthorized access and/or modification.
A program that demonstrates blocks and nested blocks is given here.Nested Scope.
This program demonstrates the life time of a variable.Life Time of a variable.
Type Conversion and Casting
- To obtain conversion between inconpatible types we use casting.
- For Example we can convert int to long. Float to double.
- If we want to convert double to byte we have to use casting.
Automatic Conversion
- Java Performs automatic conversion if the following two conditions are met.
- The two types are compatible.
- The Destination type is larger than the source type.
- Numetic types are not compatible with with char boolean.
- Char and boolean are not compatible.
- A truncation will occur if a floating point variable is converted to an integer.
The Following program demostrates conversions.Conversion.