• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Interesting JCP Ques

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ques 1) It is written in JLS that NaN is unordered. So comparing it with another NaN always results in False.
However look at this code:
class t {
public static void main(String args[]){
Double a = new Double(Double.NaN);
Double b = new Double(Double.NaN);
System.out.println(b.equals(b));
}
}
It produces output TRUE. How is this possible . We know that equals compare the contents of Objects meaning here it should check for the Values of a & b, but as they are unordered therefore it should return false.
-------------------------------------------------------------------------------------
import java.applet.*;
2: import java.awt.*;
3:
4: public class Q20 extends Applet
5: {
6: Button okButton = new Button("Ok");
7:
8: public void init()
9: {
10: setLayout(new BorderLayout());
11:
12: add("South", okButton);
13: add("North", okButton);
14: add("East", okButton);
15: add("West", okButton);
16: add("Center", okButon);
17:
18: setSize(300,300);
19: }
20: }

A) Five Buttons with label "Ok" at Top, Bottom, Right, Left and Center of the Applet.
B) Only one Button with label "Ok" at the Top of the Applet.
C) Only one Button with label "Ok" at the Bottom of the applet.
D) Only one Button with label "Ok" at the Center of the Applet.

Ans is D but How
----------------------------------------------------------------------------------------------
Ques 3). import java.applet.*;
import java.awt.*;
public class t extends Applet
{
Button okButton = new Button("Ok");
public void init()
{
setLayout(new GridLayout(2,2));
add( okButton);
add(okButton);
add( okButton);
add( new Button("4"));
add( new Button("5"));
setSize(300,300);}
}

This shows only 3 buttons with captions OK,4,5.Can anyone tell what basically happens when we add a single object reference to a Component as here we have added the same object reference okButton 3 times.What is the concept behind this.
HELP ???/

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul123 Khanna:
Great Questions!
1. JLS 4.24 "..,so a numeric comparison operation...returns false...") but .equals is not a numeric comparison.
2. Container.add( String name, Component) notes that it is
strongly advised to use the 1.1 version add(Component,Object constraints)
3. Container.add(okButton) seems to detect that
the same object is being added multiple times and
quietly only honors the last of the identicals.
Ques 1) It is written in JLS that NaN is unordered. So comparing it with another NaN always results in False.
However look at this code:
class t {
public static void main(String args[]){
Double a = new Double(Double.NaN);
Double b = new Double(Double.NaN);
System.out.println(b.equals(b));
}
}
It produces output TRUE. How is this possible . We know that equals compare the contents of Objects meaning here it should check for the Values of a & b, but as they are unordered therefore it should return false.
[/B]


 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul123 Khanna:

Ques 1) It is written in JLS that NaN is unordered. So comparing it with another NaN always results in False.
However look at this code:
class t {
public static void main(String args[]){
Double a = new Double(Double.NaN);
Double b = new Double(Double.NaN);
System.out.println(b.equals(b));
}
}
It produces output TRUE. How is this possible . We know that equals compare the contents of Objects meaning here it should check for the Values of a & b, but as they are unordered therefore it should return false.


NaN is non-ordinal for comparison BUT equals method on wrapper object with a NaN value compares NaN's correctly.
Q2: okButton is put in a "pool" the first time, the second time
JVM will look for the pool, if there is one there, JVM will regard them the same. So there is ONLY one button appears. Rememmber the borderlayout default is CENTER.
Q3: same reason as Q2, 3 okButton only shows one plus another two
newly created buttons - the total is 3 buttons.
hope this helps you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic