• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Some Codes

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i didn't get how equals print true
1)
class SS
{

static public void main(String args[])
{
Double a = new Double(Double.NaN);
Double b = new Double(Double.NaN);

if( Double.NaN == Double.NaN )
System.out.println("True");
else
System.out.println("False");

if( a.equals(b) )
System.out.println("True");
else
System.out.println("False");


}
}


2)why the following code didn't get errors

class R
{
void m()
{
}
}
class SS extends R
{
static public void main(String args[])
{
Float b=new Float("12.12");
}
}

the float value is by default Double; even if i give 12.12d the compiler is not complaining. why??




3)whts the o/p
class Test
{
Test(int i)
{
System.out.println("Test(" +i +")");
}
}

public class Q12
{
static Test t1 = new Test(1);

Test t2 = new Test(2);

static Test t3 = new Test(3);

public static void main(String[] args)
{
Q12 Q = new Q12();
}
}




4)

class SS
{



static public void main(String args[])
{
byte d=(byte)12;
d=~d;//Throwiing error why???
System.out.println(d);
}
}


5)
solve

when i am using the byte in the place of char i able to get the full range b4 overflow occurs.but in case of char the chr ? is printed which shows that b is not incrementing why it so?
class SS
{



static public void main(String args[])
{

char b = 1;

while ( ++b > 0 )
System.out.println(b); ;
System.out.println("Welcome to Java");


}
}



6) ans is b how
: public void check()
2: {
3: System.out.println(Math.min(-0.0,+0.0));
4: System.out.println(Math.max(-0.0,+0.0));
5: System.out.println(Math.min(-0.0,+0.0) == Math.max(0.0,+0.0));
6: }

A) prints -0.0, +0.0 and false.
B) prints -0.0, +0.0 and true.
C) prints 0.0, 0.0 and false.
D) prints 0.0, 0.0 and true.


7) atlast can anybody explain me in and outs of transient and volatile variable.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Parameswaran Thangavel:
i didn't get how equals print true
1)
class SS
{

static public void main(String args[])
{
Double a = new Double(Double.NaN);
Double b = new Double(Double.NaN);

if( Double.NaN == Double.NaN )
System.out.println("True");
else
System.out.println("False");

if( a.equals(b) )
System.out.println("True");
else
System.out.println("False");


}
}



The answer is in the Double API for the equals() method:

Note that in most cases, for two instances of class Double, d1 and d2, the value of d1.equals(d2) is true if and only if

