JAVA- basic data types Detailed -boolean

Boolean

There is a simple Java types represent logical values, called Boolean. Its value can only be true or false these two values. It is all of the following description of the procedures such as the use of Boolean type: 

  

 1 // Demonstrate boolean values. 
 2 class BoolTest { 
 3 public static void main(String args[]) { 
 4 boolean b; 
 5  
 6 b = false; 
 7 System.out.println("b is " + b); 
 8 b = true; 
 9 System.out.println("b is " + b); 
10 // a boolean value can control the if statement 
11 if(b) System.out.println("This is executed."); 
12 b = false; 
13 if(b) System.out.println("This is not executed."); 
14 // outcome of a relational operator is a boolean value 
15 System.out.println("10 > 9 is " + (10 > 9)); 
16 } 
17 } 

Operating results of this procedure are as follows: 

   1 b is false 2 b is true 3 This is executed. 4 10 > 9 is true  

About this program has three interesting things to note. First of all, you have seen, when the method println () output Boolean value, the display is "true" or "false". Second, the value of a Boolean variable itself is enough to control the if statement. No need to write an if statement like this: 

 1 if(b == true) ...  

Third, relational operators (e.g. <) The result is a Boolean value. This is why the display value of the expression 10> 9 is "true". Furthermore, additional expression of sides 10> 9 parentheses because the plus "+" operator than the operator ">" in the higher priority.

JAVA types Boolean logical and bitwise difference between the

From the results, two kinds of calculation results are the same, but the logic operation will be "short" phenomenon, not a bit, and bit off than logic operations more "exclusive or" function.

Short circuit

 1 class br {
 2   static boolean f1() {
 3     return false;
 4   }
 5   static boolean f2() {
 6     return true;
 7   }
 8   static boolean f3() {
 9     return true;
10   }
11 }
12   
13 boolean f_1 = br.f1()&&br.f2()&&br.f3();

The result is false, because when f1 () it is false, the && later without having to perform all know the results, JAVA occur "short" on the back of a little peace operations, performance improved.

 1 boolean f_2 = br.f2()||br.f1()||br.f3(); 

Results true, the same F2 () is true, as the operator no longer behind.
It seems very convenient and efficient, but there are still shortcomings.

 1 boolean f_3 = br.f2()||br.f3()&&br.f1(); 

The result becomes true, correct should be false, which is wrong "short circuit" caused, and would like to get the right answer you need to add in parentheses:

 1 f_3=( br.f2()||br.f3())&&br.f1(); 

It provides no logical XOR function bitwise operations:

 1 boolean f = true^true; 

Results f = false;

 

boolean accounted for a few bytes in the end?

  

Why are you asking this question, we defined eight in Java basic data types, in addition to the number of bytes of memory usage are clearly other seven types, boolean type would not give a specific number of occupied bytes, because the virtual machine does not exist for this type boolean, boolean type will be used in other types of data compiled to represent, how many bytes it actually occupies boolean type? With questions, just a search online, answers varied, according to the summary of the basic https://www.jianshu.com/p/2f663dc820d0 are the following:

1, 1 bit

The reason is a boolean value of true and false of only two kinds of logical value 0 and 1 will be used to represent compiled, which requires only a two numbers (bit) can be stored in memory, a computer storage bit is the smallest unit.

2,1 bytes

Although the reason is compiled 1 and 0 occupies only a space, but the minimum processing unit of the computer data is 1 byte, 1 byte is equal to 8, the actual storage spaces are: the lowest 1 byte Bit storage, fill the other seven with 0, if the value is true, then the stored binary as follows: 0000 0001, if it is false, then the stored binary is: 0000 0000.

3,4 bytes

Sources reason is described in the book "Java Virtual Machine Specification": "Although this definition of boolean data type, but it provides only very limited support for boolean values ​​without any specific byte in the Java virtual machine. code instructions, boolean expressions in the Java language operation, use int data type after compiling Java virtual machine instead and boolean array will be encoded into the Java virtual machine byte array, each element representing boolean element 8. " So that we can draw boolean type is used alone accounted for 4 bytes in the array is a byte.

Obviously the third is more accurate to say, then why should the virtual machine instead of boolean int it? Why not byte or short, so do not save more memory space. Most people will naturally think of this, I also have this question, through access to information that, use int reason is that for the moment the 32-bit processor (CPU), the data processing time is 32 (not here refers to a 32/64 bit system, but rather refers to the hardware level CPU), having the characteristics of efficient access.

Final summary:

According to the description http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html official document:

boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.

Boolean: Boolean data type has only two possible values: true and false. This data type is used to track true / false condition simple tag. This data type to represent this information, but its "size" is not precisely defined.

As can be seen, the type of boolean not given a precise definition, "the Java Virtual Machine Specification" gives four bytes, and defines an array of boolean byte, whether the implementation depends on the specific virtual machine to the specifications, so 1 byte 4 bytes are possible. This is actually a game between operational efficiency and storage space, both are very important.

 

Guess you like

Origin www.cnblogs.com/xiaoluohao/p/11295542.html