Why does Java use int i = 1<<4, not int i = 16? [duplicate]












7
















This question already has an answer here:




  • Why use 1<<4 instead of 16?

    3 answers




When I read the Java source code of HashMap.class,



 /** The default initial capacity - MUST be a power of two. **/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16


why does Java use 1<<4, not 16?










share|improve this question









New contributor




Smallnine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by bashrc, John Dvorak, Boann java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    Ask this guy ~ hg.openjdk.java.net/jdk8/jdk8/jdk/rev/2e3cc7f599ca

    – Phil
    yesterday






  • 6





    You may yourself write code with readability in mind. For example, one may want to declare a bunch of time period in second, and write 5025 seconds as 1 * 60 * 60 + 23 * 60 + 45 so that you know it is 1:23:45 once you look at it. These simple expressions should be optimized by compiler anyway so don't worry for performance hit.

    – Ricky Mo
    yesterday













  • @RickyMo since 1 << 4 is a compile-time constant, the compiler must treat it the same way as if you had written 16. The same applies to your 1 * 60 * 60 + 23 * 60 + 45 example. Even at the places within the code where DEFAULT_INITIAL_CAPACITY is used, the compiler must treat it the same way as the literal value 16. Besides not having any performance impact, it implies that you can use descriptive expressions and named constants at places where only compile-time constants are allowed, like annotation values or case labels of switch statements.

    – Holger
    yesterday


















7
















This question already has an answer here:




  • Why use 1<<4 instead of 16?

    3 answers




When I read the Java source code of HashMap.class,



 /** The default initial capacity - MUST be a power of two. **/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16


why does Java use 1<<4, not 16?










share|improve this question









New contributor




Smallnine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by bashrc, John Dvorak, Boann java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    Ask this guy ~ hg.openjdk.java.net/jdk8/jdk8/jdk/rev/2e3cc7f599ca

    – Phil
    yesterday






  • 6





    You may yourself write code with readability in mind. For example, one may want to declare a bunch of time period in second, and write 5025 seconds as 1 * 60 * 60 + 23 * 60 + 45 so that you know it is 1:23:45 once you look at it. These simple expressions should be optimized by compiler anyway so don't worry for performance hit.

    – Ricky Mo
    yesterday













  • @RickyMo since 1 << 4 is a compile-time constant, the compiler must treat it the same way as if you had written 16. The same applies to your 1 * 60 * 60 + 23 * 60 + 45 example. Even at the places within the code where DEFAULT_INITIAL_CAPACITY is used, the compiler must treat it the same way as the literal value 16. Besides not having any performance impact, it implies that you can use descriptive expressions and named constants at places where only compile-time constants are allowed, like annotation values or case labels of switch statements.

    – Holger
    yesterday
















7












7








7


0







This question already has an answer here:




  • Why use 1<<4 instead of 16?

    3 answers




When I read the Java source code of HashMap.class,



 /** The default initial capacity - MUST be a power of two. **/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16


why does Java use 1<<4, not 16?










share|improve this question









New contributor




Smallnine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













This question already has an answer here:




  • Why use 1<<4 instead of 16?

    3 answers




When I read the Java source code of HashMap.class,



 /** The default initial capacity - MUST be a power of two. **/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16


why does Java use 1<<4, not 16?





This question already has an answer here:




  • Why use 1<<4 instead of 16?

    3 answers








java






share|improve this question









New contributor




Smallnine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Smallnine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









Boann

37.3k1290121




37.3k1290121






New contributor




Smallnine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









SmallnineSmallnine

683




683




New contributor




Smallnine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Smallnine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Smallnine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