d1.doubleValue() == d2.doubleValue()
also has the value true. However, there are two exceptions:

  • If d1 and d2 both represent Double.NaN, then the equals method returns true, even though Double.NaN==Double.NaN has the value false.
  • If d1 represents +0.0 while d2 represents -0.0, or vice versa, the equal test has the value false, even though +0.0==-0.0 has the value true.

  • [ April 29, 2005: Message edited by: Joe Sanowitz ]
     
    Joe Sondow
    Ranch Hand
    Posts: 195
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Parameswaran Thangavel:

    2)why the following code didn't get errors

    class R
    {
    void m()
    {
    }
    }
    class SS extends R
    {
    static public void main(String args[])
    {
    Float b=new Float("12.12");
    }
    }

    the float value is by default Double; even if i give 12.12d the compiler is not complaining. why??



    Again, the answer is in the API. The Float API explains that in Float(String s) "The string is converted to a float value as if by the valueOf method."

    The API for the valueOf method explains that the String can consist of an optional sign followed by "NaN" or "Infinity" or a Floating Point Literal as defined in JLS 3.10.2.

    Examples from the JLS of Floating Point Literals include:
    1e1f
    2.f
    .3f
    0f
    3.14f
    6.022137e+23f
    1e1
    2.
    .3
    0.0
    3.14
    1e-9d
    1e137
     
    Joe Sondow
    Ranch Hand
    Posts: 195
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Parameswaran Thangavel:

    3)whts the o/p
    class Test
    {
    Test(int i)
    {
    System.out.println("Test(" +i +")");
    }
    }

    public class Q12
    {
    static Test t1 = new Test(1);

    Test t2 = new Test(2);

    static Test t3 = new Test(3);

    public static void main(String[] args)
    {
    Q12 Q = new Q12();
    }
    }



    The output is
    Test(1)
    Test(3)
    Test(2)

    The reason is that in order to execute the static main method, the JVM must first create the Class object for Q12, where all the static variables and methods for Q12 reside. In doing so, it must initialize the static variables t1 and t3, which in this case means calling new Test(1) and new Test(2). Afterwards, the JVM can start executing the body of the main method. It will initialize the static variables even if you have an empty main method:



    The output of this program is:
    Test(1)
    Test(3)
     
    Joe Sondow
    Ranch Hand
    Posts: 195
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Parameswaran Thangavel:

    4)

    class SS
    {



    static public void main(String args[])
    {
    byte d=(byte)12;
    d=~d;//Throwiing error why???
    System.out.println(d);
    }
    }


    Because whenever you perform operations on a byte, short, char, or any combination of the three, the result will be an int. That is why
    d=~d
    doesn't compile in your program. The right side evaluates to an int, and you can't assign an int to a byte without an explicit cast. It's also why this doesn't compile:

    Again, the right side evaluates to an int, and you can't assign an int to a short without an explicit cast unless the int is a compile time constant.


    5)
    solve

    when i am using the byte in the place of char i able to get the full range b4 overflow occurs.but in case of char the chr ? is printed which shows that b is not incrementing why it so?
    class SS
    {

    static public void main(String args[])
    {
    char b = 1;

    while ( ++b > 0 )
    System.out.println(b); ;
    System.out.println("Welcome to Java");

    }
    }


    The ? does not mean that b is not incrementing. It means that the console where you're printing b cannot display the character associated with that Unicode value, so ? is printed instead for each unprintable character. On my computer it takes about 9 seconds for this program to cycle through all 65536 possible values of b, but it does finish correctly.

    Try changing b to a short and you will see that it takes a similar amount of time, because short also has 65536 possible values.


    6) ans is b how
    : public void check()
    2: {
    3: System.out.println(Math.min(-0.0,+0.0));
    4: System.out.println(Math.max(-0.0,+0.0));
    5: System.out.println(Math.min(-0.0,+0.0) == Math.max(0.0,+0.0));
    6: }

    A) prints -0.0, +0.0 and false.
    B) prints -0.0, +0.0 and true.
    C) prints 0.0, 0.0 and false.
    D) prints 0.0, 0.0 and true.


    In the strange world of float and double, there is a value called negative zero and a value called positive zero. Positive zero is also just called zero. Mathematically they are equivalent.
    -0.0 == +0.0 is true
    -0.0 >= +0.0 is true
    -0.0 <= +0.0 is true
    -0.0 < +0.0 is false
    -0.0 > +0.0 is false

    However, the min and max methods of the Math class have special rules for -0.0 and +0.0. In the cases of Math.min(float, float), Math.min(double, double), Math.max(float, float) and Math.max(double, double):

    Unlike the the numerical comparison operators, this method considers negative zero to be strictly smaller than positive zero.
    -- API for the Math class

     
    Parameswaran Thangavel
    Ranch Hand
    Posts: 485
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi joe thanx for ur reply still i had some doubt let me put it

    1) i read that wrapper objeect are overridden like String obects is that true..

    2)i didn't get this point. though the valueOf() method are used to convert the string..
    the floating point literal which is 12.12 is by default should be Double or even i gave the double d explicitly.my point here is while converting the string NumberFormat Exception should thrown but didn't why....

    5) i put this in other way why ? is printing always...There should be a value for 256 characters.
     
    Joe Sondow
    Ranch Hand
    Posts: 195
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Parameswaran Thangavel:
    hi joe thanx for ur reply still i had some doubt let me put it

    1) i read that wrapper objeect are overridden like String obects is that true..



    I'm not sure what you mean by that. The only things that get overridden are methods.


    2)i didn't get this point. though the valueOf() method are used to convert the string..
    the floating point literal which is 12.12 is by default should be Double or even i gave the double d explicitly.my point here is while converting the string NumberFormat Exception should thrown but didn't why....



    In this case it doesn't matter what kind of floating point literal you use. The valueOf method in Float and the valueOf method in Double both accept the same kind of String. It will work with or without a D or F suffix. That's how the valueOf method happens to work.


    5) i put this in other way why ? is printing always...There should be a value for 256 characters.



    There are printable values for most of those early characters. If you look carefully when the program starts you might see them flash by in a fraction of a second before the ? values start.
     
    Parameswaran Thangavel
    Ranch Hand
    Posts: 485
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi i meant that whether the wrapper objects are overridden the equals() like the string...

    i.e == check for reference

    equals check for values
     
    Just the other day, I was thinking ... about this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic