• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Wrapper Doubt

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,

Long m = new Long(1);

or

Long m = new Long(1L);

Both the above are compiling....

what difference is there between them other than one taking int literal and the other taking long literal..?

Can anyone please give their suggestions on the above problem.Thank you in advance.

Regards,
Hardik.S.Raja

Will it make any difference to Long object,m..?
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what difference is there between them other than one taking int literal and the other taking long literal..?


You are passing an int literal but is it taking an int literal? Check the java.lang.Long api for more details.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In both the cases 'm' will hold the long value. Both of them work because int is automatically converted to long by jvm.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
long i=1 and long i=1L

These both are the same hence the same applies to the above
 
premanand sreedharan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
long i=1 and long i=1L

These both are the same hence the same applies to the above
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Long constructor takes either long or String. In case you pass int literal
it is automatically casted to long. (where long is required, byte, char, short and int can be substituted, no need of explicit casting).




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


Long can accept byte, short, int or long.
Long m = new Long(1);//here it is taking int 1 as a long. the 1 here is casted from int to long

or

here you are explicitly providing a long 1, though not needed.
Long m = new Long(1L);

Hope alls well

Regards,
Gitesh
 
Can you smell this for me? I think this tiny ad smells like blueberry pie!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic