Why does Java use int i = 1<<4, not int i = 16? [duplicate]
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?
java
New contributor
marked as duplicate by bashrc, John Dvorak, Boann
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.
add a comment |
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?
java
New contributor
marked as duplicate by bashrc, John Dvorak, Boann
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 write5025
seconds as1 * 60 * 60 + 23 * 60 + 45
so that you know it is1: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 since1 << 4
is a compile-time constant, the compiler must treat it the same way as if you had written16
. The same applies to your1 * 60 * 60 + 23 * 60 + 45
example. Even at the places within the code whereDEFAULT_INITIAL_CAPACITY
is used, the compiler must treat it the same way as the literal value16
. 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 orcase
labels ofswitch
statements.
– Holger
yesterday
add a comment |
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?
java
New contributor
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
java
New contributor
New contributor
edited yesterday
Boann
37.3k1290121
37.3k1290121
New contributor
asked yesterday
SmallnineSmallnine
683
683
New contributor
New contributor
marked as duplicate by bashrc, John Dvorak, Boann
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
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 write5025
seconds as1 * 60 * 60 + 23 * 60 + 45
so that you know it is1: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 since1 << 4
is a compile-time constant, the compiler must treat it the same way as if you had written16
. The same applies to your1 * 60 * 60 + 23 * 60 + 45
example. Even at the places within the code whereDEFAULT_INITIAL_CAPACITY
is used, the compiler must treat it the same way as the literal value16
. 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 orcase
labels ofswitch
statements.
– Holger
yesterday
add a comment |
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 write5025
seconds as1 * 60 * 60 + 23 * 60 + 45
so that you know it is1: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 since1 << 4
is a compile-time constant, the compiler must treat it the same way as if you had written16
. The same applies to your1 * 60 * 60 + 23 * 60 + 45
example. Even at the places within the code whereDEFAULT_INITIAL_CAPACITY
is used, the compiler must treat it the same way as the literal value16
. 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 orcase
labels ofswitch
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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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.
2
plus one, this makes it clear that it's2 pow 4
, power of two being burned inside aHashMap
; for example this is how a bucket is chosen -(n - 1) & hash
, only possible when power of two buckets.
– Eugene
yesterday
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
edited yesterday
Peter Mortensen
13.8k1987113
13.8k1987113
answered yesterday
Samuel RobertSamuel Robert
3,93852437
3,93852437
add a comment |
add a comment |
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.
2
plus one, this makes it clear that it's2 pow 4
, power of two being burned inside aHashMap
; for example this is how a bucket is chosen -(n - 1) & hash
, only possible when power of two buckets.
– Eugene
yesterday
add a comment |
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.
2
plus one, this makes it clear that it's2 pow 4
, power of two being burned inside aHashMap
; for example this is how a bucket is chosen -(n - 1) & hash
, only possible when power of two buckets.
– Eugene
yesterday
add a comment |
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.
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.
edited yesterday
Peter Mortensen
13.8k1987113
13.8k1987113
answered yesterday
Aditya BhardwajAditya Bhardwaj
3269
3269
2
plus one, this makes it clear that it's2 pow 4
, power of two being burned inside aHashMap
; for example this is how a bucket is chosen -(n - 1) & hash
, only possible when power of two buckets.
– Eugene
yesterday
add a comment |
2
plus one, this makes it clear that it's2 pow 4
, power of two being burned inside aHashMap
; 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
add a comment |
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 as1 * 60 * 60 + 23 * 60 + 45
so that you know it is1: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 written16
. The same applies to your1 * 60 * 60 + 23 * 60 + 45
example. Even at the places within the code whereDEFAULT_INITIAL_CAPACITY
is used, the compiler must treat it the same way as the literal value16
. 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 orcase
labels ofswitch
statements.– Holger
yesterday