• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

2 Question from newbie!

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)When you declare a long variable without the (L or l) does it default to int?
long l = 255L; //OK!
long l = 255; //does it default to int?
2)When a constructor is private in class1 can you still subclass class1 with class 2 and use the constructor in class2 to access members in class1 or will there be an error?

Thanks in Advance!
Edmund
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer to your first question is that it defaults to an int and then gets automatically converted to a long.
 
henry wu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> 2)When a constructor is private in class1 can you still subclass class1 with class 2 and use the constructor in class2 to access members in class1 or will there be an error?
If the constructor in class1 is private, then the subclass class2 will not be able to call it.
A private method may only be used by an instance of the same class.
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>1)When you declare a long variable without the (L or l) does it default to int ?
Yes. In your example

when Java encounters 255, it treats it as an int. Since the long it is putting 255 into is wider than an int, there's no problem.
For this next exercise, keep in mind that Integer.MAX_VALUE = 2147483647:

This code will not compile! Since 1234567891011 has no suffix, Java tries to treat it like an int, but then it can't fit that behemoth into int's thirty-two bits, and the compiler complains:

To remedy this problem, you need to say

Good luck, Edmund.
Art
 
Edmund Chiang
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick reply!
Best Regards
Edmund
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic