tag:blogger.com,1999:blog-8365206769671358312024-03-05T05:45:31.239-08:00Sun Certified Programmer For Java 6<p></p>
This is some information on the Java 6 certification that I prepare. This information is not very well structured and it is most of the time an answer to the questions that come when I read the SCJP 6 book (Bates and Sierra).
*** AS OF 5 OF MARCH 2010, I AM SCJP 6.0 ***Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.comBlogger39125tag:blogger.com,1999:blog-836520676967135831.post-81210919735153540992012-07-05T03:51:00.000-07:002012-07-05T03:51:17.918-07:0037 - to clone or not to cloneTry to understand the following :<br />
<br />
package Object;<br /><br />public class Customer implements Cloneable{<br /><br /> private String firstName = "firstName";<br /> private String lastName = "lastName" ;<br /><br /> public String getFirstName() {<br /> return firstName;<br /> }<br /><br /> public void setFirstName(String firstName) {<br /> this.firstName = firstName;<br /> }<br /><br /> public String getLastName() {<br /> return lastName;<br /> }<br /><br /> public void setLastName(String lastName) {<br /> this.lastName = lastName;<br /> }<br /><br /> Customer(String firstName, String lastName) {<br /> this.firstName = new String(firstName);<br /> this.lastName = new String(lastName);<br /> }<br /><br /> public Object getShallowCopy() throws CloneNotSupportedException{<br /> return this.clone();<br /> }<br /><br /> public Object getDeepCopy() throws CloneNotSupportedException{<br /> return new Customer(this.firstName, this.lastName);<br /> }<br />}<br />
<br />
package Object;<br /><br />import org.junit.Test;<br />import static org.junit.Assert.*;<br /><br />public class CustomerTest {<br /><br /> @Test<br /> public void testClone() throws Exception {<br /> Customer customer = new Customer("rudy", "vissers");<br /> Customer clonedCustomer = (Customer)customer.getShallowCopy();<br /> assertTrue(customer != clonedCustomer);<br /> assertTrue(customer.getFirstName() == clonedCustomer.getFirstName());<br /> assertTrue(customer.getLastName() == clonedCustomer.getLastName());<br /> assertTrue(customer.getFirstName().equals(clonedCustomer.getFirstName()));<br /> assertTrue(customer.getLastName().equals(clonedCustomer.getLastName()));<br /> }<br /><br /> @Test<br /> public void testDeepCopy() throws Exception {<br /> Customer customer = new Customer("rudy", "vissers");<br /> Customer deepCopy = (Customer)customer.getDeepCopy();<br /> assertTrue(customer != deepCopy);<br /> assertTrue(customer.getFirstName() != deepCopy.getFirstName());<br /> assertTrue(customer.getLastName() != deepCopy.getLastName());<br /> assertTrue(customer.getFirstName().equals(deepCopy.getFirstName()));<br /> assertTrue(customer.getLastName().equals(deepCopy.getLastName()));<br /> }<br /><br />}<br />
<br />
That's all folks !Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-6110817920445961522010-03-27T08:05:00.000-07:002012-02-16T06:02:24.695-08:0036 - kick butt and have fun !<div style="text-align: center;">
<b>kick butt and have fun ! :-) </b></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiR0770f2oDBB1WVLlShyYoGoKnH-RQKcEzxlmg1ONPNcYNIFwd76ou2TBCWHz1pThV69c_AJQw5iPqKEfysZ5oMzqM-524r6evvbppMU_A3EF26MFHVcPby5bxTvPk8cxo-SZK0Nutyerx/s1600/Num%C3%A9riser0028.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="118" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiR0770f2oDBB1WVLlShyYoGoKnH-RQKcEzxlmg1ONPNcYNIFwd76ou2TBCWHz1pThV69c_AJQw5iPqKEfysZ5oMzqM-524r6evvbppMU_A3EF26MFHVcPby5bxTvPk8cxo-SZK0Nutyerx/s200/Num%C3%A9riser0028.jpg" width="200" /> </a></div>
<div class="separator" style="clear: both; text-align: left;">
Until the next certification (Java 7 or 8) I continue the story on <a href="http://java1s1.blogspot.com/">Java Easy !</a> </div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com1tag:blogger.com,1999:blog-836520676967135831.post-2711051170839248982010-03-27T07:52:00.000-07:002010-03-27T07:52:21.329-07:0035 - up to the next ! :-)<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEe7p3a6V0F6Wd_w7xV4CwadM2Xw7LwdMbTtViP10jhOppJwZJTi7aANwwmnK0bTVAc8_Q4fg1HyHiZ95VLnFBJ59XvlfdcdbFym-EhVm7EWSCen1aYzY1wokfgS1DEkvWZks3750rg1Rq/s1600/Num%C3%A9riser0027.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="465" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEe7p3a6V0F6Wd_w7xV4CwadM2Xw7LwdMbTtViP10jhOppJwZJTi7aANwwmnK0bTVAc8_Q4fg1HyHiZ95VLnFBJ59XvlfdcdbFym-EhVm7EWSCen1aYzY1wokfgS1DEkvWZks3750rg1Rq/s640/Num%C3%A9riser0027.jpg" width="640" /></a></div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-32976946449580560042010-03-01T04:40:00.000-08:002010-03-01T04:42:17.946-08:0034 - What is printed ?To relax you a little please make the following exercice and guess what is going to be printed ?<br />
<br />
<br />
package ex1;<br />
<br />
public class Main {<br />
<br />
String hello = "hello";<br />
<br />
{<br />
<b> System.out.println(this.hello);</b><br />
System.out.println("block1");<br />
}<br />
<br />
public static void main(String[] args) {<br />
<br />
new Main().fool();<br />
<br />
}<br />
<br />
Main() {<br />
System.out.println("Main");<br />
}<br />
<br />
void fool() {<br />
System.out.println("fool");<br />
}<br />
<br />
{<br />
System.out.println("block2");<br />
}<br />
}<br />
<div><br />
</div><div>==========</div><div><br />
<br />
<br />
<br />
<br />
<br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><div>run:</div><div><b>hello</b></div><div>block1</div><div>block2</div><div>Main</div><div>fool</div><div><br />
</div></div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-42593246343588231622010-02-25T02:52:00.000-08:002012-02-10T08:12:00.515-08:0033 - AutoBoxing rules<span class="Apple-style-span" style="font-family: Arial; font-size: small;"><span class="Apple-style-span" style="font-size: 13px;"><span class="Apple-style-span" style="font-family: 'Times New Roman';"><span class="Apple-style-span" style="font-size: medium;">It is my rules and please drop me an email if you read something incorrect !</span></span></span></span><br />
<span class="Apple-style-span" style="font-family: Arial; font-size: small;"><span class="Apple-style-span" style="font-size: 13px;"><span class="Apple-style-span" style="font-family: 'Times New Roman';"><span class="Apple-style-span" style="font-size: medium;"><br />
</span></span></span></span><br />
<span class="Apple-style-span" style="font-family: Arial; font-size: small;"><span class="Apple-style-span" style="font-size: 13px;"><span class="Apple-style-span" style="font-family: 'Times New Roman';"><span class="Apple-style-span" style="font-size: medium;">Let's say you have several methods with the same name but different parameters(overloaded methods).</span></span></span></span><br />
<span class="Apple-style-span" style="font-family: Arial; font-size: small;"><span class="Apple-style-span" style="font-size: 13px;"><span class="Apple-style-span" style="font-family: 'Times New Roman';"><span class="Apple-style-span" style="font-size: medium;"><br />
</span></span></span></span><br />
<span class="Apple-style-span" style="font-family: Arial; font-size: small;"><span class="Apple-style-span" style="font-size: 13px;"><span class="Apple-style-span" style="font-family: 'Times New Roman';"><span class="Apple-style-span" style="font-size: medium;">You pass a "primitive wrapper object" <b>OR </b>a primitive.</span></span></span></span><br />
<span class="Apple-style-span" style="font-family: Arial; font-size: small;"><span class="Apple-style-span" style="font-size: 13px;"><span class="Apple-style-span" style="font-family: 'Times New Roman';"><span class="Apple-style-span" style="font-size: medium;"><br />
</span></span></span></span><br />
<span class="Apple-style-span" style="font-family: Arial; font-size: small;"><span class="Apple-style-span" style="font-size: 13px;"><span class="Apple-style-span" style="font-family: 'Times New Roman';"><span class="Apple-style-span" style="font-size: medium;">What is chosen ? and Why ?</span></span></span></span><br />
<span class="Apple-style-span" style="font-family: Arial; font-size: small;"><span class="Apple-style-span" style="font-size: 13px;"><span class="Apple-style-span" style="font-family: 'Times New Roman';"><span class="Apple-style-span" style="font-size: medium;"><br />
</span></span></span></span><br />
<u>Diagram :</u><br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4P1xWRoFJBFxBfm_3P9iHRt-H5o6PqmMogze86i30RcgJrrbISDM82qf9ZKcmH5DYkBOljJHXbOUINVAkqIzbz2VKBZNIbhG9jkvSJx4DpeMIoNeut21RMdqnCx5kkKujgLPWANp2ypi_/s1600-h/ab-short.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4P1xWRoFJBFxBfm_3P9iHRt-H5o6PqmMogze86i30RcgJrrbISDM82qf9ZKcmH5DYkBOljJHXbOUINVAkqIzbz2VKBZNIbhG9jkvSJx4DpeMIoNeut21RMdqnCx5kkKujgLPWANp2ypi_/s400/ab-short.jpg" width="292" /></a></div><br />
<div class="separator" style="clear: both; text-align: center;"></div><br />
A) a <b>Short </b><br />
<br />
can become a <b>Short or an Object (is an Object)</b><br />
<b><br />
</b><br />
<b>WILL NOT BECOME an Integer, a Long, a Float, a Double</b><br />
<b><br />
</b><br />
<b>Short is Short OR Object STOP</b><br />
<b><br />
</b><br />
OR can become a <b>short </b>(a primitive)<br />
look below<br />
<br />
B) a <b>short </b>(primitive)<br />
<br />
can become (what is short compatible) a <b>short</b>, a <b>int</b>, a <b>long</b>, a <b>float</b>, a <b>double </b>(I mean without a cast) <b>STOP</b><br />
<br />
OR<br />
<br />
a <b>Short </b>(primitive wrapper)<br />
Look now the way a Short is evaluated above.<br />
<br />
I have to add a rule, var-arg parameters are <b>ALWAYS - ALWAYS - ALWAYS :-) </b>chosen LAST ! Look at example AB2 (AutoBoxing2).<br />
I like the style of Kathy :-)<br />
<u><br />
</u><br />
<u>Examples</u><br />
<br />
Please find the correct answers :<br />
<br />
<br />
package autoboxing;<br />
<br />
<b>public class AB1 {</b><br />
<br />
static String method1(Short param) {<br />
return "Short";<br />
}<br />
static String method1(short param) {<br />
return "short";<br />
}<br />
<br />
public static void main(String[] args) {<br />
Short x = -32768;<br />
<br />
System.out.println(method1(x));<br />
}<br />
}<br />
<br />
<br />
package autoboxing;<br />
<br />
public class <b>AB2 </b>{<br />
<br />
static String method1(Short... param) {<br />
return "Short...";<br />
}<br />
<br />
static String method1(Object param) {<br />
return "Object";<br />
}<br />
<br />
public static void main(String[] args) {<br />
Short x = -32768;<br />
<br />
System.out.println(method1(x));<br />
}<br />
}<br />
<div><br />
</div><div><div>package autoboxing;</div><div><br />
</div><div>public class <b>AB31 </b>{</div><div><br />
</div><div> static String method1(short param) {</div><div> return "short";</div><div> }</div><div><br />
</div><div> static String method1(Object param) {</div><div> return "Object";</div><div> }</div><div><br />
</div><div> public static void main(String[] args) {</div><div> Short x = -32768;</div><div><br />
</div><div> System.out.println(method1(x));</div><div> }</div><div>}</div><div><br />
</div><div><div>package autoboxing;</div><div><br />
</div><div>public class <b>AB32 </b>{</div><div><br />
</div><div> static String method1(short param) {</div><div> return "short";</div><div> }</div><div><br />
</div><div> static String method1(double param) {</div><div> return "double";</div><div> }</div><div><br />
</div><div> public static void main(String[] args) {</div><div> Short x = -32768;</div><div><br />
</div><div> System.out.println(method1(x));</div><div> }</div><div>}</div><div><br />
</div><div><div>package autoboxing;</div><div><br />
</div><div>public class <b>AB33 </b>{</div><div><br />
</div><div> static String method1(double param) {</div><div> return "double";</div><div> }</div><div><br />
</div><div> static String method1(int param) {</div><div> return "int";</div><div> }</div><div><br />
</div><div> public static void main(String[] args) {</div><div> Short x = -32768;</div><div><br />
</div><div> System.out.println(method1(x));</div><div> }</div><div>}</div><div><br />
</div><div><div>package autoboxing;</div><div><br />
</div><div>public class <b>AB4 </b>{</div><div><br />
</div><div> static String method1(short param) {</div><div> return "short";</div><div> }</div><div><br />
</div><div> static String method1(Short param) {</div><div> return "Short";</div><div> }</div><div><br />
</div><div> public static void main(String[] args) {</div><div> short x = -32768;</div><div><br />
</div><div> System.out.println(method1(x));</div><div> }</div><div>}</div><div><br />
</div><div><div>package autoboxing;</div><div><br />
</div><div>public class <b>AB5 </b>{</div><div><br />
</div><div> static String method1(double param) {</div><div> return "double";</div><div> }</div><div><br />
</div><div> static String method1(Short param) {</div><div> return "Short";</div><div> }</div><div><br />
</div><div> public static void main(String[] args) {</div><div> short x = -32768;</div><div><br />
</div><div> System.out.println(method1(x));</div><div> }</div><div>}</div><div><br />
</div><div><div><br />
</div><div>package autoboxing;</div><div><br />
</div><div>public class <b>AB6 </b>{</div><div> static String method1(Double param) {</div><div> return "Double";</div><div> }</div><div><br />
</div><div> public static void main(String[] args) {</div><div> short x = -32768;</div><div> System.out.println(method1(x));</div><div> }</div><div>}</div><div><br />
</div><div><div>package autoboxing;</div><div><br />
</div><div>public class <b>AB7 </b>{</div><div><br />
</div><div> static String method1(Short param) {</div><div> return "Short";</div><div> }</div><div><br />
</div><div> static String method1(Object param) {</div><div> return "Object";</div><div> }</div><div><br />
</div><div> public static void main(String[] args) {</div><div> short x = -32768;</div><div> System.out.println(method1(x));</div><div> }</div><div>}</div><div><br />
</div><div><div>package autoboxing;</div><div><br />
</div><div>public class <b>AB8 </b>{</div><div><br />
</div><div> static String method1(Integer param) {</div><div> return "Integer";</div><div> }</div><div><br />
</div><div> static String method1(Object param) {</div><div> return "Object";</div><div> }</div><div><br />
</div><div> public static void main(String[] args) {</div><div> short x = -32768;</div><div> System.out.println(method1(x));</div><div> }</div><div>}</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><u>Answers :</u></div><div>AB1:Short,AB2:Object,AB31:Object,AB32:short,AB33:int,AB4:short,AB5:double,AB6:does not compile,AB7:Short,AB8:Object</div><div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div><div><br />
</div></div></div></div></div></div></div></div></div></div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com5tag:blogger.com,1999:blog-836520676967135831.post-52769855893892796882010-02-19T05:35:00.000-08:002010-02-20T06:28:41.762-08:0032 - arrays - Java 1.6I find the following very fascinating...<br />
<br />
package basic;<br />
<br />
public class Main {<br />
<br />
public static void main(String[] args) {<br />
<br />
Object d = new Long[4];<br />
Object[] e = (Object[]) d;<br />
System.out.println("e[0] : " + e[0]);<br />
<br />
Long[] f = (Long[]) d;<br />
System.out.println("\nf[0] : " + f[0] + "\n");<br />
<br />
<br />
Long[] g[] = {{1L, 2L}, {1L}};<br />
for (int i = 0; i < g.length; i++)<br />
for (int j = 0; j < g[i].length; j++)<br />
System.out.println(g[i][j]);<br />
<br />
System.out.println("");<br />
String[] s = new String[4];<br />
int i=0;<br />
for(String s1:s)<br />
System.out.println(i++ + ":" + s1);<br />
<br />
System.out.println("");<br />
int []t[] = new int[5][6];<br />
for(int k=0; k < t.length; k++) {<br />
int m = 0;<br />
for(int l:t[k]) {<br />
System.out.println(m++ + ":" + l);<br />
}<br />
System.out.println("");<br />
}<br />
}<br />
}<br />
<br />
run:<br />
e[0] : null<br />
<br />
f[0] : null<br />
<br />
1<br />
2<br />
1<br />
<br />
0:null<br />
1:null<br />
2:null<br />
3:null<br />
<br />
0:0<br />
1:0<br />
2:0<br />
3:0<br />
4:0<br />
5:0<br />
<br />
0:0<br />
1:0<br />
2:0<br />
3:0<br />
4:0<br />
5:0<br />
<br />
0:0<br />
1:0<br />
2:0<br />
3:0<br />
4:0<br />
5:0<br />
<br />
0:0<br />
1:0<br />
2:0<br />
3:0<br />
4:0<br />
5:0<br />
<br />
0:0<br />
1:0<br />
2:0<br />
3:0<br />
4:0<br />
5:0<br />
<br />
BUILD SUCCESSFUL (total time: 0 seconds)Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-28023001670883951972010-02-19T02:33:00.000-08:002010-02-19T02:33:26.456-08:0031 - Covariant Return - Java 1.6They use several time a covariant return in the master exams... Be careful...<br />
<br />
<a href="http://dev.fyicenter.com/Interview-Questions/Java-3/What_is_covariant_return_type_.html">I find the explanations here interesting</a><br />
<br />
Example :<br />
<br />
1) Java 1.1 and after (this will compile) - <b>It is NOT a covariant return !!!</b><br />
<br />
<br />
package covariantreturn;<br />
<br />
public class A {<br />
<b> A method() {</b><br />
return this;<br />
}<br />
}<br />
<div><br />
</div><div><div>package covariantreturn;</div><div><br />
</div><div>public class B extends A {</div><div><b> A method() {</b></div><div> return this;</div><div> }</div><div>}</div><div><br />
</div><div><div>package covariantreturn;</div><div><br />
</div><div>public class C extends B {</div><div> <b> A method() {</b></div><div> return this;</div><div> }</div><div>}</div><div><br />
</div><div><b>2a) Java 1.5 and after (will not compile in Java < 1.5 !!!) - Covariant Return !</b></div><div><b>You return a subtype of A in B</b></div><div><div><b>You return a subtype of B in C (can be B) </b></div><div><br />
</div></div><div><br />
</div><div><div>package covariantreturn;</div><div><br />
</div><div>public class A {</div><div> <b> A method() {</b></div><div> return this;</div><div> }</div><div><br />
</div><div>}</div><div><br />
</div></div><div><div>package covariantreturn;</div><div><br />
</div><div>public class B extends A {</div><div> <b> B method() {</b></div><div> return this;</div><div> }</div><div>}</div><div><br />
</div><div><div>package covariantreturn;</div><div><br />
</div><div>public class C extends B {</div><div> <b>B method() {</b></div><div> return this;</div><div> }</div><div>}</div><div><br />
</div><div>2b) </div><div><br />
</div><div><div><b>You return a subtype of A in B</b></div><div><div><b>You return a subtype of B in C (can be C) </b></div><div><br />
</div></div></div><div><div><div>package covariantreturn;</div><div><br />
</div><div>public class A {</div><div> <b> A method() {</b></div><div> return this;</div><div> }</div><div><br />
</div><div>}</div><div><br />
</div></div><div><div>package covariantreturn;</div><div><br />
</div><div>public class B extends A {</div><div> <b> B method() {</b></div><div> return this;</div><div> }</div><div>}</div><div><br />
</div><div><div>package covariantreturn;</div><div><br />
</div><div>public class C extends B {</div><div><b> C method() {</b></div><div> return this;</div><div> }</div><div>}</div><div><br />
</div></div></div></div></div></div></div></div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-48800922439850714622010-02-07T02:44:00.000-08:002010-02-19T00:31:31.786-08:000 - ERRATA - MASTER EXAM 1Question 68<br />
H java -cp /mp/jars/mp.jar player.MusicPlayer<br />
INSTEAD OF<br />
H java -cp /mp/jars/cp.jar player.MusicPlayer<br />
<br />
<b><span class="Apple-style-span" style="color: red;">IT IS TOTALLY UNACCEPTABLE !!! GUYS READ YOUR STUFF AND PRODUCE AN ERRATA !!!</span></b><br />
<span class="Apple-style-span" style="color: red;"><b><br />
</b></span><br />
But your book is so good that I forgive you Kathy and Bert :-)Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-6286129587697127552010-02-06T11:39:00.000-08:002010-02-19T00:30:00.451-08:0030 - Treeset - Java 1.6.0_18package javaapplication15;<br />
<br />
import java.util.Set;<br />
import java.util.TreeSet;<br />
<br />
public class Main {<br />
<br />
public static void main(String[] args) {<br />
<br />
Set treeSet = new TreeSet();<br />
<br />
treeSet.add("2"); treeSet.add("4"); treeSet.add("6"); treeSet.add("8");<br />
<br />
System.out.println("first : " + ((TreeSet)treeSet).first());<br />
System.out.println("floor(6) : " + ((TreeSet)treeSet).floor("6"));<br />
System.out.println("higher(6) : " + ((TreeSet)treeSet).higher("6"));<br />
System.out.println("last : " + ((TreeSet)treeSet).last());<br />
}<br />
<br />
}<br />
<div><br />
</div><br />
<br />
first : 2<br />
floor(6) : 6<br />
higher(6) : 8<br />
last : 8Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-81240713939666830522010-02-06T08:07:00.000-08:002010-02-19T00:29:46.723-08:0029 - Serialization - Transient - Constructor - Java 1.6.0_18package serialization2;<br />
<br />
import java.io.Serializable;<br />
<br />
public class A {<br />
int a1 = -1;<br />
int a2 = -2;<br />
int a3 = -3;<br />
<br />
A() {<br />
System.out.println("in A()");<br />
}<br />
}<br />
<div><br />
</div><div><div>package serialization2;</div><div><br />
</div><div>import java.io.Serializable;</div><div><br />
</div><div>public class A1 extends A implements Serializable {</div><div> int a;</div><div> transient int b;</div><div><br />
</div><div> A1(int a, int b) {</div><div> this.a = a;</div><div> this.b = b;</div><div> }</div><div><br />
</div><div> public String toString() {</div><div> String aString = Integer.toString(super.a1)</div><div> + " " + Integer.toString(super.a2)</div><div> + " " + Integer.toString(super.a3)</div><div> + " " + Integer.toString(a)</div><div> + " " + Integer.toString(b);</div><div> return aString;</div><div> }</div><div>}</div><div><br />
</div><div><div><br />
</div><div>package serialization2;</div><div><br />
</div><div>import java.io.FileInputStream;</div><div>import java.io.FileOutputStream;</div><div>import java.io.ObjectInputStream;</div><div>import java.io.ObjectOutputStream;</div><div>import javax.imageio.stream.FileImageInputStream;</div><div><br />
</div><div>public class Main {</div><div> public static void main(String[] args) throws Exception {</div><div> A1 refA1 = new A1(-1,-2);</div><div> System.out.println("refA1 : " + refA1);</div><div> ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("A1.ser"));</div><div> oos.writeObject(refA1);</div><div><br />
</div><div> System.out.println("reading it back");</div><div><br />
</div><div> refA1 =(A1)new ObjectInputStream(new FileInputStream("A1.ser")).readObject();</div><div> System.out.println("refA1 : " + refA1);</div><div> </div><div> }</div><div>}</div><div><br />
</div><div><u>If A is not serializable</u> </div></div><div><br />
</div><div><div>run:</div><div>in A()</div><div>refA1 : -1 -2 -3 -1 -2</div><div>reading it back</div><div><b>in A()</b></div><div>refA1 : -1 -2 -3 -1 <b>0</b></div><div><br />
</div><div>The constructor was called. b is transient and assigned 0.</div><div><br />
</div><div><u>If a is serializable</u></div><div><br />
</div><div><div>run:</div><div>in A()</div><div>refA1 : -1 -2 -3 -1 -2</div><div>reading it back</div><div>refA1 : -1 -2 -3 -1 <b>0</b></div><div><br />
</div><div>The constructor was NOT called. b is still transient and still assigned 0.</div></div></div></div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-74403873185346698542010-01-30T07:45:00.000-08:002010-02-19T00:29:33.267-08:0028 - Review - xxxValue() - parseXxx() - valueOf() - Java 1.6.0_17package chap3review;<br />
<br />
public class Main {<br />
<br />
public static void main(String[] args) {<br />
<br />
<b>// NOT STATIC</b><br />
<b> //xxxValue()</b><br />
<br />
Byte aByte = 125;<br />
byte b = aByte.byteValue();<br />
short s = aByte.shortValue();<br />
int i = aByte.intValue();<br />
long l = aByte.longValue();<br />
float f = aByte.floatValue();<br />
double d = aByte.doubleValue();<br />
<br />
Integer aInteger = 6753;<br />
i = aInteger.intValue();<br />
l = aInteger.longValue();<br />
f = aInteger.floatValue();<br />
d = aInteger.doubleValue();<br />
<br />
Boolean aBoolean = true;<br />
boolean bo = aBoolean.booleanValue();<br />
<br />
<b>// Static</b><br />
<b> // parseXxx</b><br />
<br />
b = Byte.parseByte("127"); // Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"256" Radix:10<br />
// 8 bits signed -128..127 !!!<br />
i = Integer.parseInt("123456");<br />
bo = Boolean.parseBoolean("TRUE");<br />
<br />
<b> //static </b><br />
<b> // valueOf()</b><br />
<br />
Byte anotherByte = Byte.valueOf("-128");<br />
anotherByte = Byte.valueOf(b); // NO BYTE LITTERALS !!!<br />
<br />
Integer anotherInteger = Integer.valueOf("123456");<br />
anotherInteger = Integer.valueOf(123456);<br />
}<br />
}<br />
<div><br />
</div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com1tag:blogger.com,1999:blog-836520676967135831.post-63119792411191474292010-01-29T03:32:00.000-08:002010-02-19T00:29:20.824-08:0027 - Review - type of an array - level basic - Java 1.6.0_17 - NB 6.8What is the output ?<br />
<br />
package pkg1;<br />
<br />
public class Class10 {<br />
<br />
<b> static Integer[] arrayofINT = { 1, 2, 3, 4 ,5};</b><br />
<br />
public static void main(String[] args) {<br />
<br />
<b> if(arrayofINT instanceof Object) {</b><br />
<b> System.out.println("arrayofINT is an Object !");</b><br />
<b> }</b><br />
<b> if(arrayofINT instanceof Integer[]) {</b><br />
<b> System.out.println("arrayofINT is an Integer[] !");</b><br />
}<br />
}<br />
}<br />
<div><br />
</div><div><div>run:</div><div><b>arrayofINT is an Object !</b></div><div><b>arrayofINT is an Integer[] !</b></div><div>BUILD SUCCESSFUL (total time: 0 seconds)</div><div><br />
</div></div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-74902425674401714732010-01-29T03:04:00.000-08:002010-02-19T00:29:06.072-08:0026 - Review - protected - level basic - Java 1.6.0_17 - NB 6.8??? protected ??? All the buzz about this keyword...<br />
<br />
Let's try to do it simple...<br />
<br />
<a href="http://en.wikibooks.org/wiki/Java_Programming/Keywords/protected">http://en.wikibooks.org/wiki/Java_Programming/Keywords/protected</a><br />
<br />
protected means default + inheritance<br />
<br />
<ul><li>If you are in a separate pkg, you have access to the protected members ONLY if you extend the class where the protected member is defined.</li>
<li> In the class that extends the class where the protected member is defined you have access to the member using DIRECTLY the member name (only if you didn't you the same name in the class and in this case the member protected is hidden) and using the keyword 'super'. </li>
<li>If the protected member is hidden then use super. If you want to access a method protected in a subclass user the member name (or again if it is hidden then use super).</li>
<li>Don't try to user super.super (or super.super.super). It is not implemented... If you want a member to be accessed from all the inheritance tree then make the name unique or (it is a possibility) make a kind of proxy member that will have to call super from the parent class (I don't like this).</li>
</ul><div><span class="Apple-style-span" style="text-decoration: underline;">example :</span></div><div><br />
</div><div><div><b></b><br />
<b></b><br />
<b></b><br />
<b></b><br />
<b></b><br />
<div><b><span class="Apple-style-span" style="font-weight: normal;">/*</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> * To change this template, choose Tools | Templates</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> * and open the template in the editor.</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> */</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">package pkg3.pkg31;</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">/**</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> *</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> * @author Rudy</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> */</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">public class Class1 {</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> private int a;</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> private int b;</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> protected int c = -1;</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> </span>protected int d = -888;</b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> protected Class1(int a, int b) {</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("Class1-protected Class1(int a, int b)");</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> this.a = a;</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> this.b = b;</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> }</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> protected int calculation() {</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("Class1-protected int calculation()");</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> return a + b;</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> }</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"></span></b><br />
<b><span class="Apple-style-span" style="font-weight: normal;"><br />
protected void simplyToAnnoyYou() {<br />
System.out.println("simplyToAnnoyYou");<br />
}<br />
</span></b><br />
<b><span class="Apple-style-span" style="font-weight: normal;"></span></b><br />
<b><span class="Apple-style-span" style="font-weight: normal;"></span></b><br />
<div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><b><span class="Apple-style-span" style="font-weight: normal;"></span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> public static void main(String[] args) {</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("main from pkg31.Class1\n");</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println(new Class1(1, 2).calculation());</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> }</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">}</span></b></div><div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">run:</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">main from pkg31.Class1</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">Class1-protected Class1(int a, int b)</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">Class1-protected int calculation()</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">3</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">BUILD SUCCESSFUL (total time: 0 seconds)</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><b><br />
</b></span></b></div><div><div><b><span class="Apple-style-span" style="font-weight: normal;">/*</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> * To change this template, choose Tools | Templates</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> * and open the template in the editor.</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> */</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">package pkg3.pkg32;</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">/**</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> *</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> * @author Rudy</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> */</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">public class Class2 extends pkg3.pkg31.Class1 {</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> Class2(int a, int b) {</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> super(a, b);</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("Class2(int a, int b)");</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> super.c = -2;</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> }</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> @Override</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> protected int calculation() {</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("Class2-protected int calculation()");</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> return super.calculation();</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> }</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> protected int calculation1() {</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("Class2-protected int calculation1()");</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> int a = super.calculation() + super.c + c;</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> return a;</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> }</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> public static void main(String[] args) {</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("main from pkg32.Class2\n");</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> Class2 class2 = new Class2(1, 3);</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("class2.calculation : " + class2.calculation());</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("c from class1 (protected) in class 2: " + class2.c);</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("calculation1 : " + class2.calculation1());</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> }</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">}</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><div><b><span class="Apple-style-span" style="font-weight: normal;">run:</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">main from pkg32.Class2</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">Class1-protected Class1(int a, int b)</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">Class2(int a, int b)</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">Class2-protected int calculation()</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">Class1-protected int calculation()</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">class2.calculation : 4</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">c from class1 (protected) in class 2: -2</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">Class2-protected int calculation1()</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">Class1-protected int calculation()</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">calculation1 : 0</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">BUILD SUCCESSFUL (total time: 0 seconds)</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"></span></b><br />
<b><span class="Apple-style-span" style="font-weight: normal;"></span></b><br />
<b><span class="Apple-style-span" style="font-weight: normal;"></span></b><br />
<b><span class="Apple-style-span" style="font-weight: normal;"></span></b><br />
<b><span class="Apple-style-span" style="font-weight: normal;"></span></b><br />
<div><b><span class="Apple-style-span" style="font-weight: normal;">/*</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> * To change this template, choose Tools | Templates</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> * and open the template in the editor.</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> */</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">package pkg3.pkg32;</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">/**</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> *</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> * @author Rudy</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> */</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">public class Class3 extends Class2 {</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> int d = -999;</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> Class3(int a, int b) {</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> super(a, b);</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("Class3(int a, int b)");</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> super.c = -4;</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> }</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> @Override</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> protected int calculation() {</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("Class3.calculation()");</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> return super.calculation();</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> }</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> @Override</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> protected int calculation1() {</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("Class3.calculation1()");</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> int a = super.calculation() + super.c + c;</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> return a;</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> }</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> int getD() {</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> return super.d;</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> }</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> public static void main(String[] args) {</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("main from pkg32.Class2\n");</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> Class3 class3 = new Class3(1, 3);</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("class3.calculation : " + class3.calculation());</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("c from class1 (protected) in class 2: " + class3.c);</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("calculation1 : " + class3.calculation1());</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("d : " + class3.d);</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> </span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> System.out.println("d from class1 and not defined in class 2 but hidden in class3 : " + class3.getD());</span></b><br />
<b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><b> class3.simplyToAnnoyYou();</b></span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"> }</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">}</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><div><b><span class="Apple-style-span" style="font-weight: normal;">run:</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">main from pkg32.Class2</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">Class1-protected Class1(int a, int b)</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">Class2(int a, int b)</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">Class3(int a, int b)</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">Class3.calculation()</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">Class2-protected int calculation()</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">Class1-protected int calculation()</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">class3.calculation : 4</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">c from class1 (protected) in class 2: -4</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">Class3.calculation1()</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">Class2-protected int calculation()</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">Class1-protected int calculation()</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">calculation1 : -4</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><b>d : -999</b></span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><b>d from class1 and not defined in class 2 but hidden in class3 : -888</b></span></b><br />
<b><span class="Apple-style-span" style="font-weight: normal;"><b>simplyToAnnoyYou</b></span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;">BUILD SUCCESSFUL (total time: 0 seconds)</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><b>Can you explain all the output ?</b></span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><b>:-)</b></span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div><div><b><span class="Apple-style-span" style="font-weight: normal;"><br />
</span></b></div></div><b><span class="Apple-style-span" style="font-weight: normal;"></span></b></div></div></div></div><b></b></div></div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-12007871427127203692010-01-28T04:56:00.000-08:002010-02-19T00:28:48.730-08:0025 - Review - private - level basic - Java 1.6.0_17 - NB 6.8I wanted to make you aware (if you are not) of the following...<br />
<br />
If you make your constructor "private" you can't invoke the constructor from another class... But you can still invoke it from the SAME class. In Netbeans we are used that Netbeans will generate automatically a Main class and a pvsm (public static void main[] args) method in the main class. In this case if you try to instantiate a class using a private constructor you cant... You have to give another ways (have a look to singletons). But if you put a pvsm in the class were the private constructor is defined, no problem you can instantiate the class.<br />
<br />
<span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;">package pkg3.pkg31;</span><br />
<br />
<span style="font-family: 'Courier New',Courier,monospace;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;">class Class1 {</span><br />
<span style="font-family: 'Courier New',Courier,monospace;"><br />
</span><br />
<span style="font-family: 'Courier New',Courier,monospace;"> protected int a;</span><br />
<span style="font-family: 'Courier New',Courier,monospace;"> protected int b;</span><br />
<span style="font-family: 'Courier New',Courier,monospace;"><br />
</span><br />
<span style="font-family: 'Courier New',Courier,monospace;"> <b>private </b>Class1(int a, int b) {</span><br />
<span style="font-family: 'Courier New',Courier,monospace;"> this.a = a;</span><br />
<span style="font-family: 'Courier New',Courier,monospace;"> this.b = b;</span><br />
<span style="font-family: 'Courier New',Courier,monospace;"> }</span><br />
<span style="font-family: 'Courier New',Courier,monospace;"><br />
</span><br />
<span style="font-family: 'Courier New',Courier,monospace;"> protected int calculation() {</span><br />
<span style="font-family: 'Courier New',Courier,monospace;"> return a + b;</span><br />
<span style="font-family: 'Courier New',Courier,monospace;"> }</span><br />
<span style="font-family: 'Courier New',Courier,monospace;"><br />
</span><br />
<span style="font-family: 'Courier New',Courier,monospace;"> public static void main(String[] args) {</span><br />
<span style="font-family: 'Courier New',Courier,monospace;"> System.out.println(new <b>Class1(1, 2)</b>.calculation());</span><br />
<span style="font-family: 'Courier New',Courier,monospace;"> }</span><br />
<span style="font-family: 'Courier New',Courier,monospace;">}</span><br />
<div><br />
</div><div><div>run:</div><div><b>3</b></div><div>BUILD SUCCESSFUL (total time: 0 seconds)</div><div><br />
</div></div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-46153504637189519472010-01-26T01:35:00.000-08:002010-02-19T00:57:13.477-08:0024 - Thread : notify/notifyall- level basic - Java 1.6.0_17 - NB 6.8(<br />
after all the Master Exam questions, they ask you several times to choose notify or notifyAll ...<br />
most of the time if you answer (please read the question carefully) notifyAll, it is the wrong answer...<br />
why ? it is because there is a producer and several consumers... after the producer produced something the producer will notify the consumers... the way they wrote the example, they do a wait but as soon as the thread is notified they do not check that something was produced (in the consumer thread)<br />
if the producer do notifyAll, then (and it is not new) only 1 thread continues in the synchronized block...<br />
this thread will consume what was produced and exit the synch block and it is fine...<br />
but if notifyAll was used by the producer, then the second, third,... thread (one of those) are now notified but... nothing was produced and a thread will...consume (without checking that something was produced) and...that can cause a IndexOutOfBoundsException to be thrown if the thread calls (by ex) refArrayList.remove(0) without checking that the ArrayList is empty... If you answer notify, then only one thread will be notified and then consume... Then the producer after a while (sometimes they put a sleep) will produce and then call notify...<br />
imho you serialize the way you produce and consume<br />
<br />
<br />
you produce<br />
then notify1 amongst all the threads<br />
...<br />
<br />
or<br />
you produce<br />
then notifyAll<br />
<br />
all the threads will be able to continue before the producer produced (one by one in the synch blcok)<br />
and that can cause a problem<br />
(I repeat you have to find a way that the producer made something new)<br />
<br />
<span class="postbody">*** Hope this help ***</span><br />
<br />
)<br />
<br />
<br />
<br />
3 classes. A Main that will start the threads. A producer (calling notify/notifyall) and consumers.<br />
<br />
<span style="text-decoration: underline;"><b>The Producer</b></span><br />
<br />
package threadnotifyall;<br />
<br />
public class Calculator extends Thread {<br />
<br />
int total;<br />
<br />
<b> public void run() {</b><br />
try {<br />
System.out.println("Calculator...");<br />
Thread.currentThread().sleep(1000);<br />
synchronized (this) {<br />
for (int i = 0; i < 100; i++) {<br />
total += i;<br />
}<br />
Thread.currentThread().sleep(5000);<br />
<b> notifyAll(); // OR</b><br />
<b> //notify();</b><br />
}<br />
} catch (InterruptedException ie) {<br />
ie.printStackTrace();<br />
}<br />
}<br />
}<br />
<div><br />
</div><div><span style="text-decoration: underline;"><b>The Consumer</b></span></div><div><br />
</div><div><div>package threadnotifyall;</div><div><br />
</div><div>public class Reader extends Thread {</div><div><b> Calculator c; // reference to the producer</b></div><div><br />
</div><div> public Reader(Calculator c) {</div><div> this.c = c;</div><div> }</div><div><br />
</div><div><b> public void run() { // it is a thread </b></div><div> synchronized(c) { //<b> lock the producer (try to lock)</b></div><div> try {</div><div> System.out.println(Thread.currentThread().getName() + " waiting for calculation...");</div><div><b> c.wait(); // if the producer is locked but did not notify (us) wait to be notified (1 and only 1 thread !) or wait forever (see my remarks later)</b></div><div> } catch(InterruptedException ie) {</div><div> ie.printStackTrace();</div><div> }</div><div> System.out.println(Thread.currentThread().getName() + " Total is :" + c.total); // <b>show the thread that was notified</b></div><div> }</div><div> }</div><div>}</div><div><br />
</div><div><b><span style="text-decoration: underline;">The bandmaster</span></b></div></div><div><div><br />
</div><div>package threadnotifyall;</div><div><br />
</div><div>public class Main {</div><div><br />
</div><div> public static void main(String[] args) {</div><div> </div><div><b> Calculator calculator = new Calculator(); // the producer</b></div><div><br />
</div><div> new Reader(<b>calculator</b>).start(); // <b>3 consumers threads (we pass the producer they have to wait for)</b></div><div> new Reader(<b>calculator</b>).start();</div><div> new Reader(<b>calculator</b>).start();</div><div> calculator.start(); // <b>the 3 consumers were started (all waiting and the producer shows up)</b></div><div> System.out.println("end of main"); <b>// end of the THREAD main (because main is a THREAD)</b></div><div> }</div><div>}</div><div><br />
</div><div><b><span style="text-decoration: underline;">With notify</span></b></div><div><br />
</div><div><b>IT IS AN ASSUMPTION BECAUSE THE WAY THE JAVA SCHEDULER WILL CHOOSE THE THREAD DEPENDS ON A FEW FACTORS (WE CAN INFLUENCE)</b></div><div><br />
</div><div><div><b>run:</b></div><div><b>Thread-1 waiting for calculation...</b></div><div><b>Thread-2 waiting for calculation...</b></div><div><b>Thread-3 waiting for calculation...</b></div><div><b>end of main</b></div><div><b>Calculator...</b></div><div><b>Thread-1 Total is :4950</b></div><div><b>Thread-3 Total is :4950</b></div><div><b>Thread-2 Total is :4950</b></div><div><b>BUILD SUCCESSFUL (total time: 5 seconds)</b></div><div><br />
</div><div><div><b>run:</b></div><div><b>Thread-1 waiting for calculation...</b></div><div><b>Thread-2 waiting for calculation...</b></div><div><b>end of main</b></div><div><b>Thread-3 waiting for calculation...</b></div><div><b>Calculator...</b></div><div><b>Thread-1 Total is :4950</b></div><div><b>Thread-3 Total is :4950</b></div><div><b>Thread-2 Total is :4950</b></div><div><b>BUILD SUCCESSFUL (total time: 5 seconds)</b></div><div><br />
</div><div>I did several executions and can report that if we use notify, it is always <b>the first thread that was started</b> that will be choosen (notified that the producer has something to deliver). In the output as it is Thread-1 that was the first to be started (and waiting), <b>Thread-1 is the first to be notified.</b>.. You can see that the main thread terminated BEFORE the 3 threads were terminated. But Java is polite :-) and will wait that the 3 consumers threads will terminate.</div><div><br />
</div><div><span style="text-decoration: underline;"><b>With notifyall</b></span></div><div><br />
</div><div><div>run:</div><div>Thread-1 waiting for calculation...</div><div>Thread-2 waiting for calculation...</div><div>end of main</div><div>Thread-3 waiting for calculation...</div><div>Calculator...</div><div>Thread-3 Total is :4950</div><div>Thread-2 Total is :4950</div><div>Thread-1 Total is :4950</div><div>BUILD SUCCESSFUL (total time: 5 seconds)</div><div><br />
</div><div><div>run:</div><div>Thread-1 waiting for calculation...</div><div>Thread-3 waiting for calculation...</div><div>Thread-2 waiting for calculation...</div><div>end of main</div><div>Calculator...</div><div>Thread-2 Total is :4950</div><div>Thread-3 Total is :4950</div><div>Thread-1 Total is :4950</div><div>BUILD SUCCESSFUL (total time: 5 seconds)</div><div><br />
</div><div><div>run:</div><div>Thread-1 waiting for calculation...</div><div>Thread-3 waiting for calculation...</div><div>Thread-2 waiting for calculation...</div><div>end of main</div><div>Calculator...</div><div>Thread-2 Total is :4950</div><div>Thread-3 Total is :4950</div><div>Thread-1 Total is :4950</div><div>BUILD SUCCESSFUL (total time: 5 seconds)</div><div><br />
</div><div>As you can see using notifyall, it is the <b>last thread </b>that was started that was notified this time.</div><div><br />
</div><div>Following my experimentation with the kind of JVM that I use</div><div><ul><li>If notify is used, then it is the thread <b>that was the FIRST to be waiting</b> that will be notified. Then the second,... Then the third... until all the threads waiting to be notified.</li>
<li>If notifyall is used, then it is the thread that was the <b>LAST </b>to be waiting that will be notified, then the previous, etc.</li>
<li>The Java scheduler will choose a thread to be notified (there is a list of threads awaiting) and the one that is awaken continues JUST AFTER the wait !!! But the awaken thread does not have to execute "synchronized" to get the lock. The Java scheduler guaranty the thread to receive the lock if the thread is chosen. ALL the other threads ARE WAITING to be notified (IMHO there are NOT in the runnable queue; the threads are in a special queue). They will all be notified ONE BY ONE. The way there are chosen depends on notify / notifyall. But the mechanism to awake the threads is the same. <b>One by one !!! But look in the beg of this post, if nothing was produced and the thread that is awaken does not check that something is produced (with notifyAll) you can cause a IndexOutOfBoundsException to be thrown !!!</b></li>
<li>If you inverse the consumers and the producers meaning you start the producer first and later the consumer and let's say you also put a sleep just after the producer finishes. The consumers will ALL wait to be notified but will never be notified because the producer finished before any consumers were started. You see that the main thread is finished, but all the others keep waiting <b>FOREVER </b>!!! Thus the sequence of events is important and it is written in the Sierra/Bates book that you have to wait for something that will happen (by example there is something to be read from a queue and it is your application queue) but NOT unconditionaly !!!</li>
</ul></div><div><br />
</div><div><br />
</div></div></div></div></div></div></div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com1tag:blogger.com,1999:blog-836520676967135831.post-39801033467961199352010-01-25T05:47:00.000-08:002010-02-19T00:27:26.516-08:0023 - Thread : deadlock - level basic - Java 1.6.0_17 - NB 6.8A deadlock is rather easy to produce having 2 actors (2 threads) accessing 2 resources at the same time but in different order !<br />
<br />
T1-------------T2<br />
res A----------res B<br />
res B----------res A<br />
<br />
If T1 put a lock on res A, T2 put a lock on res B, then T1 tries to lock res B (waiting the lock on res B to be released) an then T2 tries to lock res A (waiting the lock put by T1 to be released). The 2 threads do not speak to each others and will wait...indefinitely... The JVM seems not able to deal with such a problem...<br />
You have to deal with it !!!<br />
<br />
<br />
package threaddeadlock;<br />
<br />
public class Main implements Runnable {<br />
<br />
ClassA refA = new ClassA();<br />
ClassB refB = new ClassB();<br />
<br />
<b> void operation1() throws Exception {</b><br />
<b> synchronized (refA) {</b><br />
<b> refA.method1();</b><br />
<b> synchronized (refB) {</b><br />
<b> refB.method2();</b><br />
<b> }</b><br />
<b> }</b><br />
<b> }</b><br />
<br />
<b> void operation2() throws Exception {</b><br />
<b> synchronized (refB) {</b><br />
<b> refB.method2();</b><br />
<b> synchronized (refA) {</b><br />
<b> refA.method1();</b><br />
<b> }</b><br />
<b> }</b><br />
<b> }</b><br />
<br />
public void run() {<br />
try {<br />
<b> if (Thread.currentThread().getName().equals("t1")) {</b><br />
<b> operation1();</b><br />
<b> } else if (Thread.currentThread().getName().equals("t2")) {</b><br />
<b> operation2();</b><br />
<b> }</b> else { // should be an assertion !!!<br />
System.err.println("thread name must be t1 or t2 !!!");<br />
System.exit(1);<br />
}<br />
} catch (Exception e) {<br />
e.printStackTrace();<br />
}<br />
}<br />
<br />
public static void main(String[] args) {<br />
Main main = new Main();<br />
Thread t1 = new Thread(main);<br />
t1.setName("t1");<br />
Thread t2 = new Thread(main);<br />
t2.setName("t2");<br />
<b> t1.start();</b><br />
<b> t2.start();</b><br />
}<br />
}<br />
<br />
<b><span style="color: red;">wait forever in front of your screen... :-)</span></b><br />
<br />
<br />
run:<br />
t1 method1<br />
t2 method2<br />
<span style="color: red;"><b>BUILD STOPPED (total time: 13 minutes 30 seconds)</b></span><br />
<div><br />
</div><br />
<div><div><br />
</div><div>package threaddeadlock;</div><div><br />
</div><div>public class ClassA {</div><div><br />
</div><div> void method1() {</div><div> System.out.println(Thread.currentThread().getName() + " method1");</div><div> }</div><div><br />
</div><div>}</div><div><br />
</div><div><div>package threaddeadlock;</div><div><br />
</div><div>public class ClassB {</div><div><br />
</div><div> void method2() {</div><div> System.out.println(Thread.currentThread().getName() + " method2");</div><div> }</div><div>}</div><div><br />
</div></div><div><br />
</div></div><div><br />
</div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-11471994545522714072010-01-25T03:30:00.000-08:002010-02-19T00:26:58.490-08:0022 - Thread : same or different object ? - level basic - Java 1.6.0_17 - NB 6.8Look at the 2 versions of main<br />
1 will create a new object of type ClassA for every thread<br />
the second will not and look at the difference !<br />
<br />
<br />
package threadsleep;<br />
<br />
public class Main {<br />
<br />
public static void main(String[] args) {<br />
<b> // 1st version new object ClassA of every thread</b><br />
//ClassA ca = new ClassA();<br />
//Thread thread;<br />
for(int i=0; i < 10; i++) {<br />
//thread = new Thread(ca);<br />
//thread.start();<br />
<b> new Thread(new ClassA()).start();</b><br />
}<br />
}<br />
}<br />
<div><br />
</div><div><div>run:</div><div>Thread-0</div><div>a : Thread-0 sleeping 5 seconds...</div><div>Thread-1</div><div>a : Thread-1 sleeping 5 seconds...</div><div>Thread-3</div><div>a : Thread-3 sleeping 5 seconds...</div><div>Thread-5</div><div>a : Thread-5 sleeping 5 seconds...</div><div>Thread-7</div><div>a : Thread-7 sleeping 5 seconds...</div><div>Thread-6</div><div>a : Thread-6 sleeping 5 seconds...</div><div>Thread-2</div><div>a : Thread-2 sleeping 5 seconds...</div><div>Thread-4</div><div>a : Thread-4 sleeping 5 seconds...</div><div>Thread-8</div><div>a : Thread-8 sleeping 5 seconds...</div><div>Thread-9</div><div>a : Thread-9 sleeping 5 seconds...</div><div><b>a : Thread-0-Thread-0 after sleeping 5 seconds...</b></div><div><b>a : Thread-1-Thread-1 after sleeping 5 seconds...</b></div><div><b>a : Thread-5-Thread-5 after sleeping 5 seconds...</b></div><div><b>a : Thread-3-Thread-3 after sleeping 5 seconds...</b></div><div><b>a : Thread-7-Thread-7 after sleeping 5 seconds...</b></div><div><b>a : Thread-6-Thread-6 after sleeping 5 seconds...</b></div><div><b>a : Thread-2-Thread-2 after sleeping 5 seconds...</b></div><div><b>a : Thread-4-Thread-4 after sleeping 5 seconds...</b></div><div><b>a : Thread-9-Thread-9 after sleeping 5 seconds...</b></div><div><b>a : Thread-8-Thread-8 after sleeping 5 seconds...</b></div><div>BUILD SUCCESSFUL (total time: 10 seconds)</div><div><br />
</div><div><b>The instance variable "a" was not modified because for every thread there is a different "a" !</b></div><div><br />
</div><div><div>public class Main {</div><div><br />
</div><div> public static void main(String[] args) {</div><div> <b> // 2nd version the thread will share the same object</b></div><div> <b> ClassA ca = new ClassA();</b></div><div><b> Thread thread;</b></div><div><b> </b> for(int i=0; i < 10; i++) {</div><div> <b> thread = new Thread(ca);</b></div><div><b> thread.start();</b></div><div><b> </b> //new Thread(new ClassA()).start();</div><div> }</div><div> }</div><div>}</div></div><div><br />
</div><div><div>run:</div><div>Thread-0</div><div>a : Thread-0 sleeping 5 seconds...</div><div>Thread-1</div><div>a : Thread-1 sleeping 5 seconds...</div><div>Thread-2</div><div>a : Thread-2 sleeping 5 seconds...</div><div>Thread-3</div><div>a : Thread-3 sleeping 5 seconds...</div><div>Thread-5</div><div>a : Thread-5 sleeping 5 seconds...</div><div>Thread-7</div><div>a : Thread-7 sleeping 5 seconds...</div><div>Thread-9</div><div>a : Thread-9 sleeping 5 seconds...</div><div>Thread-4</div><div>a : Thread-4 sleeping 5 seconds...</div><div>Thread-6</div><div>a : Thread-6 sleeping 5 seconds...</div><div>Thread-8</div><div>a : Thread-8 sleeping 5 seconds...</div><div><b>a : Thread-8-Thread-0 after sleeping 5 seconds...</b></div><div><b>a : Thread-8-Thread-1 after sleeping 5 seconds...</b></div><div><b>a : Thread-8-Thread-2 after sleeping 5 seconds...</b></div><div><b>a : Thread-8-Thread-5 after sleeping 5 seconds...</b></div><div><b>a : Thread-8-Thread-3 after sleeping 5 seconds...</b></div><div><b>a : Thread-8-Thread-7 after sleeping 5 seconds...</b></div><div><b>a : Thread-8-Thread-9 after sleeping 5 seconds...</b></div><div><b>a : Thread-8-Thread-4 after sleeping 5 seconds...</b></div><div><b>a : Thread-8-Thread-6 after sleeping 5 seconds...</b></div><div><b>a : Thread-8-Thread-8 after sleeping 5 seconds...</b></div><div>BUILD SUCCESSFUL (total time: 11 seconds)</div><div><br />
</div><div>The thread "8" had a big influence on all the others threads because the thread "8" modified the instance variable "a" ! In fact it is because this poor thread "8" was the last to be started and as he is the last, he modified the same "a" instance variable for all the threads !!!</div><div><br />
</div><div><div>/*</div><div> * To change this template, choose Tools | Templates</div><div> * and open the template in the editor.</div><div> */</div><div>package threadsleep;</div><div><br />
</div><div>public class ClassA implements Runnable {</div><div><br />
</div><div> <b> String a;</b></div><div><br />
</div><div> public void run() {</div><div> Thread currenthread = Thread.currentThread();</div><div> <b> a = currenthread.getName();</b></div><div> System.out.println(a);</div><div> try {</div><div> <b> System.out.println("a : " + a + " sleeping 5 seconds...");</b></div><div><b> currenthread.sleep(10000);</b></div><div><b> System.out.println("a : " + a + "-" + currenthread.getName()+ " after sleeping 5 seconds...");</b></div><div> } catch (InterruptedException ie) {</div><div> ie.printStackTrace();</div><div> }</div><div> }</div><div>}</div><div><br />
</div></div><div>You can share the same objects. You will avoid to waste memory... But if your threads modify the data... You have to find a way keep the operations/data modifications consistent... In other words all the operations you do that can corrupt the data have to be atomic to avoid the corruption...<br />
<br />
</div><div><br />
</div><div>- Rudy -</div><div><br />
</div></div><div><br />
</div></div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-33501004224903632662010-01-25T02:12:00.000-08:002010-02-19T00:26:40.093-08:0021 - example of Thread.join - level basic - Java 1.6.0_17 - NB 6.83 classes<br />
Main<br />
ClassA<br />
and ClassB<br />
Main will start 2 threads one of ClassA and one of ClassB.<br />
The threads do a loop and wait some time after each iteration.<br />
After 3 seconds Main will send a message to ClassA (thread A) to join the Thread B.<br />
(ClassA will check an instance variable and if this instance variable is initialized, then it is the thread to join)<br />
<br />
<br />
package threadjoin;<br />
<br />
public class Main {<br />
<br />
public static void main(String[] args) {<br />
<br />
ClassA a = new ClassA();<br />
ClassB b = new ClassB();<br />
<br />
Thread ta = new Thread(a);<br />
Thread tb = new Thread(b);<br />
<br />
ta.start();<br />
tb.start();<br />
<br />
<b> System.out.println("sleeping 3 seconds ! then will force Thread A to join Thread B !");</b><br />
<b> Thread main = Thread.currentThread();</b><br />
<b> try {</b><br />
<b> main.sleep(3000);</b><br />
<b> a.threadToJoin = tb;</b><br />
<b> } catch (InterruptedException ie) {</b><br />
<b> ie.printStackTrace();</b><br />
<b> }</b><br />
<b><br />
</b><br />
<b> System.out.println("after 3 seconds and thread A should join thread B !");</b><br />
}<br />
}<br />
<div><br />
</div><div><div>package threadjoin;</div><div><br />
</div><div>/**</div><div> *</div><div> * @author Rudy</div><div> */</div><div>public class ClassA implements Runnable {</div><div><br />
</div><div><b> Thread threadToJoin;</b></div><div> final static int counter = 100;</div><div> final static int timeToWait = 100000000;</div><div><br />
</div><div> public void run() {</div><div> for (int c = 0; c < counter; c++) {</div><div> System.out.println("A --- Loop #" + c);</div><div> for (int t = 0; t < timeToWait; t++);</div><div><b> if (threadToJoin != null) {</b></div><div><b> try {</b></div><div><b> System.out.println("Thread A joining Thread B !!!");</b></div><div><b> threadToJoin.join();</b></div><div><b> // deactivation of the test !</b></div><div><b> threadToJoin = null;</b></div><div><b> } catch (InterruptedException ie) {</b></div><div><b> ie.printStackTrace();</b></div><div><b> }</b></div><div><b> }</b></div><div> }</div><div><br />
</div><div> }</div><div>}</div><div><br />
</div><div><div>package threadjoin;</div><div><br />
</div><div>/**</div><div> *</div><div> * @author Rudy</div><div> */</div><div>public class ClassB implements Runnable {</div><div><br />
</div><div> final static int counter = 100;</div><div> final static int timeToWait = 100000000;</div><div><br />
</div><div> public void run() {</div><div> for (int c = 0; c < counter; c++) {</div><div> System.out.println("B --- Loop #" + c);</div><div> for (int t = 0; t < timeToWait; t++);</div><div> }</div><div><br />
</div><div> }</div><div>}</div><div><br />
</div><div><div>run:</div><div><b>sleeping 3 seconds ! then will force Thread A to join Thread B !</b></div><div>A --- Loop #0</div><div>B --- Loop #0</div><div>A --- Loop #1</div><div>B --- Loop #1</div><div>A --- Loop #2</div><div>B --- Loop #2</div><div>A --- Loop #3</div><div>B --- Loop #3</div><div>A --- Loop #4</div><div>B --- Loop #4</div><div>A --- Loop #5</div><div>B --- Loop #5</div><div>A --- Loop #6</div><div>B --- Loop #6</div><div>B --- Loop #7</div><div>A --- Loop #7</div><div>B --- Loop #8</div><div>A --- Loop #8</div><div>B --- Loop #9</div><div>A --- Loop #9</div><div>B --- Loop #10</div><div>A --- Loop #10</div><div>B --- Loop #11</div><div>A --- Loop #11</div><div>B --- Loop #12</div><div>A --- Loop #12</div><div>B --- Loop #13</div><div>A --- Loop #13</div><div>B --- Loop #14</div><div>A --- Loop #14</div><div>B --- Loop #15</div><div>A --- Loop #15</div><div>B --- Loop #16</div><div>A --- Loop #16</div><div>B --- Loop #17</div><div>A --- Loop #17</div><div>B --- Loop #18</div><div>A --- Loop #18</div><div>B --- Loop #19</div><div>A --- Loop #19</div><div>B --- Loop #20</div><div>A --- Loop #20</div><div>B --- Loop #21</div><div>A --- Loop #21</div><div>B --- Loop #22</div><div>B --- Loop #23</div><div>A --- Loop #22</div><div>B --- Loop #24</div><div>A --- Loop #23</div><div>B --- Loop #25</div><div>A --- Loop #24</div><div>B --- Loop #26</div><div>A --- Loop #25</div><div>B --- Loop #27</div><div>A --- Loop #26</div><div>B --- Loop #28</div><div>A --- Loop #27</div><div>B --- Loop #29</div><div>A --- Loop #28</div><div>B --- Loop #30</div><div>A --- Loop #29</div><div>B --- Loop #31</div><div>A --- Loop #30</div><div>B --- Loop #32</div><div>A --- Loop #31</div><div>B --- Loop #33</div><div>A --- Loop #32</div><div>B --- Loop #34</div><div>A --- Loop #33</div><div>B --- Loop #35</div><div>A --- Loop #34</div><div>B --- Loop #36</div><div>A --- Loop #35</div><div>B --- Loop #37</div><div>A --- Loop #36</div><div>B --- Loop #38</div><div>A --- Loop #37</div><div>B --- Loop #39</div><div>A --- Loop #38</div><div>B --- Loop #40</div><div>A --- Loop #39</div><div>B --- Loop #41</div><div>A --- Loop #40</div><div>B --- Loop #42</div><div>A --- Loop #41</div><div>B --- Loop #43</div><div>A --- Loop #42</div><div>B --- Loop #44</div><div>A --- Loop #43</div><div>B --- Loop #45</div><div>A --- Loop #44</div><div>B --- Loop #46</div><div>A --- Loop #45</div><div>B --- Loop #47</div><div>A --- Loop #46</div><div>B --- Loop #48</div><div>A --- Loop #47</div><div>B --- Loop #49</div><div>A --- Loop #48</div><div>B --- Loop #50</div><div>A --- Loop #49</div><div>B --- Loop #51</div><div>A --- Loop #50</div><div>B --- Loop #52</div><div>A --- Loop #51</div><div>B --- Loop #53</div><div><b>after 3 seconds and thread A should join thread B !</b></div><div><b>Thread A joining Thread B !!!</b></div><div>B --- Loop #54</div><div>B --- Loop #55</div><div>B --- Loop #56</div><div>B --- Loop #57</div><div>B --- Loop #58</div><div>B --- Loop #59</div><div>B --- Loop #60</div><div>B --- Loop #61</div><div>B --- Loop #62</div><div>B --- Loop #63</div><div>B --- Loop #64</div><div>B --- Loop #65</div><div>B --- Loop #66</div><div>B --- Loop #67</div><div>B --- Loop #68</div><div>B --- Loop #69</div><div>B --- Loop #70</div><div>B --- Loop #71</div><div>B --- Loop #72</div><div>B --- Loop #73</div><div>B --- Loop #74</div><div>B --- Loop #75</div><div>B --- Loop #76</div><div>B --- Loop #77</div><div>B --- Loop #78</div><div>B --- Loop #79</div><div>B --- Loop #80</div><div>B --- Loop #81</div><div>B --- Loop #82</div><div>B --- Loop #83</div><div>B --- Loop #84</div><div>B --- Loop #85</div><div>B --- Loop #86</div><div>B --- Loop #87</div><div>B --- Loop #88</div><div>B --- Loop #89</div><div>B --- Loop #90</div><div>B --- Loop #91</div><div>B --- Loop #92</div><div>B --- Loop #93</div><div>B --- Loop #94</div><div>B --- Loop #95</div><div>B --- Loop #96</div><div>B --- Loop #97</div><div>B --- Loop #98</div><div>B --- Loop #99</div><div>A --- Loop #52</div><div>A --- Loop #53</div><div>A --- Loop #54</div><div>A --- Loop #55</div><div>A --- Loop #56</div><div>A --- Loop #57</div><div>A --- Loop #58</div><div>A --- Loop #59</div><div>A --- Loop #60</div><div>A --- Loop #61</div><div>A --- Loop #62</div><div>A --- Loop #63</div><div>A --- Loop #64</div><div>A --- Loop #65</div><div>A --- Loop #66</div><div>A --- Loop #67</div><div>A --- Loop #68</div><div>A --- Loop #69</div><div>A --- Loop #70</div><div>A --- Loop #71</div><div>A --- Loop #72</div><div>A --- Loop #73</div><div>A --- Loop #74</div><div>A --- Loop #75</div><div>A --- Loop #76</div><div>A --- Loop #77</div><div>A --- Loop #78</div><div>A --- Loop #79</div><div>A --- Loop #80</div><div>A --- Loop #81</div><div>A --- Loop #82</div><div>A --- Loop #83</div><div>A --- Loop #84</div><div>A --- Loop #85</div><div>A --- Loop #86</div><div>A --- Loop #87</div><div>A --- Loop #88</div><div>A --- Loop #89</div><div>A --- Loop #90</div><div>A --- Loop #91</div><div>A --- Loop #92</div><div>A --- Loop #93</div><div>A --- Loop #94</div><div>A --- Loop #95</div><div>A --- Loop #96</div><div>A --- Loop #97</div><div>A --- Loop #98</div><div>A --- Loop #99</div><div>BUILD SUCCESSFUL (total time: 9 seconds)</div><div><br />
</div><div>As you can see the Java scheduler will make the thread A and B execute at the same time.</div><div>We see<br />
<br />
<div><div style="margin: 0px;">A --- Loop #0</div></div><div><div style="margin: 0px;">B --- Loop #0</div></div><div><div style="margin: 0px;">A --- Loop #1</div></div><div><div style="margin: 0px;">B --- Loop #1</div></div><div><div style="margin: 0px;">A --- Loop #2</div></div><div><div style="margin: 0px;">B --- Loop #2</div></div><div><div style="margin: 0px;">A --- Loop #3</div></div><div><div style="margin: 0px;">B --- Loop #3</div></div><div><div style="margin: 0px;">A --- Loop #4</div></div><div><div style="margin: 0px;">B --- Loop #4</div></div><div><div style="margin: 0px;">A --- Loop #5</div></div><div><div style="margin: 0px;">B --- Loop #5</div></div><div><div style="margin: 0px;">A --- Loop #6</div><div style="margin: 0px;">...</div></div><div></div><br />
</div><div>Then after 3 seconds A joined B and thus A wait until the end of B.</div><div>We see<br />
<br />
<div><div style="margin: 0px;">B --- Loop #54</div></div><div><div style="margin: 0px;">B --- Loop #55</div></div><div><div style="margin: 0px;">B --- Loop #56</div></div><div><div style="margin: 0px;">B --- Loop #57</div></div><div><div style="margin: 0px;">B --- Loop #58</div></div><div><div style="margin: 0px;">B --- Loop #59</div></div><div><div style="margin: 0px;">B --- Loop #60</div></div><div><div style="margin: 0px;">B --- Loop #61</div></div><div><div style="margin: 0px;">B --- Loop #62</div></div><div><div style="margin: 0px;">B --- Loop #63</div></div><div><div style="margin: 0px;">B --- Loop #64</div></div><div><div style="margin: 0px;">B --- Loop #65</div></div><div><div style="margin: 0px;">B --- Loop #66</div></div><div><div style="margin: 0px;">B --- Loop #67</div></div><div><div style="margin: 0px;">B --- Loop #68</div></div><div><div style="margin: 0px;">B --- Loop #69</div></div><div><div style="margin: 0px;">B --- Loop #70</div></div><div><div style="margin: 0px;">B --- Loop #71</div></div><div><div style="margin: 0px;">B --- Loop #72</div></div><div><div style="margin: 0px;">B --- Loop #73</div><div style="margin: 0px;">...</div></div><div><div style="margin: 0px;">B --- Loop #94</div></div><div><div style="margin: 0px;">B --- Loop #95</div></div><div><div style="margin: 0px;">B --- Loop #96</div></div><div><div style="margin: 0px;">B --- Loop #97</div></div><div><div style="margin: 0px;">B --- Loop #98</div></div><div><div style="margin: 0px;">B --- Loop #99<br />
<br />
Look A is back !!!<br />
<br />
</div></div><div><div style="margin: 0px;">A --- Loop #52</div></div><div><div style="margin: 0px;">A --- Loop #53</div></div><div><div style="margin: 0px;">A --- Loop #54</div></div><div><div style="margin: 0px;">A --- Loop #55</div></div><div><div style="margin: 0px;">A --- Loop #56</div></div><div><div style="margin: 0px;">A --- Loop #57</div></div><div><div style="margin: 0px;">A --- Loop #58</div></div><div><div style="margin: 0px;">A --- Loop #59</div></div><div><div style="margin: 0px;">A --- Loop #60</div></div><div><div style="margin: 0px;">A --- Loop #61</div></div><div><div style="margin: 0px;">A --- Loop #62</div></div><div><div style="margin: 0px;">A --- Loop #63</div></div><div><div style="margin: 0px;">A --- Loop #64</div></div><div><div style="margin: 0px;">A --- Loop #65</div></div><div><div style="margin: 0px;">A --- Loop #66</div></div><div><div style="margin: 0px;">A --- Loop #67</div></div><div><div style="margin: 0px;">A --- Loop #68</div></div><div><div style="margin: 0px;">A --- Loop #69</div></div><div><div style="margin: 0px;">A --- Loop #70</div></div><div><div style="margin: 0px;">A --- Loop #71</div></div><div><div style="margin: 0px;">A --- Loop #72</div></div><div><div style="margin: 0px;">A --- Loop #73</div></div><div><div style="margin: 0px;">A --- Loop #74</div></div><div><div style="margin: 0px;">A --- Loop #75</div></div><div><div style="margin: 0px;">A --- Loop #76</div></div><div><div style="margin: 0px;">...</div></div><div><div style="margin: 0px;">A --- Loop #86</div></div><div><div style="margin: 0px;">A --- Loop #87</div></div><div><div style="margin: 0px;">A --- Loop #88</div></div><div><div style="margin: 0px;">A --- Loop #89</div></div><div><div style="margin: 0px;">A --- Loop #90</div></div><div><div style="margin: 0px;">A --- Loop #91</div></div><div><div style="margin: 0px;">A --- Loop #92</div></div><div><div style="margin: 0px;">A --- Loop #93</div></div><div><div style="margin: 0px;">A --- Loop #94</div></div><div><div style="margin: 0px;">A --- Loop #95</div></div><div><div style="margin: 0px;">A --- Loop #96</div></div><div><div style="margin: 0px;">A --- Loop #97</div></div><div><div style="margin: 0px;">A --- Loop #98</div></div><div><div style="margin: 0px;">A --- Loop #99</div><div style="margin: 0px;"><br />
</div></div><div></div><br />
</div><div>- Rudy -</div></div></div></div><div><br />
</div><div><br />
</div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-20871495453109199072010-01-22T09:02:00.000-08:002010-01-22T09:02:41.149-08:0020 - non public class and public method - level very basic - Java 1.6.0_17 - NB 6.8OK it's for newbies...<br />
<br />
But is it clear in your mind ?<br />
<b>If the class is not public even if the method is public you can't use the method !</b><br />
<br />
<b>package javaapplication10;<br />
</b><br />
<b>class Main { // where is public ???<br />
</b><br />
<b> public void a() { // it is public !!!<br />
</b> System.out.println("a");<br />
}<br />
<br />
public static void main(String[] args) {<br />
new Main().a(); // here you can<br />
}<br />
}<br />
<br />
<b>package newpackage;</b><br />
<br />
<b style="color: red;">import javaapplication10.Main; </b><b><span style="color: red;">// DOES NOT COMPILE !</span><br />
</b><br />
public class Test {<br />
<br />
public static void main(String[] args) {<br />
<b style="color: red;"> new Main().a(); </b><b><span style="color: red;">// DOES NOT COMPILE !</span><br />
</b><br />
}<br />
}Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-3108517959295682242010-01-16T08:08:00.000-08:002010-01-17T01:14:07.552-08:0019 - hashCode - level basic - Java 1.6.0_17 - NB 6.8<div><b><span style="color: red;">TO BE REPHRASED !!!</span></b><br />
<br />
<span style="text-decoration: underline;">To summarize</span><br />
<br />
It is not enough to override "equals" and then try to retrieve keys in a map... If the 2 objects are equal, then the hash codes have to be the same. I suppose they play a max on this one...<br />
<br />
<span style="text-decoration: underline;">More on this topic</span><br />
<br />
You put an object in a map(the key). Then you change the object. And you base the hash code on the length of the String... The key changed but the method "get" will look in the bucket corresponding to the new hash code and will not find the value...<br />
<br />
<span style="text-decoration: underline;">pseudo code</span><br />
<br />
key1, value1<br />
length("key1") => bucket 4<br />
then you change key1 in "key11"<br />
the object is still in bucket 4 but the hash code will reference the bucket5 !!! because lenght("key11") is... 5 !!!<br />
It is not a good idea to change the key... I pref remove and then add...<br />
<br />
<span style="text-decoration: underline;">To understand the statement on p 554, I propose to study the following example :</span><br />
</div><div><br />
</div><div>1) equals is overridden and hashCode is not.<br />
</div><div>2) equals is overridden and and hashCode is overridden and returns always 0.<br />
</div><div>3) equals is overridden and hashCode is overridden and returns the length of the string.<br />
</div><div><br />
</div><div>package mapandkeys;<br />
</div><div><br />
</div><div><br />
</div><div>public class Dog {<br />
</div><div><br />
</div><div> String name;<br />
</div><div><br />
</div><div> public Dog(String n) {<br />
</div><div> name = n;<br />
</div><div> }<br />
</div><div><br />
</div><div> public boolean equals(Object o) {<br />
</div><div> System.out.println("accessing method equals");<br />
</div><div> if((o instanceof Dog) && (((Dog)o).name == name)) {<br />
</div><div> return true;<br />
</div><div> }<br />
</div><div> return false;<br />
</div><div> }<br />
</div><div><br />
</div><div><br />
</div><div><b>hasCode NOT OVERRIDDEN</b><br />
</div><div><br />
</div><div>OR<br />
</div><div><br />
</div><div><b>// public int hashCode() {</b><br />
</div><div><b>// System.out.println("accessing method hashcode");</b><br />
</div><div><b>// return 0;</b><br />
</div><div><b>//</b><br />
</div><div><b>// }</b><br />
</div><div><br />
</div><div>OR<br />
</div><div><br />
</div><div><b>// public int hashCode() {</b><br />
</div><div><b>// System.out.println("accessing method hashcode");</b><br />
</div><div><b>// return name.length();</b><br />
</div><div><b>//</b><br />
</div><div><b>// }</b><br />
</div><div><br />
</div><div><br />
</div><div> public String toString() {<br />
</div><div> return name;<br />
</div><div> }<br />
</div><div>}<br />
</div><div><br />
</div><div><br />
</div><div>package mapandkeys;<br />
</div><div><br />
</div><div>public class Cat {<br />
</div><div>}<br />
</div><div><br />
</div><div>package mapandkeys;<br />
</div><div><br />
</div><div><b>import java.util.HashMap;</b><br />
</div><div><b>import java.util.Map;</b><br />
</div><div><br />
</div><div>public class Main {<br />
</div><div><br />
</div><div> public static void main(String[] args) {<br />
</div><div> Map<object, object=""> m = new HashMap<object, object="">();</object,></object,><br />
</div><div><b> m.put("k1", "k1");</b><br />
</div><div><b> m.put("k2", "k2");</b><br />
</div><div> System.out.println("1 size:" + m.size());<br />
</div><div><br />
<b> String aString = (String) m.get("k1");</b><br />
<br />
<b>The 3 will return "k1"</b><br />
<span style="text-decoration: underline;">default :</span> it is the same "k1" from the string pool and hashCode will return the same id<br />
0 : all objects will return the same bucket (bucket 0)<br />
<span style="text-decoration: underline;">lenght :</span> have the same length (bucket 2)<br />
<br />
</div><div> System.out.println("2 aString : " + aString);<br />
</div><div><br />
</div><div><b> m.put(new Dog("dog1"), "dog1");</b><br />
</div><div><b> m.put(new Dog("dog2"), "dog2");</b><br />
</div><div> System.out.println("3 size:" + m.size());<br />
</div><div><b> aString = (String) m.get(new Dog("dog1"));</b><br />
</div><div> System.out.println("4 aString : " + aString);<br />
</div><div><b> aString = (String) m.get(new Dog("dog2"));</b><br />
</div><div> System.out.println("5 aString : " + aString);<br />
</div><div><br />
<span style="text-decoration: underline;"><b>default :</b></span><b> You do a new 2 times and the default hashCode will return 2 diffent hash ! You will not be able to retrieve the values !!!</b><br />
0 and length will return the hash same thus we can retrieve the keys.<br />
<br />
</div><div> Cat aCat = new Cat();<br />
</div><div> System.out.println("hash of aCat :" + aCat.hashCode());<br />
</div><div><b> m.put(aCat, "cat1");</b><br />
</div><div> System.out.println("6 size:" + m.size());<br />
</div><div><b> aString = (String) m.get(aCat);</b><br />
</div><div> System.out.println("7 aString : " + aString);<br />
<br />
</div><div>Nothing was overridden in the Cat class and you do a "get" on the same cat (same object and same hash code) so the "cat1" is returned.<br />
</div><div><br />
</div><div> Dog d1Key = new Dog("cloverkey");<br />
</div><div> Dog d1Value = new Dog("clovervalue");<br />
</div><div> <b> m.put(d1Key, d1Value);</b><br />
</div><div><b> </b> System.out.println("8 size:" + m.size());<br />
</div><div> <b> System.out.println("9 d1Value : " + m.get(d1Key));</b><br />
<br />
It is the same object and can be found in the 3 cases.<br />
<br />
</div><div><b> d1Value.name = "clovervalue1";</b><br />
</div><div> System.out.println("10 size:" + m.size());<br />
</div><div><b> System.out.println("11 d1Value : " + m.get(d1Key));</b><br />
<br />
</div><div>The value was changed but not the key. The 3 will return the same (new) value.<br />
<br />
</div><div><b> d1Key.name = "1";</b><br />
</div><div><br />
<b>The key is changed !!! It was "cloverkey" and now it is "1" !</b><br />
<br />
</div><div><b> System.out.println("12 d1Value() : " + m.get(d1Key));</b><br />
</div><div><br />
<b>This time we change the key ! The hash code will make a difference...</b><br />
<b>def and 0, both point to the same bucket</b><br />
<b>length("clovervalue") and length("1") are different... Java will try to find the key in the wrong bucket !</b><br />
<br />
</div><div><b> Dog d2 = new Dog("1");</b><br />
</div><div><b> System.out.println("13 d1Value() : " + m.get(d2));</b><br />
</div><div><br />
<span style="text-decoration: underline;">default :</span> it is a different object because we used the keyword new !<br />
0 : OK They all use the bucket 0 and it is not performant !<br />
<span style="text-decoration: underline;">lenght :</span> NOK because the length("clovervalue") = 11 and length("1") = 1 the value is in bucket 11 and we try to find the value in bucket 1 !<br />
<b>If you use 0 you put all the keys and values in the same bucket !</b><br />
<br />
</div><div> System.out.println("14 hash of 12:" + d1Key.hashCode());<br />
</div><div> System.out.println("15 hash of 13:" + d2.hashCode());<br />
</div><div><br />
</div><div> <br />
</div><div> System.out.println("14 contains");<br />
</div><div> if(m.containsKey(d1Key)) {<br />
</div><div> System.out.println("true");<br />
</div><div> }<br />
</div><div><br />
</div><div> }<br />
</div><div>}<br />
</div><div><br />
</div><div><br />
</div><div><span style="text-decoration: underline;">hashcode in Dog is not overriden</span><br />
</div><div><br />
</div><div>run:<br />
</div><div>1 size:2<br />
</div><div>2 aString : k1<br />
</div><div>3 size:4<br />
</div><div>4 aString : null<br />
</div><div>5 aString : null<br />
</div><div>hash of aCat :12677476<br />
</div><div>6 size:5<br />
</div><div>7 aString : cat1<br />
</div><div>8 size:6<br />
</div><div>9 d1Value : clovervalue<br />
</div><div>10 size:6<br />
</div><div>11 d1Value : clovervalue1<br />
</div><div>12 d1Value() : clovervalue1<br />
</div><div>13 d1Value() : null<br />
</div><div>14 hash of 12:33263331<br />
</div><div>15 hash of 13:6413875<br />
</div><div>14 contains<br />
</div><div>true<br />
</div><div>BUILD SUCCESSFUL (total time: 0 seconds)<br />
</div><div><br />
</div><div><span style="text-decoration: underline;">hashCode is and returns 0</span><br />
</div><div><br />
</div><div>run:<br />
</div><div>1 size:2<br />
</div><div>2 aString : k1<br />
</div><div>accessing method hashcode<br />
</div><div>accessing method hashcode<br />
</div><div>accessing method equals<br />
</div><div>3 size:4<br />
</div><div>accessing method hashcode<br />
</div><div>accessing method equals<br />
</div><div>accessing method equals<br />
</div><div>4 aString : dog1<br />
</div><div>accessing method hashcode<br />
</div><div>accessing method equals<br />
</div><div>5 aString : dog2<br />
</div><div>hash of aCat :11394033<br />
</div><div>6 size:5<br />
</div><div>7 aString : cat1<br />
</div><div>accessing method hashcode<br />
</div><div>accessing method equals<br />
</div><div>accessing method equals<br />
</div><div>8 size:6<br />
</div><div>accessing method hashcode<br />
</div><div>9 d1Value : clovervalue<br />
</div><div>10 size:6<br />
</div><div>accessing method hashcode<br />
</div><div>11 d1Value : clovervalue1<br />
</div><div>accessing method hashcode<br />
</div><div>12 d1Value() : clovervalue1<br />
</div><div>accessing method hashcode<br />
</div><div>accessing method equals<br />
</div><div>13 d1Value() : clovervalue1<br />
</div><div>accessing method hashcode<br />
</div><div>14 hash of 12:0<br />
</div><div>accessing method hashcode<br />
</div><div>15 hash of 13:0<br />
</div><div>14 contains<br />
</div><div>accessing method hashcode<br />
</div><div>true<br />
</div><div><br />
</div><div><span style="text-decoration: underline;">hasCode is and returns name.lenght()</span><br />
</div><div><br />
</div><div>run:<br />
</div><div>1 size:2<br />
</div><div>2 aString : k1<br />
</div><div>accessing method hashcode<br />
</div><div>accessing method hashcode<br />
</div><div>accessing method equals<br />
</div><div>3 size:4<br />
</div><div>accessing method hashcode<br />
</div><div>accessing method equals<br />
</div><div>accessing method equals<br />
</div><div>4 aString : dog1<br />
</div><div>accessing method hashcode<br />
</div><div>accessing method equals<br />
</div><div>5 aString : dog2<br />
</div><div>hash of aCat :11394033<br />
</div><div>6 size:5<br />
</div><div>7 aString : cat1<br />
</div><div>accessing method hashcode<br />
</div><div>8 size:6<br />
</div><div>accessing method hashcode<br />
</div><div>9 d1Value : clovervalue<br />
</div><div>10 size:6<br />
</div><div>accessing method hashcode<br />
</div><div>11 d1Value : clovervalue1<br />
</div><div>accessing method hashcode<br />
</div><div>12 d1Value() : null<br />
</div><div>accessing method hashcode<br />
</div><div>13 d1Value() : null<br />
</div><div>accessing method hashcode<br />
</div><div>14 hash of 12:1<br />
</div><div>accessing method hashcode<br />
</div><div>15 hash of 13:1<br />
</div><div>14 contains<br />
</div><div>accessing method hashcode<br />
</div><div><br />
</div><br />
<div><span style="font-family: Arial; font-size: small;"><span style="font-size: 13px;"></span></span><br />
</div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com1tag:blogger.com,1999:blog-836520676967135831.post-8915536355695578482010-01-16T07:17:00.001-08:002010-01-16T07:31:26.035-08:0018 - Sorted Sets and a strange class - level basic - Java 1.6.0_17 - NB 6.8<span style="color: red;"><b>COULD YOU NOTICE THAT I HAD TO PUT A SINGLE QUOTE IN FRONT OF THE GREATER SIGN OF THE GENERIC. IF I DON'T DO THAT I HAVE A PROBLEM WITH THE EDITOR !</b></span><br />
<br />
This example illustrates the fact that the objects of a collection have to be mutually comparable. If not a ClassCastException is returned by compare. Book page 583.<br />
In this example if the object you put in the TreeSet is the a String, the comparison is made on name of the class...<br />
<br />
package treesets;<br />
<br />
public class StrangeClass implements Comparable <Object'> {<br />
<br />
Object aStrangeObject;<br />
String name;<br />
<br />
StrangeClass(Object o) {<br />
aStrangeObject = o;<br />
if (o instanceof String) {<br />
name = (String) o;<br />
} else {<br />
name = o.getClass().getName();<br />
}<br />
}<br />
<br />
public int compareTo(Object o) {<br />
return name.compareTo(((StrangeClass)o).name);<br />
}<br />
<br />
public String toString() {<br />
if (aStrangeObject instanceof String) {<br />
return (String) aStrangeObject;<br />
}<br />
return name;<br />
}<br />
}<br />
<br />
package treesets;<br />
<br />
import java.util.Set;<br />
import java.util.TreeSet;<br />
<br />
public class TestStrangeClass {<br />
<br />
public static void main(String[] args) {<br />
<br />
boolean[] ba = new boolean[5];<br />
<br />
//Set s = new HashSet();<br />
Set s = new TreeSet<StrangeClass'>();<br />
<br />
ba[0] = s.add(new StrangeClass(new String("c")));<br />
ba[1] = s.add(new StrangeClass(new Integer(42)));<br />
ba[2] = s.add(new StrangeClass(new String("b")));<br />
ba[3] = s.add(new StrangeClass("a"));<br />
ba[4] = s.add(new StrangeClass(new Object()));<br />
<br />
<br />
for (int x = 0; x < ba.length; x++) {<br />
System.out.println(ba[x]);<br />
}<br />
<br />
System.out.println("Display : ");<br />
for (Object o : s) {<br />
System.out.println(o);<br />
}<br />
}<br />
}<br />
<div><br />
<br />
run:<br />
true<br />
true<br />
true<br />
true<br />
true<br />
Display :<br />
<b>a</b><br />
<b>b</b><br />
<b>c</b><br />
<b>java.lang.Integer</b><br />
<b>java.lang.Object</b><br />
<br />
<br />
</div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-90317673274196770832010-01-15T03:08:00.000-08:002010-01-16T04:46:04.105-08:0017 - Arrays.sort example - level basic - Java 1.6.0_17 - NB 6.8<span style="font-family: Arial; font-size: small;"><span style="font-size: 13px;"></span></span><br />
<span style="font-family: Arial; font-size: small;"><span style="font-size: 13px;"><div>public static void main(String[] args) {<br />
</div><div><br />
</div><div> // reverse order<br />
</div><div> <b>char[] array1 = {'f', 'e', 'd', 'c', 'b', 'a'};</b><br />
</div><div><br />
</div><div> for (char cs : array1) {<br />
</div><div> System.out.println(cs);<br />
</div><div> }<br />
</div><div> <b>Arrays.sort(array1);</b><br />
</div><div> for (char cs : array1) {<br />
</div><div> System.out.println(cs);<br />
</div><div> }<br />
</div><div> }<br />
</div><div><br />
</div><div><div><b>run:</b><br />
</div><div><b>f</b><br />
</div><div><b>e</b><br />
</div><div><b>d</b><br />
</div><div><b>c</b><br />
</div><div><b>b</b><br />
</div><div><b>a</b><br />
</div><div><b>a</b><br />
</div><div><b>b</b><br />
</div><div><b>c</b><br />
</div><div><b>d</b><br />
</div><div><b>e</b><br />
</div><div><b>f</b><br />
</div><div><br />
</div></div></span></span>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-70859454613838403812010-01-15T02:56:00.000-08:002010-01-15T02:58:34.649-08:0016 - Comparable and Comparator - level basic - Java 1.6.0_17 - NB 6.8public class DVD <b>implements Comparable</b><dvd> {</dvd><br />
<br />
String title;<br />
String genre;<br />
String leadActor;<br />
<br />
DVD(String t, String g, String a) {<br />
title = t;<br />
genre = g;<br />
leadActor = a;<br />
}<br />
<br />
String getGenre() {<br />
return genre;<br />
}<br />
<br />
@Override<br />
public String toString() {<br />
return title + " " + genre + " " + leadActor;<br />
}<br />
<br />
<b> public int compareTo(DVD o) {</b><br />
<b> //throw new UnsupportedOperationException("Not supported yet.");</b><br />
<b> return title.compareTo(o.title);</b><br />
<b> }</b><br />
}<br />
<div><br />
</div><div><div>import java.util.Comparator;<br />
</div><div><br />
</div><div>public class GenreSort <b>implements Comparator</b><dvd> {</dvd><br />
</div><div><br />
</div><div><b> public int compare(DVD o1, DVD o2) {</b><br />
</div><div><b> //throw new UnsupportedOperationException("Not supported yet.");</b><br />
</div><div><b> return o1.getGenre().compareTo(o2.getGenre());</b><br />
</div><div><b> }</b><br />
</div><div><br />
</div><div>}<br />
</div><div><br />
</div><div><div>import java.util.ArrayList;<br />
</div><div>import java.util.Collections;<br />
</div><div><br />
</div><div>public class TestDVD {<br />
</div><div><br />
</div><div> ArrayList<dvd> dvdList = new ArrayList<dvd>();</dvd></dvd><br />
</div><div><br />
</div><div> void populateList() {<br />
</div><div> dvdList.add(new DVD("title5", "genre4", "actor2"));<br />
</div><div> dvdList.add(new DVD("title4", "genre3", "actor1"));<br />
</div><div> dvdList.add(new DVD("title3", "genre2", "actor4"));<br />
</div><div> dvdList.add(new DVD("title2", "genre1", "actor6"));<br />
</div><div> dvdList.add(new DVD("title1", "genre2", "actor6"));<br />
</div><div> }<br />
</div><div><br />
</div><div> void go() {<br />
</div><div> populateList();<br />
</div><div> System.out.println("dvdlist :" + dvdList);<br />
</div><div><b> Collections.sort(dvdList);</b><br />
</div><div> System.out.println("dvdlist :" + dvdList);<br />
</div><div><b> GenreSort gs = new GenreSort();</b><br />
</div><div><b> Collections.sort(dvdList, gs);</b><br />
</div><div> System.out.println("dvdlist :" + dvdList);<br />
</div><div> }<br />
</div><div><br />
</div><div> public static void main(String[] args) {<br />
</div><div> new TestDVD().go();<br />
</div><div> }<br />
</div><div>}<br />
</div><div><br />
</div><div><div><b>run:</b><br />
</div><div><b>dvdlist :[title5 genre4 actor2, title4 genre3 actor1, title3 genre2 actor4, title2 genre1 actor6, title1 genre2 actor6]</b><br />
</div><div><b>dvdlist :[title1 genre2 actor6, title2 genre1 actor6, title3 genre2 actor4, title4 genre3 actor1, title5 genre4 actor2]</b><br />
</div><div><b>dvdlist :[title2 genre1 actor6, title1 genre2 actor6, title3 genre2 actor4, title4 genre3 actor1, title5 genre4 actor2]</b><br />
</div><div><b><br />
</b><br />
</div><div>A) The class implements Comparable (is thus modified) and the list is sorted on titles.<br />
</div><div>B) A new Comparator is implemented and the class is sorted on genres using the class GenreSort.<br />
</div><div>C) You can avoid to return something from a method if you throw an Exception. Did you know that ?<br />
</div></div></div></div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-30022825267862340982010-01-15T02:00:00.000-08:002010-01-15T02:51:05.937-08:0015 - two ways to compare - level basic - Java 1.6.0_17 - NB 6.8<span style="font-family: Arial; font-size: small;"><span style="font-size: 13px;">A) equals IS NOT OVERRIDDEN</span></span><br />
<div><span style="font-family: Arial; font-size: small;"><span style="font-size: 13px;"><br />
</span></span><br />
</div><div><span style="font-family: Arial; font-size: small;"><span style="font-size: 13px;"></span></span><br />
<span style="font-family: Arial; font-size: small;"><span style="font-size: 13px;"><div>public class Class1 {<br />
</div><div> int value;<br />
</div><div> Class1() {<br />
</div><div><br />
</div><div> }<br />
</div><div> Class1(int value) {<br />
</div><div> this.value = value;<br />
</div><div> }<br />
</div><div>}<br />
</div><div><br />
</div><div><div>public class Main {<br />
</div><div><br />
</div><div> /**<br />
</div><div> * @param args the command line arguments<br />
</div><div> */<br />
</div><div> public static void main(String[] args) {<br />
</div><div><br />
</div><div> System.out.println("ref1Class1 and ref2Class1");<br />
</div><div> Class1 ref1Class1 = new Class1();<br />
</div><div> Class1 ref2Class1 = ref1Class1;<br />
</div><div> if(ref1Class1.equals(ref2Class1)) {<br />
</div><div> System.out.println("Objects are equal");<br />
</div><div> } else {<br />
</div><div> System.out.println("Objects are not equal");<br />
</div><div> }<br />
</div><div> if(ref1Class1 == ref2Class1) {<br />
</div><div> System.out.println("Objects are ==");<br />
</div><div> } else {<br />
</div><div> System.out.println("Objects are not ==");<br />
</div><div> }<br />
</div><div><br />
</div><div> System.out.println("ref3Class1 and ref4Class1");<br />
</div><div> Class1 ref3Class1 = new Class1(5);<br />
</div><div> Class1 ref4Class1 = new Class1(5);<br />
</div><div> if(ref3Class1.equals(ref4Class1)) {<br />
</div><div> System.out.println("Objects are equal");<br />
</div><div> } else {<br />
</div><div> System.out.println("Objects are not equal");<br />
</div><div> }<br />
</div><div> if(ref3Class1 == ref4Class1) {<br />
</div><div> System.out.println("Objects are ==");<br />
</div><div> } else {<br />
</div><div> System.out.println("Objects are not ==");<br />
</div><div> }<br />
</div><div><br />
</div><div> }<br />
</div><div><br />
</div><div>}<br />
</div><div><br />
</div><div><div>run:<br />
</div><div>ref1Class1 and ref2Class1<br />
</div><div>Objects are equal<br />
</div><div>Objects are ==<br />
</div><div>ref3Class1 and ref4Class1<br />
</div><div>Objects are not equal<br />
</div><div>Objects are not ==<br />
</div></div></div></span></span><br />
</div><div><span style="font-family: Arial; font-size: small;"><span style="font-size: 13px;"><br />
</span></span><br />
</div><div><span style="font-family: Arial; font-size: small;"><span style="font-size: 13px;">B) equals IS OVERRIDDEN</span></span><br />
</div><div><span style="font-family: Arial; font-size: small;"><span style="font-size: 13px;"><br />
</span></span><br />
</div><div><span style="font-family: Arial; font-size: small;"><span style="font-size: 13px;"></span></span><br />
<span style="font-family: Arial; font-size: small;"><span style="font-size: 13px;"><div> public boolean equals(Object objectToCompare) {<br />
</div><div> return this.value == ((Class1)objectToCompare).value;<br />
</div><div> }<br />
</div><div><br />
</div><div><div>run:<br />
</div><div>ref1Class1 and ref2Class1<br />
</div><div>Objects are equal<br />
</div><div>Objects are ==<br />
</div><div>ref3Class1 and ref4Class1<br />
</div><div><b>Objects are equal</b><br />
</div><div><b>Objects are not ==</b><br />
</div><div><br />
</div><div>This is Java for Beginners and you should understand the meaning of these example by yourself... :-)<br />
</div></div></span></span><br />
</div><div><span style="font-family: Arial; font-size: small;"><span style="font-size: 13px;"><br />
</span></span><br />
</div><div><br />
</div>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0tag:blogger.com,1999:blog-836520676967135831.post-15466480801544527072010-01-12T07:46:00.000-08:002010-02-01T01:41:14.045-08:0014 - RegExpimport java.util.regex.*;<br />
class Regex2 {<br />
public static void main(String[] args) {<br />
Pattern p = Pattern.compile(args[0]);<br />
Matcher m = p.matcher(args[1]);<br />
boolean b = false;<br />
while(b = m.find()) {<br />
System.out.print(m.start() + m.group());<br />
}<br />
}<br />
}<br />
<br />
1) simple<br />
<br />
<b>F:\SCP\CHAP6>java Regex2 "ab" "abaaaba"</b><br />
0 ab<br />
4 ab<br />
<br />
2)<br />
<br />
<b>F:\SCP\CHAP6>java Regex2 "aba" "ababab"</b><br />
0 aba<br />
<br />
3) meta d : a digit<br />
<br />
<b>F:\SCP\CHAP6>java Regex2 "\d" "a12c3e456f"</b><br />
1 1<br />
2 2<br />
4 3<br />
6 4<br />
7 5<br />
8 6<br />
<br />
4) meta w : a word character (letters, digits or "_")<br />
<br />
<b>F:\SCP\CHAP6>java Regex2 "\w" "a 1 56 _Z"</b><br />
0 a<br />
2 1<br />
4 5<br />
5 6<br />
7 _<br />
8 Z<br />
<br />
5) 1 char !<br />
<br />
<b>F:\SCP\CHAP6>java Regex2 [a-cA-C] cafeBABE</b><br />
0 c<br />
1 a<br />
4 B<br />
5 A<br />
6 B<br />
<br />
6) no +<br />
<br />
<b>F:\SCP\CHAP6>java Regex2 "0[xX][0-9a-fA-F]" "12 0x 0x12 0Xf 0xg"</b><br />
6 0x1<br />
11 0Xf<br />
<br />
7) +<br />
<br />
<b>F:\SCP\CHAP6>java Regex2 "0[xX][0-9a-fA-F]+" "12 0x 0x12 0Xf 0xg"</b><br />
6 0x12<br />
11 0Xf<br />
<br />
8a) a digit !<br />
<br />
<b>F:\SCP\CHAP6>java Regex2 "\d" "1 a12 234b"</b><br />
0 1<br />
3 1<br />
4 2<br />
6 2<br />
7 3<br />
8 4<br />
<br />
8b) a digit and a + !<br />
<br />
<b>F:\SCP\CHAP6>java Regex2 "\d+" "1 a12 234b"</b><br />
0 1<br />
3 12<br />
6 234<br />
<br />
9) parenthesis and +<br />
<br />
<b>F:\SCP\CHAP6>java Regex2 "0[xX]([0-9a-fA-F])+" "12 0x 0x12 0Xf 0xg 0XFAAAAA"</b><br />
6 0x12<br />
11 0Xf<br />
19 0XFAAAAA<br />
<br />
10) give me proj1 followed by something that is NOT a "," zero or several times<br />
<br />
<b>F:\SCP\CHAP6>java Regex2 "proj1[^,]*" proj3.txt,proj1sched.pdf,proj1,proj2,proj1</b><br />
<b>.java</b><br />
10 proj1sched.pdf<br />
25 proj1<br />
37 proj1.java<br />
<br />
11) \s is a white space<br />
<br />
? is zero or 1<br />
+ 1 or more<br />
give me 3 digits followed by zero or 1 "-" or zero or 1 space followed by 4 digits<br />
<br />
<b>F:\SCP\CHAP6>java Regex2 "\d\d\d([-\s])?\d\d\d\d" 1234567</b><br />
0 1234567<br />
<br />
<b>F:\SCP\CHAP6>java Regex2 "\d\d\d([-\s])?\d\d\d\d" "123 4567"</b><br />
0 123 4567<br />
<br />
<b>F:\SCP\CHAP6>java Regex2 "\d\d\d([-\s])?\d\d\d\d" "123-4567"</b><br />
0 123-4567<br />
<br />
12) . dot is any character<br />
<br />
the following can produce<br />
<br />
<b>F:\SCP\CHAP6>java Regex2 "a.c" "ac abc a c"</b><br />
3 abc<br />
7 a c<br />
<br />
13)<br />
greedy<br />
<b><br />
</b><br />
<b>F:\SCP\CHAP6>java Regex2 ".*xx" "yyxxxyxx"</b><br />
0 yyxxxyxx<br />
<br />
reluctant<br />
<b>F:\SCP\CHAP6>java Regex2 ".*?xx" "yyxxxyxx"</b><br />
0 yyxx<br />
4 xyxx<br />
<br />
14) \n \b \t<br />
<br />
15)<br />
<br />
<b>F:\SCP\CHAP6>java Regex2 "\d" a1b2c3d44</b><br />
1 1<br />
3 2<br />
5 3<br />
7 4<br />
8 4<br />
<br />
<b>F:\SCP\CHAP6>java Regex2 "\d+" a1b2c3d44</b><br />
1 1<br />
3 2<br />
5 3<br />
7 44<br />
<br />
<b><span style="color: red;">F:\SCP\CHAP6>java Regex2 "\d*" a1b2c3d44</span></b><br />
<span style="color: red;"><b>0</b></span><br />
<span style="color: red;"><b>1 1</b></span><br />
<span style="color: red;"><b>2</b></span><br />
<span style="color: red;"><b>3 2</b></span><br />
<span style="color: red;"><b>4</b></span><br />
<span style="color: red;"><b>5 3</b></span><br />
<span style="color: red;"><b>6</b></span><br />
<span style="color: red;"><b>7 44</b></span><br />
<span style="color: red;"><b>9</b></span>Houdinihttp://www.blogger.com/profile/01876963146819035171noreply@blogger.com0