简体   繁体   English

Java Trie节点布尔值与布尔值

[英]Java trie node Boolean vs boolean

I'm creating trie node class, which contains field with logical type and I don't know what will be better use boolean or Boolean. 我正在创建trie节点类,其中包含具有逻辑类型的字段,我不知道使用boolean还是Boolean会更好。

I know that one instance of Boolean takes 16 Bytes and 4 bytes takes it reference in 32-bit VM. 我知道Boolean的一个实例需要16个字节,而4个字节需要在32位VM中对其进行引用。 But Boolean have constants Boolean.TRUE and Boolean.FALSE, what can be used for space optimization. 但是布尔值具有常量Boolean.TRUE和Boolean.FALSE,可用于空间优化。 So application wiil takes 4*N+32 Bytes, where N is number of nodes, yes? 因此,应用程序需要4 * N + 32字节,其中N是节点数,是吗?

As far, as I know boolean value takes 4 bytes (in array it optimize to 1 byte per element) in 32-bit VM too) 据我所知,在32位VM中,布尔值也占用4个字节(在数组中,每个元素也优化为1个字节)

However I can use code like this 但是我可以使用这样的代码

boolean EOW = Boolean.TRUE;

but how many space it will take and how many time wiil take autboxing/inboxing? 但是需要多少空间以及需要多少时间进行自动装箱?

So what will be better to use to optimize space usage and time? 那么,使用什么来优化空间使用和时间会更好呢? And could you answer me how many space it tales on 64-bit VM? 您能回答我在64位VM上占用多少空间吗?

Just use the primitive type boolean with true and false . 只需将基本类型booleantruefalse Primitive types take less memory, work with better performance and are null-safe. 基本类型占用更少的内存,具有更好的性能,并且是空安全的。

Instead of this: 代替这个:

boolean EOW = Boolean.TRUE;

Just write this: 只需写下:

boolean EOW = true;

There is no reason to use Boolean.TRUE , it will be auto-unboxed to the primitive value true anyway, it's more efficient and less verbose to use the value true directly. 没有理由使用Boolean.TRUE ,无论如何它将自动取消装箱为原始值true ,直接使用true值更高效且更省力。

Only use wrapper classes such as Boolean when you have a good reason to do so - otherwise, use primitive types such as boolean . 仅在有充分理由时才使用包装器类,例如Boolean ;否则,请使用基本类型,例如boolean One reason to use a wrapper is when you need to store values in a collection (such as an ArrayList ) - collection classes can only hold objects and not primitive values, so you'll have to use the wrapper class in that case. 使用包装器的一个原因是,当您需要将值存储在集合(例如ArrayList )中时,集合类只能容纳对象,而不能存储原始值,因此在这种情况下,您必须使用包装器类。

Using Boolean objects is never more efficient than using boolean primitive values. 使用Boolean对象永远比使用boolean元值更有效。

Note that primitive values are just values , not objects. 请注意,原始值只是 ,而不是对象。 If you use the value true ten times in your code, there will not be ten duplicate objects in memory. 如果在代码中使用true值十次,则内存中将没有十个重复的对象。

I expect the exact code, that you pasted, to be optimized to boolean EOW = true; 我希望您粘贴的确切代码可以优化为boolean EOW = true; , so it shouldn't be unboxed at all, but if there's no such an optimization, then you'll haven't any memory affected, because you use the static field, which will be initialized only once. ,因此根本不应该取消装箱,但是如果没有这样的优化,那么您将不会受到任何内存影响,因为您使用的是静态字段,该字段只会被初始化一次。 Nevertheless, you still will lose one operation on imlicit running method booleanValue() , which will just return stored value. 但是,您仍然会丢失对隐式运行方法booleanValue()一项操作,该方法只会返回存储的值。 Actually, I don't have any idea, why you would want to have such an expression. 实际上,我不知道您为什么要有这样的表达。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM