tag:blogger.com,1999:blog-46508746580089902222024-02-20T00:29:58.818-08:00Java 'n MoreDimensions of Java TechnologyK.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.comBlogger55125tag:blogger.com,1999:blog-4650874658008990222.post-49094593660415500632018-03-01T02:12:00.002-08:002018-03-01T03:57:14.922-08:00Programming: Who started?<br />
<div class="MsoNormal">
Do you know who was the first programmer?</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
a) Eckert J</div>
<div class="MsoNormal">
b) William Gibson</div>
<div class="MsoNormal">
c) Ada lovelace</div>
<div class="MsoNormal">
d) Mauchly J</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Ans: Ada Lovelace, daughter of famous poet Lord Byron is
considered asa first programmer. She wrote an algorithm to be executed on Charles
Babbage’s computer. The first computer
program was related to to calculation of Bernoulli’s numbers. It is said that
this program remain on paper only. Charles Babbage gave her another name “The
Enchantress of Numbers”.</div>
<div class="MsoNormal">
<br /></div>
<br />
<br />
<br />K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-38544824829531501422018-02-28T08:25:00.000-08:002018-02-28T08:25:22.956-08:00Random memory : The First Computer<br />
<div class="MsoNormal">
Many years back when I
studied Computer programming, <span style="mso-spacerun: yes;"> </span>I was
taught <span style="mso-spacerun: yes;"> </span>that ENIAC (Electronic Numeric
Integrator And Calculator) is the first digital computer. It was created by
Eckert J P and Mauchly J. It took around 3 years. The purpose was to help in World
War II against German forces. It was a huge machine.<span style="mso-spacerun: yes;"> </span>It used about 20000 vacuum tubes,<span style="mso-spacerun: yes;"> </span>70000 resistors, 10000 capacitors and 1500
relays.<span style="mso-spacerun: yes;"> </span>It occupied about 1800 square
feet.<span style="mso-spacerun: yes;"> </span>It used huge amount of electricity
about 200 kilo watts. It weighed about 30 tons. How much did it cost? Just about
$4,87000 <span style="font-family: Wingdings; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-char-type: symbol; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-symbol-font-family: Wingdings;"><span style="mso-char-type: symbol; mso-symbol-font-family: Wingdings;">J</span></span>
It is 72 years old now. It was retired in a very young age and enjoys its
retired life in Smithsonian Institution. </div>
<div class="MsoNormal">
Later I found that ENIAC has elder siblings.<span style="mso-spacerun: yes;"> </span>Some names to mention are:</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
</div>
<ul>
<li>Z1 (it has German citizenship), and it is almost ten years elder
to ENIAC.</li>
<li>Colossus (British citizenship) was used for code breaking.</li>
<li>ABC (Atnasoff-Berry Computer) is of American origin.</li>
</ul>
<br />
<div class="MsoNormal">
ENIAC certainly has younger sibling and is ancestor of all computers
we are using today and will use in future. </div>
<br />K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-60975212759213701232017-01-02T04:21:00.002-08:002017-01-02T04:22:08.785-08:00Asset-Centric Engineering<div class="MsoNormal">
Asset Centric Engineering can be applied to </div>
<div class="MsoNormal" style="margin-left: .5in; mso-list: l0 level1 lfo1; text-indent: -.25in;">
<!--[if !supportLists]-->a)<span style="font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;"> </span><!--[endif]-->Unified
Process Development Methodology</div>
<div class="MsoNormal" style="margin-left: .5in; mso-list: l0 level1 lfo1; text-indent: -.25in;">
<!--[if !supportLists]-->b)<span style="font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;"> </span><!--[endif]-->Scrum
Methodology</div>
<div class="MsoNormal" style="margin-left: .5in; mso-list: l0 level1 lfo1; text-indent: -.25in;">
<!--[if !supportLists]-->c)<span style="font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;"> </span><!--[endif]-->Extreme
Programming Methodology</div>
<div class="MsoNormal" style="margin-left: .5in; mso-list: l0 level1 lfo1; text-indent: -.25in;">
<!--[if !supportLists]-->d)<span style="font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;"> </span><!--[endif]-->Waterfall
Methodology</div>
<div class="MsoNormal" style="margin-left: .5in; mso-list: l0 level1 lfo1; text-indent: -.25in;">
<!--[if !supportLists]-->e)<span style="font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;"> </span><!--[endif]-->None
of the above</div>
<div class="MsoNormal" style="margin-left: .5in; mso-list: l0 level1 lfo1; text-indent: -.25in;">
<!--[if !supportLists]-->f)<span style="font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;"> </span><!--[endif]-->All
of the above</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-44630758012282317662016-12-30T08:03:00.000-08:002016-12-30T08:03:37.428-08:00SOA and SOI<div style="line-height: 18.15pt; vertical-align: baseline;">
Q: IS SOI (Service
Oriented Integration) Reference Architecture a subset of SOA (Service Oriented
Architecture) reference Architecture?<o:p></o:p></div>
<div style="line-height: 18.15pt; vertical-align: baseline;">
<br /></div>
<div style="line-height: 18.15pt; vertical-align: baseline;">
a) True</div>
<div style="line-height: 18.15pt; vertical-align: baseline;">
b) False</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-68335506846260128232016-12-27T04:04:00.000-08:002016-12-27T04:09:17.233-08:00SOA Capabilty Maturity Levels<div style="line-height: 18.15pt; vertical-align: baseline;">
SOA capability Maturity
levels</div>
<div style="line-height: 18.15pt; vertical-align: baseline;">
<br /></div>
<div style="line-height: 18.15pt; vertical-align: baseline;">
Level - 0 No SOA : You have not pursued SOA Approach<br />
Level - 1 AD HOC : You
are just trying to play around with SOA<br />
Level -2 Opportunistic : you are trying
SOA for quick implementation, may be a demo<br />
Level -3 Systematic : you are
getting serious with SOA, consistent application with share and reuse Level - 4
Managed : you have step further, considering to drive business value<br />
Level - 5
Optimized : you are getting full benefit of SOA strategy, cost-effectiveness,
business intiatives, standards, processes</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-32825697783089658392016-09-15T04:25:00.000-07:002016-09-15T04:25:26.896-07:00bool Data Type in C++<div class="MsoNormal">
While making decisions, it is human to think in terms of true or false. In C Programming language false is represented by Zero value and true by Non-Zero. C++ introduced bool data type to hold true or false values.</div>
<div class="MsoNormal">
Code example :</div>
<div class="MsoNormal">
#include <iostream></div>
<div class="MsoNormal">
using namespace std;</div>
<div class="MsoNormal">
int main()</div>
<div class="MsoNormal">
{</div>
<div class="MsoNormal">
int x=10,y=20;</div>
<div class="MsoNormal">
bool b=x>y;</div>
<div class="MsoNormal">
cout<<" x> y = "<<b<<endl;</div>
<div class="MsoNormal">
return 0;</div>
<div class="MsoNormal">
}</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
On execution :<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaNBtVa-X1zqt34X_FIiZjlmmihbF9-VKa2WJLUSRGxUDIzig5YOTBPh2iPmjSGuqV1zO8xBrjvnJoosWTXVZJxhamHRQN9gbD9IEc5RRpX0iKu93CYYHplQRPT4wvAkfobLYWr-HvkFGo/s1600/bool2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" height="196" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaNBtVa-X1zqt34X_FIiZjlmmihbF9-VKa2WJLUSRGxUDIzig5YOTBPh2iPmjSGuqV1zO8xBrjvnJoosWTXVZJxhamHRQN9gbD9IEc5RRpX0iKu93CYYHplQRPT4wvAkfobLYWr-HvkFGo/s1600/bool2.png" width="640" /></a><br />
<br />
<div class="MsoNormal">
<br />
Even though we can use keywords : true or false for assigning purposes to bool variable, the values stored in a bool variable for true is 1 and false is 0.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
bool b=true;</div>
<div class="MsoNormal">
cout<<b<<endl;</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The outcome is 1.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
std::boolalpha function can be used for text values for bool type. With boolalpha, cout statement will look like :</div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
cout<<" x> y = "<<std::boolalpha<<b<<endl; </div>
<div class="MsoNormal">
On execution :<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaNBtVa-X1zqt34X_FIiZjlmmihbF9-VKa2WJLUSRGxUDIzig5YOTBPh2iPmjSGuqV1zO8xBrjvnJoosWTXVZJxhamHRQN9gbD9IEc5RRpX0iKu93CYYHplQRPT4wvAkfobLYWr-HvkFGo/s1600/bool2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" height="196" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaNBtVa-X1zqt34X_FIiZjlmmihbF9-VKa2WJLUSRGxUDIzig5YOTBPh2iPmjSGuqV1zO8xBrjvnJoosWTXVZJxhamHRQN9gbD9IEc5RRpX0iKu93CYYHplQRPT4wvAkfobLYWr-HvkFGo/s1600/bool2.png" width="640" /></a><br />
<br />
boolalpha is a format flag...</div>
</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-16153765043506428412016-07-20T11:57:00.003-07:002016-07-20T12:08:56.355-07:00Another way to convert an int to String<div class="MsoNormal">
import java.util.Scanner;</div>
<div class="MsoNormal">
public class IntToStrMethodsDemo {</div>
<div class="MsoNormal">
public static void
main(String[] args) {</div>
<div class="MsoNormal">
int num;</div>
<div class="MsoNormal">
num=readInt();</div>
<div class="MsoNormal">
String
str=intToStr(num);</div>
<div class="MsoNormal">
System.out.println(intToStr(num)+1);</div>
<div class="MsoNormal">
}</div>
<div class="MsoNormal">
public static int
readInt(){</div>
<div class="MsoNormal">
System.out.println("enter an int");</div>
<div class="MsoNormal">
return new
Scanner(System.in).nextInt();</div>
<div class="MsoNormal">
}</div>
<div class="MsoNormal">
public static
String intToStr(int num){</div>
<div class="MsoNormal">
return
""+num;</div>
<div class="MsoNormal">
}</div>
<br />
<div class="MsoNormal">
}<br />
<div class="MsoNormal">
Or use the code below :</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
String str = ""+new Scanner(System.in).nextInt();</div>
<div class="MsoNormal">
System.out.println(str+1);</div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
It does the same</div>
</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com2tag:blogger.com,1999:blog-4650874658008990222.post-56195429503995830442016-07-16T15:05:00.000-07:002016-07-16T15:05:10.279-07:00Simple utility to convert an int to String<div class="MsoNormal">
package utilities;</div>
<div class="MsoNormal">
import java.util.Scanner;</div>
<div class="MsoNormal">
public class IntToStr {</div>
<div class="MsoNormal">
public static void
main(String[] args) {</div>
<div class="MsoNormal">
Scanner
scanner = new Scanner(System.in);</div>
<div class="MsoNormal">
System.out.println("Enter an integer :");</div>
<div class="MsoNormal">
int num =
scanner.nextInt();</div>
<div class="MsoNormal">
String str =
intToStr(num);</div>
<div class="MsoNormal">
System.out.println("number String : "+str);</div>
<div class="MsoNormal">
}</div>
<div class="MsoNormal">
public static
String intToStr(Integer num){</div>
<div class="MsoNormal">
return
num.toString();</div>
<div class="MsoNormal">
}</div>
<br />
<div class="MsoNormal">
}</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-63624087510614835802016-03-21T18:31:00.001-07:002016-03-21T18:31:20.761-07:00Business Model Canvas<div class="MsoNormal">
BMC is a single page view, which is works like a strategic
management tool for an enterprise to show </div>
<div class="MsoNormal">
</div>
<ul>
<li>What you want to do or what you do?</li>
<li>How you want to go about it?</li>
</ul>
<br />
<div class="MsoNormal">
It lays out crucial activities and challenges and their relationship.
The intention of a company to deliver a service or a product is described by
nine building blocks : </div>
<div class="MsoNormal">
</div>
<ul>
<li>Customer Segments</li>
<li>Value propositions</li>
<li>Channels</li>
<li>Customer Relationships</li>
<li>Revenue Streams</li>
<li>Key Resources</li>
<li>Key Activities</li>
<li>Key partnerships</li>
<li>Cost Structures</li>
</ul>
<div>
A Single page view of BMC from hbr.org :</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgaE-pi-f-lmNUzTDwtx56oCIkX34eBgdKA7qHYWwT14jLPfJp5iTm_GWEbRFGJzJ02eLM6Bd9n44RbOjDC_Tus-Tc3TgBr80LsN_nNXXBDleBwcvMiDnQmVW3LKelz1kYW0xLYBjn1B1JW/s1600/BMC.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="208" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgaE-pi-f-lmNUzTDwtx56oCIkX34eBgdKA7qHYWwT14jLPfJp5iTm_GWEbRFGJzJ02eLM6Bd9n44RbOjDC_Tus-Tc3TgBr80LsN_nNXXBDleBwcvMiDnQmVW3LKelz1kYW0xLYBjn1B1JW/s320/BMC.gif" width="320" /></a></div>
<div>
<br /></div>
<br />K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-29452505905879258052016-03-20T13:12:00.004-07:002016-03-20T13:12:55.584-07:00Strategy Maps<div class="MsoNormal">
A Strategy map provides a visual framework for company’s objectives.
It shows cause-and-effect links, which helps attaining desired outcomes. Four
basic perspectives of Balanced Scorecards are considered related to the VISION:</div>
<div class="MsoNormal">
</div>
<ul>
<li>Financial</li>
<li>Customer</li>
<li>Internal</li>
<li>Learning and growth</li>
</ul>
<br />
<br />
<div class="MsoNormal">
Followed by critical success factors and measures which will
validate these critical success factors.</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-58533343447252576822016-03-16T17:06:00.004-07:002016-03-16T17:06:55.105-07:00A Good Strategy<div class="MsoNormal">
A good business strategy statement will have</div>
<div class="MsoNormal">
</div>
<ul>
<li>Objective</li>
<li>Scope</li>
<li>Advantage</li>
</ul>
<br />
<br />
<div class="MsoNormal">
Remember goals is NOT business strategy</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-30768825092098179782016-03-16T01:58:00.003-07:002016-03-16T01:58:34.416-07:00An interesting Challenge....<div class="MsoNormal">
Can you state/summarize your company strategy in succinct manner
(say about 50 words)?</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-4061040681095346692016-03-15T15:36:00.002-07:002016-03-15T15:39:26.311-07:00Learning Business Architecture - 1<div class="MsoNormal">
Michael Porter’s 5 competitive forces that help shape a company,
its business strategy are:</div>
<div class="MsoListParagraphCxSpFirst" style="mso-list: l0 level1 lfo1; text-indent: -.25in;">
<br />
<ul>
<li><span style="font-size: 7pt; font-stretch: normal; text-indent: -0.25in;"> </span><span style="text-indent: -0.25in;">Rivals</span></li>
<li><span style="font-size: 7pt; font-stretch: normal; text-indent: -0.25in;"> </span><span style="text-indent: -0.25in;">Customers</span></li>
<li><span style="font-stretch: normal; text-indent: -0.25in;"> </span><span style="font-size: 7pt; font-stretch: normal; text-indent: -0.25in;"> </span><span style="text-indent: -0.25in;">New Entrants</span></li>
<li><span style="font-stretch: normal; text-indent: -0.25in;"> </span><span style="font-size: 7pt; font-stretch: normal; text-indent: -0.25in;"> </span><span style="text-indent: -0.25in;">Suppliers</span></li>
<li><span style="font-stretch: normal; text-indent: -0.25in;"> </span><span style="font-size: 7pt; font-stretch: normal; text-indent: -0.25in;"> </span><span style="text-indent: -0.25in;">Substitutes</span></li>
</ul>
<!--[if !supportLists]--></div>
<br />
<div class="MsoNormal">
These forces can vary from business to business. If in one
business rivals are the strongest force, new entrants can be in the other. To
succeed, business must overcome threats posed by these forces and make and
follow business strategy.</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-48133179717358217672016-02-01T07:40:00.000-08:002016-02-25T02:10:38.209-08:00My Technical memories: memory - 4<div class="MsoNormal">
I had been busy, actually very very busy with some technical
preparations on SOA. I kind of forgot to look into the past. But like a ghost
past pops up at one or the other occasion. So it happened that I had to look
into Serialization for some issue and here I see the Ghost of past reminding me
of interesting issue about static and serialization. Static data is not
serialized. Oh! I forgot to tell you, I am talking about Java Object
Serialization. So here is a small example to show from my Java memories…</div>
<div class="MsoNormal">
I created an Employee class which implements Serializable.
This class object is to be Serialized.<br />
<br /></div>
<div class="MsoNormal">
package test.domain;</div>
<div class="MsoNormal">
import java.io.Serializable;<br />
<br /></div>
<div class="MsoNormal">
public class Employee implements Serializable{</div>
<div class="MsoNormal">
private int empId;</div>
<div class="MsoNormal">
private String
empName;</div>
<div class="MsoNormal">
private double
salary;</div>
<div class="MsoNormal">
public static
final int branchCode=3;</div>
<div class="MsoNormal">
public static int
count;<br />
<br /></div>
<div class="MsoNormal">
/*public Employee() {</div>
<div class="MsoNormal">
count++;</div>
<div class="MsoNormal">
empId=count;</div>
<div class="MsoNormal">
}*/<br />
<br /></div>
<div class="MsoNormal">
public
Employee(String empName, double salary) {</div>
<div class="MsoNormal">
count++;</div>
<div class="MsoNormal">
empId=count;</div>
<div class="MsoNormal">
this.empName =
empName;</div>
<div class="MsoNormal">
this.salary =
salary;</div>
<div class="MsoNormal">
}<br />
<br /></div>
<div class="MsoNormal">
public int
getEmpId() {</div>
<div class="MsoNormal">
return empId;</div>
<div class="MsoNormal">
}<br />
<br /></div>
<div class="MsoNormal">
public void
setEmpId(int empId) {</div>
<div class="MsoNormal">
this.empId =
empId;</div>
<div class="MsoNormal">
}<br />
<br /></div>
<div class="MsoNormal">
public String
getEmpName() {</div>
<div class="MsoNormal">
return
empName;</div>
<div class="MsoNormal">
}<br />
<br /></div>
<div class="MsoNormal">
public void
setEmpName(String empName) {</div>
<div class="MsoNormal">
this.empName =
empName;</div>
<div class="MsoNormal">
}<br />
<br /></div>
<div class="MsoNormal">
public double
getSalary() {</div>
<div class="MsoNormal">
return salary;</div>
<div class="MsoNormal">
}<br />
<br /></div>
<div class="MsoNormal">
public void
setSalary(double salary) {</div>
<div class="MsoNormal">
this.salary =
salary;</div>
<div class="MsoNormal">
}<br />
<br /></div>
<div class="MsoNormal">
public static int
getCount() {</div>
<div class="MsoNormal">
return count;</div>
<div class="MsoNormal">
}<br />
<br /></div>
<div class="MsoNormal">
public static void
setCount(int count) {</div>
<div class="MsoNormal">
Employee.count
= count;</div>
<div class="MsoNormal">
}<br />
<br /></div>
<div class="MsoNormal">
public String toString(){</div>
<div class="MsoNormal">
return
"count : "+count+</div>
<div class="MsoNormal">
" Employee Id : "+empId+</div>
<div class="MsoNormal">
" Branch Code : "+branchCode+</div>
<div class="MsoNormal">
" Employee Name : "+empName+</div>
<div class="MsoNormal">
" salary : "+salary;</div>
<div class="MsoNormal">
}</div>
<div class="MsoNormal">
}<br />
<br /></div>
<div class="MsoNormal">
Then I created a class which can serialize the objects of
Employee class. I named this class as PersistEmployee.<br />
<br /></div>
<div class="MsoNormal">
package teststatic;<br />
<br /></div>
<div class="MsoNormal">
import java.io.FileInputStream;</div>
<div class="MsoNormal">
import java.io.FileOutputStream;</div>
<div class="MsoNormal">
import java.io.IOException;</div>
<div class="MsoNormal">
import java.io.ObjectInputStream;</div>
<div class="MsoNormal">
import java.io.ObjectOutputStream;</div>
<div class="MsoNormal">
import test.domain.Employee;<br />
<br /></div>
<div class="MsoNormal">
public class PersistEmployee {<br />
<br /></div>
<div class="MsoNormal">
public static void
main(String[] args) {</div>
<div class="MsoNormal">
Employee emp;</div>
<div class="MsoNormal">
try
(FileOutputStream fos = new FileOutputStream("d:\\emp.dat");</div>
<div class="MsoNormal">
ObjectOutputStream out = new ObjectOutputStream(fos)) {</div>
<div class="MsoNormal">
emp=new Employee("John",120000);</div>
<div class="MsoNormal">
out.writeObject(emp);</div>
<div class="MsoNormal">
emp=new Employee("Tim",140000);</div>
<div class="MsoNormal">
out.writeObject(emp);</div>
<div class="MsoNormal">
}catch(IOException e){</div>
<div class="MsoNormal">
System.out.println("Input Output Error...");</div>
<div class="MsoNormal">
}<br />
<br /></div>
<div class="MsoNormal">
/* Employee e;</div>
<div class="MsoNormal">
try (FileInputStream fis = new
FileInputStream("d:\\emp.dat");</div>
<div class="MsoNormal">
ObjectInputStream in = new ObjectInputStream(fis)) {</div>
<div class="MsoNormal">
e =
(Employee)in.readObject();</div>
<div class="MsoNormal">
System.out.println (e);</div>
<div class="MsoNormal">
e =
(Employee)in.readObject();</div>
<div class="MsoNormal">
System.out.println (e);</div>
<div class="MsoNormal">
} catch
(ClassNotFoundException | IOException i) {</div>
<div class="MsoNormal">
System.out.println("Exception reading in Portfolio: " + i);</div>
<div class="MsoNormal">
}*/</div>
<div class="MsoNormal">
}</div>
<div class="MsoNormal">
}<br />
<br /></div>
<div class="MsoNormal">
When I run this class, it serializes two objects of Employee
class, static variable will be incremented with every constructor call. After
these two objects are created value of count will be 2, branchCode is static
final and will always hold value 3. I am
using static and static final variables in the Employee class. The code will show
how JVM deals with static and static final during serialization and
deserialization. <br />
<br /></div>
<div class="MsoNormal">
I created another class ReadEmployee where deserialization
is taking place for Employee objects.<br />
<br /></div>
<div class="MsoNormal">
package teststatic;<br />
<br /></div>
<div class="MsoNormal">
import java.io.FileInputStream;</div>
<div class="MsoNormal">
import java.io.IOException;</div>
<div class="MsoNormal">
import java.io.ObjectInputStream;</div>
<div class="MsoNormal">
import test.domain.Employee;<br />
<br /></div>
<div class="MsoNormal">
public class ReadEmployee {</div>
<div class="MsoNormal">
<br />
public static
void main(String[] args) {</div>
<div class="MsoNormal">
Employee e;</div>
<div class="MsoNormal">
<br />
try
(FileInputStream fis = new FileInputStream("d:\\emp.dat");</div>
<div class="MsoNormal">
ObjectInputStream in = new ObjectInputStream(fis)) {</div>
<div class="MsoNormal">
e =
(Employee)in.readObject();</div>
<div class="MsoNormal">
System.out.println (e);</div>
<div class="MsoNormal">
e =
(Employee)in.readObject();</div>
<div class="MsoNormal">
System.out.println (e);</div>
<div class="MsoNormal">
} catch
(ClassNotFoundException | IOException i) {</div>
<div class="MsoNormal">
System.out.println("Exception reading in Portfolio: " + i);</div>
<div class="MsoNormal">
}</div>
<div class="MsoNormal">
}</div>
<div class="MsoNormal">
}<br />
<br /></div>
<div class="MsoNormal">
Now when I run ReadEmployee it brings this result for me.<br />
<br /></div>
<div class="MsoNormal">
run:</div>
<div class="MsoNormal">
count : 0 Employee Id
: 1 Branch Code : 3 Employee Name : John salary : 120000.0</div>
<div class="MsoNormal">
count : 0 Employee Id
: 2 Branch Code : 3 Employee Name : Tim salary : 140000.0</div>
<div class="MsoNormal">
BUILD SUCCESSFUL (total time: 2 seconds)<br />
<br /></div>
<div class="MsoNormal">
I can see that static variable value is zero, static
variable is not persisted, branchCode is also not persisted, but it prints
value 3 because it is static final variable. An interesting part will be be if
you uncomment the read code from PersistEmployee and run it. In this case both
serialization and deserialization happens in the same class and hence you will
find an interesting difference in the outcome. Here it is:<br />
<br /></div>
<div class="MsoNormal">
run:</div>
<div class="MsoNormal">
count : 2 Employee Id
: 1 Branch Code : 3 Employee Name : John salary : 120000.0</div>
<div class="MsoNormal">
count : 2 Employee Id
: 2 Branch Code : 3 Employee Name : Tim salary : 140000.0</div>
<div class="MsoNormal">
BUILD SUCCESSFUL (total time: 2 seconds)<br />
<br /></div>
<div class="MsoNormal">
Can you figure out why count shows value 2 and not zero?</div>
<br />
<div class="MsoNormal">
<br /></div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-49130312267983433712016-01-23T13:04:00.000-08:002016-01-23T13:04:32.923-08:00My Technical Memories - Memory 3<div class="MsoNormal">
Ah! What is it? I could see a lot of symbols. I had to ponder about it a bit… Na.. I am joking. How can I ever forget
Unix Shell Scripts? I have a long lasting relationship with it. It is really
interesting to read my thoughts after a long… long… time. Here comes a small
and simple snippet of thoughts….</div>
<div class="MsoNormal">
Unix Shell Script to check every minute whether a user is
logged in or not?</div>
<div class="MsoNormal">
# usage is : scriptname.sh username</div>
<div class="MsoNormal">
if [ $# -lt 1 ]; then</div>
<div class="MsoNormal">
echo improper usage</div>
<div class="MsoNormal">
echo Correct usage
is : $0 username</div>
<div class="MsoNormal">
exit 1</div>
<div class="MsoNormal">
fi</div>
<div class="MsoNormal">
logname=$1</div>
<div class="MsoNormal">
time=0</div>
<div class="MsoNormal">
while true</div>
<div class="MsoNormal">
do</div>
<div class="MsoNormal">
who | grep
`$logname` > /dev/null</div>
<div class="MsoNormal">
if [ $? –eq 0 ] ;
then</div>
<div class="MsoNormal">
echo $logname
has logged in</div>
<div class="MsoNormal">
if [ $time – ne
0 ]; then</div>
<div class="MsoNormal">
echo $logname
is $time minutes late</div>
<div class="MsoNormal">
fi</div>
<div class="MsoNormal">
exit 0</div>
<div class="MsoNormal">
else</div>
<div class="MsoNormal">
time=`expre $time
+ 1`</div>
<div class="MsoNormal">
sleep 60</div>
<div class="MsoNormal">
fi</div>
<div class="MsoNormal">
done</div>
<br />
<div class="MsoNormal">
Unix and its look alike or derivatives are really amazing,
may it be linux, solaris, venix, xenix, HP-unix, SCO- Unix. I hope I
recollected my memory accurately. Why don’t you just execute and check… Lot
more on its way… see you again later….</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-76665109999572353202016-01-21T02:06:00.001-08:002016-02-25T02:10:58.931-08:00My Technical Memories: Memory - 2<div class="MsoNormal">
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<span style="font-size: 13.0pt; line-height: 150%;">Here is what I found in the second memory. It looked like a random access
to my thoughts. I wondered to see that right from IDENTIFICATION DIVISION it
took me to COBOL data files. I thought of sharing it rather than putting it
into the background. So here it comes. COBOL file or as a matter of fact any
file is a collection of data stored on secondary storage device. In a broader
perspective COBOL support three types of file organization.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
</div>
<ul>
<li><span style="font-size: 13pt; line-height: 150%;">Sequential File</span></li>
<li><span style="font-size: 13pt; line-height: 150%;">Indexed File</span></li>
<li><span style="font-size: 13pt; line-height: 150%;">Random File</span></li>
</ul>
<br />
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<b><span style="font-size: 13.0pt; line-height: 150%;">Sequential File </span></b><span style="font-size: 13.0pt; line-height: 150%;">stores the data in the same order in
which the data data is provided by the user. Sequential file can be Line,
Record or Printer sequential file. Organization of the data in file can be
specified in code as:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<span style="font-size: 13.0pt; line-height: 150%;"> SELECT MYFILE ASSIGN TO “MYFILE.DAT”<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<span style="font-size: 13.0pt; line-height: 150%;"> ORGANIZATION IS LINE SEQUENTIAL.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<span style="font-size: 13.0pt; line-height: 150%;">Organization can be otherwise RECORD SEQUENTIAL or PRINTER SEQUENTIAL. By
default it is RECORD SEQUENTIAL.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<b><span style="font-size: 13.0pt; line-height: 150%;">Indexed File </span></b><span style="font-size: 13.0pt; line-height: 150%;"> stores data using an Indexed key. Each record
contains the primary key which is unique. In Indexed file you can store unique
data. It can also be considered Master file or Master Data as we refer in
COBOL. Separate file will be created with .idx extension to the data file. It
will look something like this:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<span style="font-size: 13.0pt; line-height: 150%;"> SELECT INDFILE ASSIGN TO “INDFILE.DAT”<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<span style="font-size: 13.0pt; line-height: 150%;"> ORGANIZTION IS INDEXED<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<span style="font-size: 13.0pt; line-height: 150%;"> ACCESS MODE IS DYNAMIC<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<span style="font-size: 13.0pt; line-height: 150%;"> RECORD KEY IS IND-FILE-REC-KEY.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<span style="font-size: 13.0pt; line-height: 150%;">By the way, you have a facility to define DUPLICATE KEYS, in that case
file can contain redundant data, but hold on, there is a limit which varies
depending on type of Index file. Indexed file can be accessed or in other words
I can say that data in Indexed file can be organized in three different ways
i.e. INDEXED SEQUENTIAL (default, records are accessed on Ascending/Descending
Record Key), INDEXED RANDOM (Value of Record Key is used to access the Record)
and INDEXED DYNAMIC (Program can switch between Sequential or Random mode by
using appropriate I/O statements).<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<b><span style="font-size: 13.0pt; line-height: 150%;">Relative File </span></b><span style="font-size: 13.0pt; line-height: 150%;"> identifies each record by ordinal position.
This facilitates to access files sequentially or Randomly by using ordinal
position in sequence or randomly. If you provide relative position while
writing file as 1 and then 100, when you open this data file, can you imagine
what will you see? Now, that it my question to you. I was fathomed by what I
saw, it was interesting. Another interesting thing I noted while working on
Relative files is that although I can create variable length records, System
assume the size of largest record length and uses padding for unused spaces.
The beneficial side is it brings speed. System can calculate the position of
record to be accessed by using relative key and record length. So this is how
it looks like:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<span style="font-size: 13.0pt; line-height: 150%;"> SELECT MYRELFILE ASSIGN TO “MYRELFILE.DAT”<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<span style="font-size: 13.0pt; line-height: 150%;"> ORGANIZATION IS RELATIVE<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<span style="font-size: 13.0pt; line-height: 150%;"> ACCESS MODE IS RANDOM<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<span style="font-size: 13.0pt; line-height: 150%;"> RELATIVE KEY IS R-KEY.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<span style="font-size: 13.0pt; line-height: 150%;">Like Indexed file, Relative file can be accessed sequentially or randomly
is ACCESS MODE is DYNAMIC.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<span style="font-size: 13.0pt; line-height: 150%;">Enjoy this read and look for more… Now what will be next…I have no idea,
it depends, I need to decide whether I have to access my memory sequentially or
randomly… have fun..<o:p></o:p></span></div>
<br />
<div class="MsoNormal" style="line-height: 150%; margin-bottom: 6.0pt; margin-left: 0in; margin-right: 0in; margin-top: 6.0pt;">
<br /></div>
</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-4714324351407758292016-01-18T04:30:00.000-08:002016-01-18T04:39:05.191-08:00My Technical Memories : Memory - 1<div class="MsoNormal">
<span style="font-family: "georgia" , "serif"; font-size: 13.5pt; line-height: 115%;">I just opened my thoughts pot. Ah! I felt it was in a mess.
So many different types of thoughts: Technical, Personal etc. It was just like
if you have been downloading and saving a lot of information in a hard disk to
manage it later. I realized I have been doing it. So I imposed a responsibility
on myself to arrange it all. Now the question was how to proceed? To manage
technical section of my thoughts was a huge task, as I have been working in the
industry from a long time. So I opened that area and started looking at the
thoughts to classify and manage. The first thought which I saw was related to
COBOL. I have a special soft spot for COBOL, as this is the technology which I
used in my very first job. So here it is :</span><br />
<span style="font-family: "georgia" , "serif"; font-size: 13.5pt; line-height: 115%;"><br /></span>
<span style="font-family: "georgia" , "serif"; font-size: 13.5pt; line-height: 115%;">Every COBOL program must have four divisions in the following order:</span><br />
<span style="font-family: "georgia" , "serif"; font-size: 13.5pt; line-height: 115%;"><br /></span>
<span style="font-family: "georgia" , "serif"; font-size: 13.5pt; line-height: 115%;">IDENTIFICATION DIVISION.</span><br />
<span style="font-family: "georgia" , "serif"; font-size: 13.5pt; line-height: 115%;">ENVIRONMENT DIVISION.</span><br />
<span style="font-family: "georgia" , "serif"; font-size: 13.5pt; line-height: 115%;">DATA DIVISION.</span><br />
<span style="font-family: "georgia" , "serif"; font-size: 13.5pt; line-height: 115%;">PROCEDURE DIVISION.</span><br />
<span style="font-family: "georgia" , "serif"; font-size: 13.5pt; line-height: 115%;"><br /></span>
<span style="font-family: georgia, serif;"><span style="font-size: 18px; line-height: 20.7px;">Is this statement TRUE?</span></span><br />
<span style="font-family: georgia, serif;"><span style="font-size: 18px; line-height: 20.7px;"><br /></span></span>
<span style="font-family: georgia, serif;"><span style="font-size: 18px; line-height: 20.7px;">PS: I thought of putting my random technical thoughts here... keep looking for more...</span></span><br />
<br /></div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-58444690050701364552015-12-18T16:27:00.001-08:002023-12-09T08:43:28.435-08:00Enhanced for loop and Immutability?<div class="MsoNormal">
<div style="line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt;">
<div style="line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; vertical-align: baseline;">
<div style="line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; vertical-align: baseline;">
<span style="font-family: "inherit","serif"; font-size: 14pt;">There I find a twist in the enhanced for loop
and immutable nature. Where it does not change the data in an array of
primitives, here I can see a clear variation.<o:p></o:p></span></div>
<div style="box-sizing: border-box; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; outline: 0px; vertical-align: baseline;">
<span style="font-family: "inherit","serif"; font-size: 14pt;">Here is a Person class.<o:p></o:p></span></div>
<div style="box-sizing: border-box; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; outline: 0px; vertical-align: baseline;">
<span style="font-family: "inherit","serif"; font-size: 14pt;">package test;<o:p></o:p></span></div>
<div style="box-sizing: border-box; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; outline: 0px; vertical-align: baseline;">
<span style="font-family: "inherit","serif"; font-size: 14pt;">public class Person {<br style="box-sizing: border-box;" />
private int pId;<br style="box-sizing: border-box;" />
private String name;<o:p></o:p></span></div>
<div style="box-sizing: border-box; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; outline: 0px; vertical-align: baseline;">
<span style="font-family: "inherit","serif"; font-size: 14pt;">
public Person(int pId, String name) {<br style="box-sizing: border-box;" />
this.pId = pId;<br style="box-sizing: border-box;" />
this.name = name;<br style="box-sizing: border-box;" />
}<br style="box-sizing: border-box;" />
public String toString(){<br style="box-sizing: border-box;" />
return "Id : "+pId+"
Name : "+name;<br style="box-sizing: border-box;" />
}<br style="box-sizing: border-box;" />
public int getpId() {<br style="box-sizing: border-box;" />
return pId;<br style="box-sizing: border-box;" />
}<o:p></o:p></span></div>
<div style="box-sizing: border-box; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; outline: 0px; vertical-align: baseline;">
<span style="font-family: "inherit","serif"; font-size: 14pt;"> public
void setpId(int pId) {<br style="box-sizing: border-box;" />
this.pId = pId;<br style="box-sizing: border-box;" />
}<o:p></o:p></span></div>
<div style="box-sizing: border-box; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; outline: 0px; vertical-align: baseline;">
<span style="font-family: "inherit","serif"; font-size: 14pt;"> public
String getName() {<br style="box-sizing: border-box;" />
return name;<br style="box-sizing: border-box;" />
}<o:p></o:p></span></div>
<div style="box-sizing: border-box; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; outline: 0px; vertical-align: baseline;">
<span style="font-family: "inherit","serif"; font-size: 14pt;"> public void
setName(String name) {<br style="box-sizing: border-box;" />
this.name = name;<br style="box-sizing: border-box;" />
}<br style="box-sizing: border-box;" />
}<o:p></o:p></span></div>
<div style="box-sizing: border-box; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; outline: 0px; vertical-align: baseline;">
<span style="font-family: "inherit","serif"; font-size: 14pt;">And here is a tester
class with enhanced for loop.<o:p></o:p></span></div>
<div style="box-sizing: border-box; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; outline: 0px; vertical-align: baseline;">
<span style="font-family: "inherit","serif"; font-size: 14pt;">package test;<o:p></o:p></span></div>
<div style="box-sizing: border-box; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; outline: 0px; vertical-align: baseline;">
<span style="font-family: "inherit","serif"; font-size: 14pt;">import static
java.lang.Math.random;<br style="box-sizing: border-box;" />
import java.util.ArrayList;<br style="box-sizing: border-box;" />
import java.util.List;<o:p></o:p></span></div>
<div style="box-sizing: border-box; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; outline: 0px; vertical-align: baseline;">
<span style="font-family: "inherit","serif"; font-size: 14pt;">public class PersonList {<br style="box-sizing: border-box;" />
public static void main(String[] args) {<br style="box-sizing: border-box;" />
List<Person> pList = new
ArrayList<>();<br style="box-sizing: border-box;" />
pList.add( new
Person(101,"John"));<br style="box-sizing: border-box;" />
pList.add( new
Person(102,"James"));<br style="box-sizing: border-box;" />
pList.add( new
Person(103,"Dan"));<br style="box-sizing: border-box;" />
pList.add( new
Person(104,"Mathew"));<br style="box-sizing: border-box;" />
pList.add( new
Person(105,"Sam"));<br style="box-sizing: border-box;" />
<br style="box-sizing: border-box;" />
System.out.println("Added List elements
: "+pList);<br style="box-sizing: border-box;" />
/* To perfomr element wise operation */<br style="box-sizing: border-box;" />
for(Person p : pList){<br style="box-sizing: border-box;" />
System.out.println(p);<br style="box-sizing: border-box;" />
}<br style="box-sizing: border-box;" />
/* you try to modify the data in pList
through enhanced for loop */<br style="box-sizing: border-box;" />
System.out.println("List elements now : ");<br style="box-sizing: border-box;" />
for(Person p : pList){<br style="box-sizing: border-box;" />
if(p.getName().equals("Sam"))<br style="box-sizing: border-box;" />
p.setName("Samuel");<br style="box-sizing: border-box;" />
}<o:p></o:p></span></div>
<div style="box-sizing: border-box; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; outline: 0px; vertical-align: baseline;">
<span style="font-family: "inherit","serif"; font-size: 14pt;">/* OOPs... it has not
changed...But let us conform it once again by using enhanced for loop */<o:p></o:p></span></div>
<div style="box-sizing: border-box; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; outline: 0px; vertical-align: baseline;">
<span style="font-family: "inherit","serif"; font-size: 14pt;">
System.out.println("After Updation : view element in the
list : ");<br style="box-sizing: border-box;" />
for(Person p : pList){<br style="box-sizing: border-box;" />
System.out.println(p);<br style="box-sizing: border-box;" />
}<br style="box-sizing: border-box;" />
}<br style="box-sizing: border-box;" />
}<o:p></o:p></span></div>
<div style="box-sizing: border-box; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; outline: 0px; vertical-align: baseline;">
<span style="font-family: "inherit","serif"; font-size: 14pt;">When I run this I see
something different.<o:p></o:p></span></div>
<div style="box-sizing: border-box; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; outline: 0px; vertical-align: baseline;">
<span style="font-family: "inherit","serif"; font-size: 14pt;">Added List elements :
[Id : 101 Name : John, Id : 102 Name : James, Id : 103 Name : Dan, Id : 104
Name : Mathew, Id : 105 Name : Sam]<br style="box-sizing: border-box;" />
Id : 101 Name : John<br style="box-sizing: border-box;" />
Id : 102 Name : James<br style="box-sizing: border-box;" />
Id : 103 Name : Dan<br style="box-sizing: border-box;" />
Id : 104 Name : Mathew<br style="box-sizing: border-box;" />
Id : 105 Name : Sam<br style="box-sizing: border-box;" />
List elements now :<span class="apple-converted-space"> </span><br style="box-sizing: border-box;" />
After Updation : view element in the list :<span class="apple-converted-space"> </span><br style="box-sizing: border-box;" />
Id : 101 Name : John<br style="box-sizing: border-box;" />
Id : 102 Name : James<br style="box-sizing: border-box;" />
Id : 103 Name : Dan<br style="box-sizing: border-box;" />
Id : 104 Name : Mathew<br style="box-sizing: border-box;" />
Id : 105 Name : Samuel<o:p></o:p></span></div>
<div style="box-sizing: border-box; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in; margin: 0in 0in 24.5pt; outline: 0px; vertical-align: baseline;">
<span style="font-family: "inherit","serif"; font-size: 14pt;">Now the question is why
the name gets changed from within the enhanced for loop. Now in previous post x
reads data from ArrayList, and value change in x does not reflect in ArrayList.
But in current example, p holds the reference to an object and using reference,
it change the value. Rest is left to your imagination... :) !!!<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<br /></div>
</div>
</div>
</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-79765888631620032852015-12-16T13:24:00.002-08:002015-12-16T13:30:49.554-08:00Enhanced for Loop : Collections and Immutability<div style="line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in;">
<span style="font-family: "georgia" , "serif"; font-size: 14.0pt;">Enhanced
for loop was added in Java SE 1.5. It can be used with Arrays and collections
to iterate item-wise/element-wise. It works like read data of very ancient
programming language called BASIC... atleast to me it looks like that... You
can define read element of the collection element type and do operations on
data. The amazing part is whatever you do to the read element, it does not
disturb the original collection, that is data in for loop is immutable...</span><br />
<div style="line-height: 24.5pt; margin-bottom: 24.5pt; margin-left: 0in; margin-right: 0in; margin-top: 0in;">
<span style="font-family: "Georgia","serif"; font-size: 14.0pt;">package
test;<o:p></o:p></span></div>
<div style="line-height: 24.5pt; margin: 0in 0in 24.5pt;">
<span style="font-family: "Georgia","serif"; font-size: 14.0pt;">import
java.util.ArrayList;<br />
import java.util.List;<br />
import static java.lang.Math.random;<br />
/**<br />
*<br />
* @author nkan<br />
*/<br />
public class EnhancedForLoop {<br />
public static void main(String[] args) {<br />
List<Integer> intList = new ArrayList<>();<br />
for (int i=0;i<5;i++){<br />
intList.add((int) (random() * 100));<br />
}<br />
<br />
System.out.println("Added List elements : "+intList);<br />
/* To perfomr element wise operation */<br />
for(int x : intList){<br />
x++;<br />
System.out.println("x = "+x);<br />
}<br />
/* you have incremented x, may be you are thinking that ArrayList : intList has
be modified */<br />
System.out.println("List elements now : "+intList);<br />
/* OOPs... it has not changed...But let us conform it once again by using
enhanced for loop */<br />
System.out.println("After Updation : incremented element list is :
");<br />
for(int x : intList){<br />
System.out.println("x = "+x);<br />
}<br />
}<br />
}<o:p></o:p></span></div>
<div style="line-height: 24.5pt; margin: 0in 0in 24.5pt;">
<span style="font-family: "Georgia","serif"; font-size: 14.0pt;">When I executed this
program :<o:p></o:p></span></div>
<div style="line-height: 24.5pt; margin: 0in 0in 24.5pt;">
<span style="font-family: "Georgia","serif"; font-size: 14.0pt;">Added List elements :
[98, 14, 30, 34, 43]<br />
x = 99<br />
x = 15<br />
x = 31<br />
x = 35<br />
x = 44<br />
List elements now : [98, 14, 30, 34, 43]<br />
After Updation : incremented element list is :<span class="apple-converted-space"> </span><br />
x = 98<br />
x = 14<br />
x = 30<br />
x = 34<br />
x = 43<o:p></o:p></span></div>
<br />
<div style="line-height: 24.5pt; margin: 0in 0in 24.5pt;">
<span style="font-family: Georgia, serif; font-size: 14pt;">Created and posted by
Nancy @ 12/17/2015 2:25 am<span style="color: rgba(0, 0, 0, 0.701960784313725);"><o:p></o:p></span></span></div>
</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-86692567553442701792015-10-16T03:17:00.001-07:002015-10-16T03:18:56.823-07:00Understanding mysterious static - 2<div dir="ltr" style="text-align: left;" trbidi="on">
Oh! that was interesting, if you tried executing the program in the previous post. For a second may be it gave a food for thought to your mind. Why main is not executing first? Why am i getting output as:<br />
<br />
In static block...InterestingStatic1<br />
In main...InterestingStatic1<br />
<br />
May be something is happening before main is executing. I must know it. Everything is mystery until understood. So let me take you to another interesting and mysterious scenario :<br />
<br />
Here is what you do. Nothing much. Add InterestingStatic1Test class to the project you created yesterday. Let the class InterestingStatic1 be there in the project without any changes.<br />
<br />
package teststatic;<br />
public class InterestingStatic1Test {<br />
static{<br />
System.out.println("In static block...InterestingStatic1Test");<br />
}<br />
public void printSomething(){<br />
System.out.println("Just printing ... Welcome to Mysteries of Java");<br />
}<br />
public static void main(String[] args) {<br />
System.out.println("In main...InterestingStatic1Test");<br />
InterestingStatic1Test justObject = new InterestingStatic1Test();<br />
justObject.printSomething();<br />
InterestingStatic1 newObject = new InterestingStatic1();<br />
}<br />
}<br />
<br />
Yes, you are right. I am going to ask you to guess the output first and then run the program. Does it match your guess? Why the output is what it is. wait... there is more to come ... See you...<br />
<br />
<div class="MsoNormal">
©K.A.N. Nancy</div>
</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com1tag:blogger.com,1999:blog-4650874658008990222.post-18036745601925857102015-10-15T02:49:00.000-07:002015-10-15T02:49:01.587-07:00Understanding mysterious static - 1<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
package teststatic;</div>
<div class="MsoNormal">
public class InterestingStatic1 {</div>
<div class="MsoNormal">
static{</div>
<div class="MsoNormal">
System.out.println("In static block...InterestingStatic1");</div>
<div class="MsoNormal">
}</div>
<div class="MsoNormal">
public static void
main(String[] args) {</div>
<div class="MsoNormal">
System.out.println("In main...InterestingStatic1");</div>
<div class="MsoNormal">
}</div>
<div class="MsoNormal">
}</div>
<div class="MsoNormal">
What do you think should be the output. Normal understanding
is main is executed first by JVM. Run this code. Do you find something
unexpected?</div>
<div class="MsoNormal">
More interesting code … tomorrow… let the mystery of static
be resolved slowly, so that you enjoy…</div>
<br />
<div class="MsoNormal">
©K.A.N. Nancy</div>
</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com1tag:blogger.com,1999:blog-4650874658008990222.post-41948748708854691872015-10-14T08:27:00.000-07:002015-10-15T02:49:19.128-07:00static in Java - 1<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
When static block in java class is executed ?</div>
<div class="MsoNormal">
a) when the class is compiled</div>
<div class="MsoNormal">
b) when the first method in class is executed</div>
<div class="MsoNormal">
c) when the object is created, but before the constructor is called</div>
<div class="MsoNormal">
d) when the object is created and after the constructor is called</div>
<div class="MsoNormal">
e) when the class is loaded</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
©K.A.N. Nancy</div>
</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com2tag:blogger.com,1999:blog-4650874658008990222.post-68728029252561194942015-10-13T13:51:00.001-07:002015-10-13T13:51:10.921-07:00Adding 2 numbers using Java<div dir="ltr" style="text-align: left;" trbidi="on">
Let us have fun to add two numbers in variety of ways :<br />
<br />
public class FunWithAdd {<br />
private int num1;<br />
private int num2;<br />
public FunWithAdd(int num1, int num2){<br />
this.num1=num1;<br />
this.num2=num2;<br />
}<br />
public int getNum1(){<br />
return num1;<br />
}<br />
public int getNum2(){<br />
return num2;<br />
}<br />
public void setNum1(int num1){<br />
this.num1=num1;<br />
}<br />
public void setNum2(int num2){<br />
this.num2=num2;<br />
}<br />
int sum(){<br />
return num1+num2;<br />
}<br />
int sum(int n1, int n2){<br />
return n1+n2;<br />
}<br />
int sumWithItr (int n1, int n2){<br />
int s=n1;<br />
<br />
for(int i=1;i<=n2;i++){<br />
s++;<br />
}<br />
return s;<br />
}<br />
int sumRecursive(int n1, int n2){<br />
if (n2 ==0){<br />
return n1;<br />
}<br />
return sumRecursive(++n1,--n2);<br />
}<br />
}<br />
<br />
Create a class to test variety of sum methods.<br />
<br />
public class Add2Nums {<br />
public static void main(String[] args) {<br />
/* Summing Locals */<br />
int num1=25;<br />
int num2=12;<br />
System.out.println("sum : "+(num1+num2));<br />
/* Sum using Object of FunWithAdd*/<br />
FunWithAdd numsObject= new FunWithAdd(30,60);<br />
System.out.println("Adding the numbers in Object : "+numsObject.sum());<br />
System.out.println("Adding the numbers in Object by sending data from outside :"+numsObject.sum(19,20));<br />
numsObject.setNum1(7);<br />
numsObject.setNum2(9);<br />
System.out.println("sum using sumWithItr method : " +numsObject.sumWithItr(numsObject.getNum1(),numsObject.getNum2()));<br />
numsObject.setNum1(43);<br />
numsObject.setNum2(2);<br />
System.out.println("sum using sumRecursive: "+numsObject.sumRecursive(numsObject.getNum1(),numsObject.getNum2()));<br />
}<br />
}<br />
<br />
Output :<br />
sum : 37<br />
Adding the numbers in Object : 90<br />
Adding the numbers in Object by sending data from outside :39<br />
sum using sumWithItr method : 16<br />
sum using sumRecursive: 45<br />
<br />
Change the values and have fun… :)<br />
©Nancy : created at 2:07 am IST<br />
<div>
<br /></div>
</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-5025927234768530152015-10-13T05:11:00.001-07:002015-10-13T05:11:28.778-07:00Java EE 6 Collocated Model<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<span style="font-family: "Georgia","serif"; font-size: 12.5pt; line-height: 115%;">EJB 3.1 provides subset of EJB. This subset is known as
EJBLite. EJBLite is available as a part of Java EE6 Web Container. You can
create local EJBs with and without interface view. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: "Georgia","serif"; font-size: 12.5pt; line-height: 115%;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjo5uilt8QENx53NdG2M4Wy5N8249-moBTbVY1M6ASGXL6Td87qR8hBFMHT99nCP1yZ1OjJtwfOC2UUIATKBWIprzKCKEVq2_ne_9Vz2rXCC64sb4AooOAotXigXopFut6IT076msGc8gTp/s1600/collocated.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="155" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjo5uilt8QENx53NdG2M4Wy5N8249-moBTbVY1M6ASGXL6Td87qR8hBFMHT99nCP1yZ1OjJtwfOC2UUIATKBWIprzKCKEVq2_ne_9Vz2rXCC64sb4AooOAotXigXopFut6IT076msGc8gTp/s320/collocated.jpg" width="320" /></a></div>
<div class="MsoNormal">
<span style="font-family: "Georgia","serif"; font-size: 12.5pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: Georgia, serif; font-size: 16.6667px; line-height: 19.1667px;">EJB 3.1 The benefit is when Web component and EJB component need to access the object for business processing, no marshaling is required. If an EJB makes changes in the object state , web component can view the changes. </span></div>
<div class="MsoNormal">
<span style="font-family: Georgia, serif; font-size: 16.6667px; line-height: 19.1667px;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: Georgia, serif; font-size: 16.6667px; line-height: 19.1667px;">The local EJBs can be packaged in a WAR file. This is called as Collocated model of Java EE 6.</span></div>
</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com0tag:blogger.com,1999:blog-4650874658008990222.post-90408093139484414302015-06-21T00:42:00.001-07:002015-06-21T00:42:09.110-07:00Interesting Facts about JAVA - 2<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="line-height: 18.0pt; margin-bottom: 22.5pt; margin-left: 1.0in; margin-right: 0in; margin-top: 5.0pt; mso-list: l0 level2 lfo1; text-indent: -.25in;">
<span style="color: #4d4f51; font-family: Helvetica, sans-serif; line-height: 18pt; text-indent: -0.25in;">Did you know ? (These were later removed from java)</span></div>
<div style="line-height: 18.0pt; margin-bottom: 22.5pt; margin-left: 1.0in; margin-right: 0in; margin-top: 5.0pt; mso-list: l0 level2 lfo1; text-indent: -.25in;">
<span style="color: #4d4f51; font-family: Helvetica, sans-serif; line-height: 18pt; text-indent: -0.25in;">1. All exceptions were unchecked</span></div>
<div style="line-height: 18.0pt; margin-bottom: 22.5pt; margin-left: 1.0in; margin-right: 0in; margin-top: 5.0pt; mso-list: l0 level2 lfo1; text-indent: -.25in;">
<span style="color: #4d4f51; font-family: Helvetica, sans-serif; line-height: 18pt; text-indent: -0.25in;">2. unprotect keyword could be used to avoid signaling asynchronous exceptions</span></div>
</div>
K.A.N.Nancyhttp://www.blogger.com/profile/04428346391568619474noreply@blogger.com1