TYPES, VALUES AND VARIABLES(1):
There are two kind of types in java programming language:
- Primitive types
- Reference types
These types correspond to the kinds of data values that exist in java, primitive values and reference values.
A special null type exists, without name, therefore, being nameless, no variable can be declared of null type or cast to null type. The null reference is the only possible value of an expression of null type and note that the null reference can always be cast to any reference type. In other words, a null contains no field and therefore is not a type with name and therefore, being nameless, you can only reference it but since it contains nothing, it has no value and will give you nothing except nothing which means that if you have a type, m, which is any type, null can be cast to m so that m will be given an empty value as reference.
PRIMITIVE TYPES AND VALUES:
Primitive types are either numeric types or boolean types. Primitive values do not share state with other primitive values and these values are always of the same type. The numeric types are the integral types and floating point types. The integral types are byte, short, int, and long whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two’s complement integers respectively, and char, whose values are 16-bit unsigned integer representing UTF-16 code units.
The floating point types are float, whose values include 32-bit IEEE 754 floating point numbers and double, whose values include the 64-bit IEEE 754 floating point numbers.
The boolean type has exactly 2 values: true and false.
INTEGRAL TYPES AND VALUES:
The integral types have values in the following ranges:
- for byte, from -128 to 127, inclusive.
- For short, from -32768 to 32767, inclusive
- For int, from -2147483648 to 2147483647, inclusive
- For long, from -92233720368524775808 to 92233720368524775807.
- For char, from ‘\u0000’ to ‘\uFFFF’, inclusive. i.e from 0 to 65535.
Examples:
package api_package;
class IntegerOperations {
static byte first = 120;
static short second = 30999;
static int third = 55436787, age=25;
static long fourth = 65543678765L;//affix the L
char fifth = '\u0987';
public static void main(String[] args) {
//<, <=, > and >= operators return type boolean
//that is, true or false to the operation
if (second > first){
System.out.println("yes, second is greater.");
}
else {
System.out.println("Nope, second is not greater.");
}
//just another example. Am i lesser or equal to myself?
if (fourth <= fourth){
System.out.println("That proposition is somewhat
true!");
}
else{
System.out.println("I'm greater than myself.");
}
//== and != numerica equality operators are somewhat
//focused on a definite equal or not equal.
if (third != third){
System.out.println("I'm not equal to me!");
}else{
System.out.println("I should be equal to me!");
}
//for numerical operators that result in type int or long
//such as +(unary),-(unary),*,/,%,+ and -(additive),
//++ and -- prefix and postfix increment and decrement op.
//<<,>>,>>>, signed and unsigned shift ops,
//~ bitwise complement op;&, |, ^ integer bitwise ops.
//so let's begin from the beginning
int result = -first;
System.out.println("We've made first negative: "+result);
result = first * age; //120 times 25 = 3000
System.out.println("Multiplying first by age: "+result);
result = first / age; //120 divided 25 = 4
System.out.println("A division gives: "+result);
result = second % first; //result is 39, the remainder
System.out.println("Remainder of the division is:
"+result);
result = first + age; //u guessed right,145
System.out.println("Simpler all the time, it's "+result);
System.out.println("Doing a prefix increment addition:
"+(++result));
//little playing with bits,<<>
//>> is age/2 raised to 2, see jls
result = age>>2;
System.out.println("25 divided by 2 powered 2 is:
"+result);
//try this bitwise complement, remember first is byte
first = 127; //the byte is highest value, all 1s, and +ve
System.out.print("Bitwise complementing the highest byte
value ");
System.out.print(first+" becomes the lowest ");
System.out.println(~first); //1 -> 0, +ve -> -ve
//a little intricacy, but nothing to bother you.
//bitwise, note integer bitwise operators
//expo: for & the bits return 1 when same or 0
//for |, they bring 1 when any is 1 else 0
//for ^, they bring 1 only when both are complements else 0
System.out.println("This gives 25: "+(25&27));
System.out.println("This give 27: "+(25|27));
System.out.println("This gives a 2: "+(25^27));
//if a long exists in operation, promote result to long
//automatically. e.g here forces promotion
if ((fourth*third)>Integer.MAX_VALUE){
System.out.println("Result "+fourth*third+" returned
as long");
}else {
System.out.println("Result"+fourth*third+" was int");
}
//if none of the operands is specified as int
//numeric promotion to type int forced. inconsistent result
//always type variables and values and anticipate ranges
System.out.println("This multiplication overflows on int
giving "+2147483645*2);
}
}
No comments:
Post a Comment