marked as duplicate by bashrc, John Dvorak, Boann java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by bashrc, John Dvorak, Boann java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2





    Ask this guy ~ hg.openjdk.java.net/jdk8/jdk8/jdk/rev/2e3cc7f599ca

    – Phil
    yesterday






  • 6





    You may yourself write code with readability in mind. For example, one may want to declare a bunch of time period in second, and write 5025 seconds as 1 * 60 * 60 + 23 * 60 + 45 so that you know it is 1:23:45 once you look at it. These simple expressions should be optimized by compiler anyway so don't worry for performance hit.

    – Ricky Mo
    yesterday













  • @RickyMo since 1 << 4 is a compile-time constant, the compiler must treat it the same way as if you had written 16. The same applies to your 1 * 60 * 60 + 23 * 60 + 45 example. Even at the places within the code where DEFAULT_INITIAL_CAPACITY is used, the compiler must treat it the same way as the literal value 16. Besides not having any performance impact, it implies that you can use descriptive expressions and named constants at places where only compile-time constants are allowed, like annotation values or case labels of switch statements.

    – Holger
    yesterday
















  • 2





    Ask this guy ~ hg.openjdk.java.net/jdk8/jdk8/jdk/rev/2e3cc7f599ca

    – Phil
    yesterday






  • 6





    You may yourself write code with readability in mind. For example, one may want to declare a bunch of time period in second, and write 5025 seconds as 1 * 60 * 60 + 23 * 60 + 45 so that you know it is 1:23:45 once you look at it. These simple expressions should be optimized by compiler anyway so don't worry for performance hit.

    – Ricky Mo
    yesterday













  • @RickyMo since 1 << 4 is a compile-time constant, the compiler must treat it the same way as if you had written 16. The same applies to your 1 * 60 * 60 + 23 * 60 + 45 example. Even at the places within the code where DEFAULT_INITIAL_CAPACITY is used, the compiler must treat it the same way as the literal value 16. Besides not having any performance impact, it implies that you can use descriptive expressions and named constants at places where only compile-time constants are allowed, like annotation values or case labels of switch statements.

    – Holger
    yesterday










2




2





Ask this guy ~ hg.openjdk.java.net/jdk8/jdk8/jdk/rev/2e3cc7f599ca

– Phil
yesterday





Ask this guy ~ hg.openjdk.java.net/jdk8/jdk8/jdk/rev/2e3cc7f599ca

– Phil
yesterday




6




6





You may yourself write code with readability in mind. For example, one may want to declare a bunch of time period in second, and write 5025 seconds as 1 * 60 * 60 + 23 * 60 + 45 so that you know it is 1:23:45 once you look at it. These simple expressions should be optimized by compiler anyway so don't worry for performance hit.

– Ricky Mo
yesterday







You may yourself write code with readability in mind. For example, one may want to declare a bunch of time period in second, and write 5025 seconds as 1 * 60 * 60 + 23 * 60 + 45 so that you know it is 1:23:45 once you look at it. These simple expressions should be optimized by compiler anyway so don't worry for performance hit.

– Ricky Mo
yesterday















@RickyMo since 1 << 4 is a compile-time constant, the compiler must treat it the same way as if you had written 16. The same applies to your 1 * 60 * 60 + 23 * 60 + 45 example. Even at the places within the code where DEFAULT_INITIAL_CAPACITY is used, the compiler must treat it the same way as the literal value 16. Besides not having any performance impact, it implies that you can use descriptive expressions and named constants at places where only compile-time constants are allowed, like annotation values or case labels of switch statements.

– Holger
yesterday







@RickyMo since 1 << 4 is a compile-time constant, the compiler must treat it the same way as if you had written 16. The same applies to your 1 * 60 * 60 + 23 * 60 + 45 example. Even at the places within the code where DEFAULT_INITIAL_CAPACITY is used, the compiler must treat it the same way as the literal value 16. Besides not having any performance impact, it implies that you can use descriptive expressions and named constants at places where only compile-time constants are allowed, like annotation values or case labels of switch statements.

– Holger
yesterday














2 Answers
2






active

oldest

votes


















6














It provides more readability and understanding of how you arrived at a certain number to begin with. Consider the below example



final int red = 1;
final int blue = 1 << 1;
final int magenta = red | blue; // 3


Each bit in the above numbers represent a primary color, and from the code you could easily figure out why I chose 3 for magenta. It wouldn't have been easier for the reader if you directly set the value 3 in the declaration.






