synchronization possible on objects only and not on primitives
int x;
synchronized(x) will be compiler error. No autoboxing here…
synchronization possible on objects only and not on primitives
int x;
synchronized(x) will be compiler error. No autoboxing here…
StringBuffer sb = “hello”;
this won’t compile. StringBuffer have to be explicity created by new operator.
When class implements two or more interface(or extends classes) containing same member varaible type(say, int i). Referencing this variable in the new class will cause ambiguous match for compiler as long as that variable is not redefined in the new class. Redefining the variable in new class will resolve the issue.
Not possible to access methods of private inner class as the following code will give compile error.
class MyOuter{
private class MyInner{
public float f(){return 1.2f;}
}
public MyInner getMyInner(){
return new MyInner();
}
}
public class Test13{
public static void main(String... args){
MyOuter o = new MyOuter();
float f = new MyOuter().getMyInner().f();
}
}
For %b(boolean) : All data types can be passed
For %c(char) : byte, char, short, int
For %d(int) : byte, short, int, long (No char)
For %f(float) : only float and double
For %s(string) : everything
=======================================
public class Test12{
public static void main(String… args){
boolean b = false;
byte by = 2;
char c = ‘A’;
short s = 7;
int i = 35565;
long l = 2345676436323746l;
float f = 234.567f;
double d = 234.678;
//System.out.printf(“%d\t”, b);
System.out.printf(“%d\t”, by);
//System.out.printf(“%d\t”, c);
System.out.printf(“%d\t”, s);
System.out.printf(“%d\t”, i);
System.out.printf(“%d\t”, l);
//System.out.printf(“%d\t”, f);
//System.out.printf(“%d\t”, d);
System.out.println();
System.out.printf(“%b\t”, b);
System.out.printf(“%b\t”, by);
System.out.printf(“%b\t”, c);
System.out.printf(“%b\t”, s);
System.out.printf(“%b\t”, i);
System.out.printf(“%b\t”, l);
System.out.printf(“%b\t”, f);
System.out.printf(“%b\t”, d);
System.out.println();
System.out.printf(“%s\t”, b);
System.out.printf(“%s\t”, by);
System.out.printf(“%s\t”, c);
System.out.printf(“%s\t”, s);
System.out.printf(“%s\t”, i);
System.out.printf(“%s\t”, l);
System.out.printf(“%s\t”, f);
System.out.printf(“%s\t”, d);
System.out.println();
//System.out.printf(“%f\t”, b);
//System.out.printf(“%f\t”, by);
//System.out.printf(“%f\t”, c);
//System.out.printf(“%f\t”, s);
//System.out.printf(“%f\t”, i);
//System.out.printf(“%f\t”, l);
System.out.printf(“%f\t”, f);
System.out.printf(“%f\t”, d);
System.out.println();
//System.out.printf(“%c\t”, b);
System.out.printf(“%c\t”, by);
System.out.printf(“%c\t”, c);
System.out.printf(“%c\t”, s);
System.out.printf(“%c\t”, i);
//System.out.printf(“%c\t”, l);
//System.out.printf(“%c\t”, f);
//System.out.printf(“%c\t”, d);
}
}
========================
Output:
2 7 35565 2345676436323746
false true true true true true true true
false 2 A 7 35565 2345676436323746 234.567 234.678
234.567001 234.678000
A ?
Advantages of encapsulation:
Good Link for on Oracle collection : http://sheikyerbouti.developpez.com/collections/collections.htm
Object equals checks both objects are of same type and their contents are equal. In case of Float/Double:
two instances of class Double, d1 and d2, the value of d1.equals(d2) is true if and only if
d1.doubleValue() == d2.doubleValue()
Same holds good in case of Float also.
Exceptions:
In a arrayList of object, any types of object(mixed) can be put. However in an array of objects, once initialized to a particular object type, other type of objects cannot be put.
Thus,
ArrayList a = new ArrayList();
a.add(“krishna”);
a.add(5);
is allowed and will run fine.
However,
Object[] obj = new String[5];
obj[0] = new Integer(1);
will throw ArrayStoreException(is a RunTimeException).
With generics, arraylist is also not allowed to store Integer if it is declared as ArrayList of String. Thus, with generics,
ArrayList<String> a = new ArrayList<String>();
a.add(“krishna”);
a.add(5); — will be compiler error(unlike array which is at rutime).
But with generics also, one more difference is array allows polymorphic assignment., i.e.,
Number[] n = new Integer[5];
However, in case of generics, polymorphic assignment holds good only for the base
type and not for the generic type. i.e.,
List<Integer> l = new ArrayList<Integer>; — for base type hold good
ArrayList<Number> n = new ArrayList<Integer>; — not allowed for generic type