b. Data types
Primitive data types: Java supports eight primitive data types
||32-bit signed two's complement integer
||8-bit signed two's complement integer
||16-bit signed two's complement integer
||64-bit signed two's complement integer
||32-bit IEEE 754 floating point
||64-bit IEEE 754 floating point
||16-bit Unicode character
||True or false
Reference data type:Reference variables are used to access objects and are created constructors of the classes. These are declared to be of a specific type that cannot be changed. These can refer to any object of declared type or any compatible type. Class objects and array variables are some examples.
Literals:A literal is source code representation of a fixed value.
1. Unary operators.
1. Increment and Decrement operators ++ -- :We have postfix and prefix notation. In post-fix notation value of the variable/expression is modified after the value is taken for the execution of statement. In prefix notation, value of the variable/expression is modified before the value is taken for the execution of statement.
x = 5; y = 0; y = x++; Result will be x = 6, y = 5
x = 5; y = 0; y = ++x; Result will be x = 6, y = 6
2.Implicit narrowing conversion is done, when applied to byte, short or char.
3. Unary minus and unary plus + -
+ Has no effect than to stress positivity.
- Negates an expression's value. (2's complement for integral expressions)
4. Negation!:Inverts the value of a Boolean expression.
5. Complement ~: Inverts the bit pattern of an integral expression. (1's complement
- 0s to 1s and 1s to 0s). Cannot be applied to non-integral types.
6. Cast (): Persuades compiler to allow certain assignments. Extensive checking is done at compile and runtime to ensure type-safety.
2. Arithmetic operators - *, /, %, +, -
- Can be applied to all numeric types.
- Can be applied to only the numeric types, except '+' - it can be applied to Strings as well.
- All arithmetic operations are done at least with 'int'. (If types are smaller, promotion happens. Result will be of a type at least as wide as the wide type of operands)
- Accuracy is lost silently when arithmetic overflow/error occurs. Result is a nonsense value.
- Integer division by zero throws an exception.
- % - Reduce the magnitude of LHS by the magnitude of RHS. (Continuous subtraction). % - Sign of the result entirely determined by sign of LHS 5 % 0 throws an Arithmetic Exception.
- Floating point calculations can produce NaN (square root of a negative no) or Infinity (division by zero). Float and Double wrapper classes have named constants for NaN and infinities.
- NaN's are non-ordinal for comparisons. x == Float.NaN won't work. Use Float.IsNaN (x) But equals method on wrapper objects (Double or Float) with NaN values compares Nan's correctly.
- Infinities are ordinal. X == Double.POSITIVE_INFINITY will give expected result.
- + Also performs String concatenation (when any operand in an expression is a String). The language itself overloads this operator. toString methods of non-String object operands are called to perform concatenation. In case of primitives, a wrapper object is created with the primitive value and toString method of that object is called. ("Vel" + 3 will work.)
System.out.println (1 + 2 + "3"); // Prints 33
System.out.println ("1" + 2 + 3); // Prints 123