share|improve this answer

































    13














    Because it's clearly stated in the Java documentation that default initial capacity must be a power of two. If we were to see just any other integer instead of the bitwise operator, that wouldn't illustrate the limitation so well.



    Thus by using a left shift operator, it is letting every developer know that it is there for us to notice a point which one should know, be it while modifying or using the HashMap class.






    share|improve this answer





















    • 2





      plus one, this makes it clear that it's 2 pow 4, power of two being burned inside a HashMap; for example this is how a bucket is chosen - (n - 1) & hash, only possible when power of two buckets.

      – Eugene
      yesterday


















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    It provides more readability and understanding of how you arrived at a certain number to begin with. Consider the below example



    final int red = 1;
    final int blue = 1 << 1;
    final int magenta = red | blue; // 3


    Each bit in the above numbers represent a primary color, and from the code you could easily figure out why I chose 3 for magenta. It wouldn't have been easier for the reader if you directly set the value 3 in the declaration.






    share|improve this answer






























      6














      It provides more readability and understanding of how you arrived at a certain number to begin with. Consider the below example



      final int red = 1;
      final int blue = 1 << 1;
      final int magenta = red | blue; // 3


      Each bit in the above numbers represent a primary color, and from the code you could easily figure out why I chose 3 for magenta. It wouldn't have been easier for the reader if you directly set the value 3 in the declaration.






      share|improve this answer




























        6












        6








        6







        It provides more readability and understanding of how you arrived at a certain number to begin with. Consider the below example



        final int red = 1;
        final int blue = 1 << 1;
        final int magenta = red | blue; // 3


        Each bit in the above numbers represent a primary color, and from the code you could easily figure out why I chose 3 for magenta. It wouldn't have been easier for the reader if you directly set the value 3 in the declaration.






        share|improve this answer















        It provides more readability and understanding of how you arrived at a certain number to begin with. Consider the below example



        final int red = 1;
        final int blue = 1 << 1;
        final int magenta = red | blue; // 3


        Each bit in the above numbers represent a primary color, and from the code you could easily figure out why I chose 3 for magenta. It wouldn't have been easier for the reader if you directly set the value 3 in the declaration.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday









        Peter Mortensen

        13.8k1987113




        13.8k1987113










        answered yesterday









        Samuel RobertSamuel Robert

        3,93852437




        3,93852437

























            13














            Because it's clearly stated in the Java documentation that default initial capacity must be a power of two. If we were to see just any other integer instead of the bitwise operator, that wouldn't illustrate the limitation so well.



            Thus by using a left shift operator, it is letting every developer know that it is there for us to notice a point which one should know, be it while modifying or using the HashMap class.






            share|improve this answer





















            • 2





              plus one, this makes it clear that it's 2 pow 4, power of two being burned inside a HashMap; for example this is how a bucket is chosen - (n - 1) & hash, only possible when power of two buckets.

              – Eugene
              yesterday
















            13














            Because it's clearly stated in the Java documentation that default initial capacity must be a power of two. If we were to see just any other integer instead of the bitwise operator, that wouldn't illustrate the limitation so well.



            Thus by using a left shift operator, it is letting every developer know that it is there for us to notice a point which one should know, be it while modifying or using the HashMap class.






            share|improve this answer





















            • 2





              plus one, this makes it clear that it's 2 pow 4, power of two being burned inside a HashMap; for example this is how a bucket is chosen - (n - 1) & hash, only possible when power of two buckets.

              – Eugene
              yesterday














            13












            13








            13







            Because it's clearly stated in the Java documentation that default initial capacity must be a power of two. If we were to see just any other integer instead of the bitwise operator, that wouldn't illustrate the limitation so well.



            Thus by using a left shift operator, it is letting every developer know that it is there for us to notice a point which one should know, be it while modifying or using the HashMap class.






            share|improve this answer















            Because it's clearly stated in the Java documentation that default initial capacity must be a power of two. If we were to see just any other integer instead of the bitwise operator, that wouldn't illustrate the limitation so well.



            Thus by using a left shift operator, it is letting every developer know that it is there for us to notice a point which one should know, be it while modifying or using the HashMap class.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited yesterday









            Peter Mortensen

            13.8k1987113




            13.8k1987113










            answered yesterday









            Aditya BhardwajAditya Bhardwaj

            3269




            3269








            • 2





              plus one, this makes it clear that it's 2 pow 4, power of two being burned inside a HashMap; for example this is how a bucket is chosen - (n - 1) & hash, only possible when power of two buckets.

              – Eugene
              yesterday














            • 2





              plus one, this makes it clear that it's 2 pow 4, power of two being burned inside a HashMap; for example this is how a bucket is chosen - (n - 1) & hash, only possible when power of two buckets.

              – Eugene
              yesterday








            2




            2





            plus one, this makes it clear that it's 2 pow 4, power of two being burned inside a HashMap; for example this is how a bucket is chosen - (n - 1) & hash, only possible when power of two buckets.

            – Eugene
            yesterday





            plus one, this makes it clear that it's 2 pow 4, power of two being burned inside a HashMap; for example this is how a bucket is chosen - (n - 1) & hash, only possible when power of two buckets.

            – Eugene
            yesterday



            Popular posts from this blog

            How to label and detect the document text images

            Vallis Paradisi

            Tabula Rosettana