This article explains in detail the difference between boolean and Boolean in Java and their application in programs. Boolean is the basic data type of Java, and Boolean is its corresponding encapsulation class. The two are basically the same in assignment and judgment, but in some cases, such as when obtaining values from a collection, only the Boolean type can be used. The article also demonstrates the difference in default values after initialization of boolean and Boolean arrays through code examples, and provides a brief overview of the application of boolean in different programming languages.
The difference between boolean and Boolean is that boolean is a basic data type and Boolean is its encapsulation class. Like other classes, it has attributes and methods and can be new. For example: Boolean flag = new Boolean("true"); // boolean is not possible. Boolean is an instantiation object class of boolean, just like Integer corresponds to int.
Since jdk1.5.0 or above, Boolean is the same as boolean in terms of "assignment" and judgment, that is, you: boolean b1 = true; or Boolean b2 = true; both are fine. A few situations where Boolean can only be used are when obtaining values from lists or hash tables. For example, boolean t = false;Map map = new HashMap();map.put("t", t); then only Boolean can be used to obtain the value t1 = (Boolean) map.get(t); //The previous one can only Use Boolean to cast, boolean cannot be used.
The value of an object created by Boolean defaults to false, and an object created by Boolean is null before it is instantiated.
Boolean[] used = new Boolean[3];
for (Boolean flag : used) {
System.out.print(flag + ” “);
}
//The print result is null null null
boolean[] used1 = new boolean[3];
for (Boolean flag1 : used1) {
System.out.print(flag1 + ” “);
}
//Print result is false false false
Further reading:
boolean data type boolean variables are stored as 8-bit (1 byte) numerical values, but can only be True or False. The value of a boolean variable is displayed as True or False (when using Print), or #TRUE# or #FALSE# (when using Write #). Use the keywords True and False to assign a boolean variable to one of these two states. In Java, boolean values can only be true and false, and cannot be replaced by 0 and 1, and they must be lowercase. boolean operate refers to Boolean operation. In the ansys software, its drop-down menu will prompt the Boolean operation items to be performed. In some programming languages, the keyword of this type is bool, such as C++, and the usage is the same.
The above is about the difference between boolean and Boolean. I hope it will be helpful to everyone.
All in all, understanding the difference between boolean and Boolean is crucial to writing efficient and correct Java code. Choosing the appropriate type depends on the specific application scenario. Only by mastering the characteristics of both can you better develop programs.