Wednesday, August 20, 2014

Types Values Variables

Java is Statically and Strongly typed language.

Statically - Every variable and every expression has a type that is known at compile time.

Strongly - because types
limit the values that a variable can hold or that an expression can produce,
limit the operations supported on those values.

Two types are there in java.
* Primitive types - boolean type, numeric type (byte, short, int, long, char, float and double)
* Reference types - class type, interface types, array type.

Primitive types and values.
  Primitive types are predefined by java and named by its reserved key word.

Integer types and values.
byte (-128 to 127)
short (-32768 to 32767)
int    (-2147483648 to 2147483647)
long (-9223372036854775808 to 9223372036854775807) 0 to 18446744073709551615
char ('\u0000' to '\uffff')  0 to 65535

Operations can be performed on Integer.
=
>
<
>=
<=
~
? : (condition)
==
!=
&&
||
++ (prefix and postfix)
--  (prefix and postfix)
+
-
*
/
&
|
^
%
( <<
  >>    
  >>>  singed and unsigned shifts ).
cast operator to any primitive types.
String concatenation operator.

Floating point types and values.
Float
Double
[ This part ready hard ]







Add caption






class Test {
 public static void main(String[] args) {
 // An example of overflow:
 double d = 1e308;
 System.out.print("overflow produces infinity: ");
 System.out.println(d + "*10==" + d*10);
 // An example of gradual underflow:
 d = 1e-305 * Math.PI;
 System.out.print("gradual underflow: " + d + "\n ");
 for (int i = 0; i < 4; i++)
 System.out.print(" " + (d /= 100000));
 System.out.println();
 // An example of NaN:
 System.out.print("0.0/0.0 is Not-a-Number: ");
 d = 0.0/0.0;
 System.out.println(d);
 // An example of inexact results and rounding:
 System.out.print("inexact results with float:");
 for (int i = 0; i < 100; i++) {
 float z = 1.0f / i;
 if (z * i != 1.0f)
 System.out.print(" " + i);
 }
 System.out.println();
 // Another example of inexact results and rounding:
 System.out.print("inexact results with double:");
 for (int i = 0; i < 100; i++) {
 double z = 1.0 / i;
 if (z * i != 1.0)
 System.out.print(" " + i);
 }
 System.out.println();
 // An example of cast to integer rounding:
 System.out.print("cast to int rounds toward 0: ");
 d = 12345.6;
 System.out.println((int)d + " " + (int)(-d));
 }
}


Boolean type values and operations.

true or false.
==
!=
&
^
|
&&
||
? :
string concatenation +
it controls flow in several statements.
if
while
do
for


Reference Type and values.
Four Reference types.
Class type
Interface type
Type variable
Array type.

Object:
An Object is class instance or Array Instance.
The reference values are just pointer to this object. null reference points no object.
instance creation can take place where string concatenation performed new string object will created.

A new array object is implicitly created when an array initializer expression is evaluated
class or interface is initialized
new instance of a class is created
local variable declaration statement is executed
New objects of the types Boolean, Byte, Short, Character, Integer, Long, Float, and Double may be implicitly created by boxing conversion.

class Point {
 
  int x, y;
 
  Point() { System.out.println("default"); }
  Point(int x, int y) { this.x = x; this.y = y; }  
  static Point origin = new Point(5, 5);                        /* A Point instance is explicitly created at  class initialization time: */
  public String toString() { return "(" + x + "," + y + ")"; } /* A String can be implicitly created by a + operator: */
}

class Test {
  public static void main(String[] args) {
   
    Point p = null;  
   
    System.out.println("p: " + p);                               /* Strings are implicitly created by + operators: */
    try {
      p = (Point)Class.forName("Point").newInstance();         /* A Point object is explicitly created using newInstance: */
    }  
    catch (Exception e) {
      System.out.println(e);
    }  

    System.out.println("p: " + p);                               /* Strings are implicitly created by + operators: */
    Point a[] = { new Point(0,0), new Point(1,1) };              /* An array is implicitly created  by an array constructor: */
    System.out.println("a: { " + a[0] + ", " + a[1] + " }");     /* An array is explicitly created by an array creation expression: */
    System.out.println(Point.origin);
  }  
}

Output:
p: null
default
p: (0,0)
a: { (0,0), (1,1) }
(5,5)


Primitive and reference identity.

class Value {
  int val;
}

class Test {

  public static void main(String[] args) {
    int i1 = 3;
    int i2 = i1;
    i2 = 4;
   
    System.out.print("i1==" + i1);
    System.out.println(" but i2==" + i2);
   
    Value v1 = new Value();
    v1.val = 5;
    Value v2 = v1;                                             //The Reference v1 assigned to v2
    v2.val = 6;
   
    System.out.print("v1.val==" + v1.val);
    System.out.println(" and v2.val==" + v2.val);
  }  
}

