• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

operator

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can any one explain this program this is from k & B book form chapter operators self test

class Comp2
{
public static void main(String[] args)
{
float f 1 = 2.3 f;
float [] [] f2 = {{42.0f} , {1.7f,2.3 f},{2.6f,2.7f}};
float [] f3 ={2.7 f};
Long x = 42 L;
// insert code here
System.out.println("true");
}
}

And the following five code fragments :
F1. if(f1==f2)
F2. if(f1==f2[2][1])
F3. if(x==f2[0][0])
F4. if(f1==f2[1,1])
F5 if(f3==f2[2])

o/p
Threeof them will compile , only one will be true

thanks in advance
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Guys,

I am having this same issue with this question. Can someone please explain why
F3. if(x==f2[0][0]) compiles and returns true

Also what happens first, boxing and then widening or what? I am confused as to how
Long can be compared to float?

Thanks.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And the following five code fragments:
F1. if(f1 == f2) // not compile, f1 is float while f2 is object
F2. if(f1 == f2[2][1]) // compile but not true, f1=2.3 while f2[2][1]=2.7
F3. if(x == f2[0][0]) // compile and true, 42L==42.0f results in true
F4. if(f1 == f2[1,1]) // not compile, wrong syntax (comma not allowed on array index)
F5. if(f3 == f2[2]) // compile but not true, f3={2.7f} while f2[2]={2.6f, 2.7f}

Meghna, the == operator can compare numeric values (unboxing the Long to long and then comparing to a float).

Greetings.
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK Thanks for that David!
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic