Monday, May 7, 2012

auto-boxing, boxing, unboxing



  • Lesson3 Boxing & Unboxing

http://www.youtube.com/watch?v=ujbeM5MD84M&feature=autoplay&list=ULr_jhKrpDD5c&playnext=1



  • java 09 - Wrappers and Boxing

http://www.youtube.com/watch?v=KUdvCjLqP_0
wrapper class wraps primitive type into an object





  • Java Tutorial 7.1 Autoboxing Part 1/3

http://www.youtube.com/watch?v=JPIN1vZBM8M&feature=relmfu


  • Java Tutorial 7.2 Autoboxing Part 2/3

http://www.youtube.com/watch?v=uGLoHyjURtc&feature=relmfu


  • java Tutorial 7.3 Autoboxing Part 3/3

http://www.youtube.com/watch?v=jqKn_dCBU20&list=UU77mSrM47r2NgFoxGSl4DXw&index=154&feature=plpp_vide



  • Autoboxing

Autoboxing and unboxing is introduced in Java 1.5 to automatically convert primitive type into boxed primitive( Object or Wrapper class).
before Java 1.5 then you are familiar with the issues like you can not directly put primitives into Collections, instead you first need to convert them into Object only then only you can put them into Collections. Wrapper class like Integer
With the introduction of autoboxing and unboxing in Java this primitive to object conversion happens automatically by Java compiler which makes code more readable


it becomes even more important because they are automatic and can create subtle bugs if you are not sure when autoboxing  in Java code occurs and when unboxing happens.

When Java automatically converts a primitive type like int into corresponding wrapper class object e.g. Integer than its called autoboxing  because primitive is boxed into wrapper class while in opposite case is called unboxing, where an Integer object is converted into primitive int.


All primitive types e.g. byte, short, char, int, long, float, double and boolean has corresponding wrapper class e.g. Byte, Short, Integer, Character etc and participate in autoboxing and unboxing.


During autoboxing boolean is converted to Boolean, byte to Byte, char converted to Character, float changes to Float, int goes to Integer, long goes to Long and short converts to Short, while in unboxing opposite happens like Float to float.

for example In method invocation where an object argument is expected,  if you pass primitive, Java automatically converts primitive into equal value Object.

Autoboxing mainly occur in two places one is during assignment and other is during method invocation


One of the danger of autoboxing is throw away object which gets created if autoboxing occurs in a loop. Here is an example of how unnecessary object can slow down your application


Integer sum = 0;
 for(int i=1000; i<5000 i="i" p="p">   sum+=i;
 }

In this code sum+=i will expand as sum = sum + i and since + operator is not applicable to Integer object it will trigger unboxing of sum Integer object and then autoboxing of result which will be stored in sum which is Integer as shown below :

sum = sum.intValue() + i;
Integer sum = new Integer(result);    
   
here since sum is Integer, it will create around 4000 unnecessary Intger object which are just throw away and if this happens on large scale has It potential to slow down system with frequent GC for arithmetic calculation always prefer primitive over boxed primitive and look for unintentional autoboxing in Java

autoboxing is error prone e.g. equality operator "==". Since equality operator can be applied on both primitive and Objects it leads to confusion and can cause subtle issues

When you compare two object using "==" operator it compares object's identity and not value and also no auto boxing occur.
its not best practice to use  equality operator to compare Objects, use equals method instead

if we compare one primitive with another object than unboxing of object is occur which could throw NullPointerException if object is null e.g.

Since autoboxing creates unnecessary object and if that goes beyond a limit usually outside the range of cached value it can potentially slow your program by frequently causing garbage collection.



http://javarevisited.blogspot.com/2012/07/auto-boxing-and-unboxing-in-java-be.html#ixzz22r1HOohK





what is transaction?



  • Lesson6 Transactions

http://www.youtube.com/watch?v=r_jhKrpDD5c&feature=channel&list=UL



  • transaction

In computer programming, a transaction usually means a sequence of information exchange and related work (such as database updating) that is treated as a unit for the purposes of satisfying a request and for ensuring database integrity.

A typical transaction is a catalog merchandise order phoned in by a customer and entered into a computer by a customer representative. The order transaction involves checking an inventory database, confirming that the item is available, placing the order, and confirming that the order has been placed and the expected time of shipment. If we view this as a single transaction, then all of the steps must be completed before the transaction is successful and the database is actually changed to reflect the new order. If something happens before the transaction is successfully completed, any changes to the database must be kept track of so that they can be undone.
http://searchcio.techtarget.com/definition/transaction



  • What Is a Transaction Processing System?

Transaction processing system is a program or software that processes all transactions that go in and out of a business or a company.
The system goes over the transactions one by one, like in an automated teller machine (ATM) that permits only one user at a time
The term “transaction,” in this case, does not only refer to financial aspects, but to requests for information as a whole, such as booking a flight or just logging into an online bank account.

In direct contrast to a transaction processing system is a batch processing system.
This system processes multiple transactions—or a “batch” of transactions—simultaneously.
The main disadvantage is that the processed transactions do not return immediate results.

A modern analogy is a torrent file connected to a folder of files.
The user cannot immediately access a file until all the files in the folder are completely downloaded.
http://www.wisegeek.com/what-is-a-transaction-processing-system.htm