Output
i1==3 but i2==4
v1.val==6 and v2.val==6

The class Object.
The class Object is super class to all other classes. so all classes inherit the Objects methods.

clone is used to make a duplicate of an object.

equals defines a notion of object equality, which is based on value, not reference, comparison.

finalize is run just before an object is destroyed.

getClass returns the Class object that represents the class of the object.

hashCode is very useful, together with the method equals, in hashtables such as java.util.Hashmap.

wait, notify, and notifyAll are used in concurrent programming using threads

toString returns a String representation of the object.


Type Variables.

A type variable is an unqualified identifier used as a type in class, interface, method, and constructor bodies.
A type variable is introduced by the declaration of a type parameter of a generic class,generic interface, generic method, or generic constructor.


class C { 
  public void mCPublic() {}  
  
  protected void mCProtected() {}  
  
  void mCPackage() {}  
  
  private void mCPrivate() {}  
}

interface I { 
  void mI();

class CT extends C implements I { 
  public void mI() {}  

class Test {
  <T extends C & I>     // T is type variable
  void test(T t) {
    t.mI(); // OK 
    t.mCPublic(); // OK 
    t.mCProtected(); // OK 
    t.mCPackage(); // OK 
    //t.mCPrivate(); // Compile-time error 
  }   
}

Here T is same as CT


Parameterized type:

A parameterized type C<T1,...,Tn> is well-formed if all of the following are true:
  C is the name of a generic type.
  The number of type arguments is the same as the number of type parameters in the generic declaration of C.
  When subjected to capture conversion (§5.1.10) resulting in the type C<X1,...,Xn>, each type argument Xi is a subtype of S[F1:=X1,...,Fn:=Xn] for each bound type S in Bi (Fi:=Xi)


Type arguments of parameterized types.

Type arguments may be either reference types or wildcards. Wildcards are useful in situations where only partial knowledge about the type parameter .

Unbounded wildcards.

import java.util.Collection; 
import java.util.ArrayList; 
 
class Test { 
 static void printCollection(Collection<?> c) { 
 // a wildcard collection 
 for (Object o : c) { 
 System.out.println(o); 
 } 
 } 
 
 public static void main(String[] args) { 
 Collection<String> cs = new ArrayList<String>(); 
 cs.add("hello"); 
 cs.add("world"); 
 printCollection(cs); 
 } 
}

Type erasure.

erasure of a parameterized type (§4.5) G<T1,...,Tn> is |G|.
erasure of a nested type T.C is |T|.C.
erasure of an array type T[] is |T|[].
erasure of a type variable (§4.4) is the erasure of its leftmost bound.
erasure of every other type is the type itself.

Raw Type:

class Cell<E> { 
  
  E value;
  
  Cell(E v) {
  value = v;
  }   
  
  E get() {
  return value;
  }   
  
  void set(E v) {
  value = v;
  }   
  
  public static void main(String[] args) { 
    Cell x = new Cell<String>("abc"); 
    System.out.println(x.value); // OK, has type Object 
    System.out.println(x.get()); // OK, has type Object 
    //x.set("def");
 } 
}

Raw Type Inheritance.

import java.util.*; 
class NonGeneric { 
 Collection<Number> myNumbers() { return null; } 
 
abstract class RawMembers<T> extends NonGeneric 
 implements Collection<String> { 
 static Collection<NonGeneric> cng = 
 new ArrayList<NonGeneric>(); 
 
 public static void main(String[] args) { 
 RawMembers rw = null; 
 Collection<Number> cn = rw.myNumbers(); 
 // OK 
 Iterator<String> is = rw.iterator(); 
 // Unchecked warning 
 Collection<NonGeneric> cnn = rw.cng; 
 // OK, static member 
 } 
}

Interface and class.

interface Colorable { 
  void setColor(byte r, byte g, byte b); 
}

class Point { int x, y; } 

class ColoredPoint extends Point implements Colorable { 
  byte r, g, b;  
  
  public void setColor(byte rv, byte gv, byte bv) { 
  r = rv; g = gv; b = bv; 
  }   
}

class Test { 
 
  public static void main(String[] args) { 
    Point p = new Point(); 
    ColoredPoint cp = new ColoredPoint(); 
    cp.setColor((byte)1, (byte)2, (byte)3);
    p = cp; 
    System.out.println("Point p : " + p.x); 
    Colorable c = cp; 
    System.out.println("Colorable c :" + c); 
    ColoredPoint c1 = cp; 
    System.out.println("Colorable c :" + c1.r); 
  }   
}


No comments:

Post a Comment