Home  >  Article  >  Java  >  Detailed explanation of Java's eight basic types and examples of encapsulated classes

Detailed explanation of Java's eight basic types and examples of encapsulated classes

黄舟
黄舟Original
2017-09-15 10:33:491790browse

The eight basic data types are: int, short, float, double, long, boolean, byte, char; follow the editor of Script Home to learn the eight basic types of Java and the basic type encapsulation class

1. First of all, the eight basic data types are: int, short, float, double, long, boolean, byte, char;

Their encapsulation classes are: Integer, Short, Float, Double , Long, Boolean, Byte, Character.


2. The values ​​in Java are all signed. There are no unsigned numbers. Their value ranges are also fixed and will not change with the Changes due to changes in hardware environment or operating system.

 3. Original data types are passed by value when passing parameters, and encapsulated classes are passed by reference.

 4. In Java language, the default declared decimal is of double type, so type conversion is required when initializing float type variables.

 There are two initialization methods for float type variables: float f = 1.0f or float f =(float) 1.0.

5. "==" and "equal()" methods:

1) The basic type and the basic encapsulated type are compared with the "==" operator. The basic encapsulated type will It will be automatically unboxed and converted into a basic type before comparison, so Integer(0) will be automatically unboxed into an int type before comparison, and it will obviously return true.

2) Compare two Integer types with "==". If the value is between -128 and 127, then return true, otherwise return false. This is related to the buffer object of Integer.valueOf() and is not used here. Elaborate.

3) Compare two basic encapsulated types with equals(). First, equals() will compare the types. If the types are the same, then continue to compare the values. If the values ​​are also the same, return true.

4) The basic encapsulation type calls equals(), but the parameter is a basic type. At this time, automatic boxing will be performed first, the basic type is converted to its encapsulation type, and then the comparison in 3 is performed.


 int i=0;
  Integer j=new Integer(0);
  System.out.println(i==j);
  System.out.println(j.equals(j));
  double d=0.5;
  Double b=new Double(0.5);
  System.out.println(d==b);
  System.out.println(b.equals(d));
  Integer aa=-100;
  Integer bb=-100;
  System.out.println(aa==bb);
  Integer aaa=129;
  Integer bbb=129;
  System.out.println(aaa==bbb);

Program running results:


true
true
true
true
true
false

Summary

The above is the detailed content of Detailed explanation of Java's eight basic types and examples of encapsulated classes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn