Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Queries--

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)
class Ex4{
1.public static void main(String[] args){

2.String s = "-";
3.Integer x= 343;
4.long L343 = 343L;
5.if(x.equals(L343)) s+=".e1";
6.if(x.equals(343)) s+= ".e2";
7.Short s1 = (short)((new Short((short)343))/(new Short((short)49)));

8.if(s1==7)
9.s+="=s";

10.if(s1<new Integer(7+1)) s+="fly ";
11.System.out.println(s);
12.}
13.}


Wrf to Example4, chapter 6, its bit confusing to understand type conversions ..
1)how can we compare a Integer object with Long object resulted in line no.6
2)On line no 8, why do we need to convert to short, in place of that can we use �Short�.
3)On line 11, how does comparison happens? Specially its comparison of 2 different wrappers.


2) is-a relationship always rely on polymorphism? Can anyone pls explain this statement

3) What does exactly happen when a code between try and finally (there isn�t any catch block) throws an Exception?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In x.equals(L343), the primitive long (L343) is boxed to a Long wrapper for comparison. However, if you check the API for Integer's equals method, you will see that the argument must be an Integer in order for the method to return true. So this returns false, even though the boxed values are the same.

In Short s1 = (short)((new Short((short)343))/(new Short((short)49))), the primitive shorts ((short)343 and (short)49) are first used to create new Short instances. But then these are unboxed and widened to type int for the arithmetic operation of division, which results in an int. That int is then cast back to type short to be assigned to a variable of type Short.

In s1 < new Integer(7 + 1), the Short (s1) and the new Integer are both unboxed for the numeric comparison. (Additionally, the unboxed short is widened to type int.)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shaily Sharma, please quote your sources.
 
reply
    Bookmark Topic Watch Topic
  • New Topic