a) Avoid creating unnecessary objects. Long took 42 sec
where as long took 4 sec to complete calculation.
/* Avoid
creating unnecessary objects */
public class WrapperClass {
public static void main(String[] args) {
System.out.println(System.currentTimeMillis());
long sum = 0L; // uses Long,
not long
for (long i = 0; i <= Integer.MAX_VALUE; i++) {
sum += i;
}
System.out.println(sum);
System.out.println(System.currentTimeMillis());
}
}
Output
Using Long sum =0L – program took 7 sec whereas using
long sum =0L program took 1 sec to complete
b) The lack of native
value equality is also a concern (.equals() is fairly verbose compared to ==)
Also result is unpredictable depending on JVM cache implementation.
public class TestInteger {
public static void main(String[] args) {
int x=1000;
int y=1000;
System.out.println(x==y);
Integer x1 = 1000;
Integer y1 = 1000;
System.out.println(x1 == y1);
}
}
Output
True
False
c) Assignment work is
often confusing taking wrapper and primitive both into account.
public class TestInteger {
public static void main(String[] args) {
Integer i=null;
int i1=i;
}
}
Exception in thread "main" java.lang.NullPointerException at com.wrapper.TestInteger.main(TestInteger.java:7)
d)
Boxed types have
poorer performance and require more memory.
e) Primitive types are much faster:
int i;
i++;
Integer (all Numbers and also a String) is an immutable type:
once created it can not be changed. If i was Integer, than i++ would
create a new Integer object - much more expensive in terms of memory and
processor.
| |||
Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care
ReplyDeletehttp://chennaitraining.in/qliksense-training-in-chennai/
http://chennaitraining.in/pentaho-training-in-chennai/
http://chennaitraining.in/machine-learning-training-in-chennai/
http://chennaitraining.in/artificial-intelligence-training-in-chennai/
http://chennaitraining.in/msbi-training-in-chennai/
http://chennaitraining.in/citrix-training-in-chennai/