My Java Learning notes (11) About boxing, parameter mutable methods, and enumeration types

Source: Internet
Author: User
Tags wrappers

1. All basic types have a class corresponding to them, which is often referred to as wrappers.

2. The object wrapper class is immutable, that is, once the wrapper is constructed, it is not allowed to change the value in which the wrapper is wrapped. Object wrappers are final, so they cannot be defined by subclasses.

3. Suppose you define an integer array list, and the type parameter in angle brackets does not allow the base type, that is, Arraylist<int> is not allowed, and you can use the integer object wrapper class.

array<integer> L = new arraylist<> ();

4. Boxing and unpacking are compiler-approved, not virtual machines. The compiler inserts the necessary method calls when generating bytecode for the class. The virtual machine simply executes these bytecode.

5. In the method of variable number of parameters, ... is part of the Java code, which indicates that this method can receive any number of objects.

public static double Max (double ... values) {  }


6. When comparing the values of two enumerated types, you never need to call equals, and you can use "= =" directly.

7. All enum types are subclasses of the enum class.

8. Each enumeration class has a
The static Values method, which returns an array that contains all the enumerations that are worth.

Color[] values = Color.values ();

The 9.ordinal method returns the position of the enumeration constant in the enum declaration, starting at 0.


Related instance Code

Import java.util.*;p ublic class Test {public static void main (string[] args) {Scanner in = new Scanner (system.in); System.out.println ("Please enter a color: (Red,blue,white,green,yello)"); String i = In.next (). toUpperCase (); Color C = enum.valueof (Color.class, i); SYSTEM.OUT.PRINTLN ("color =" + c); SYSTEM.OUT.PRINTLN ("abbreviation =" + c.getabbreviation ()); if (c = = color.red) System.out.println ("You chose the same color as I liked!") "); color[] G = color.values (); for (Color e:g) {System.out.print (E + "");} System.out.println (); System.out.println (Color.RED.ordinal ());} Enum color{red ("Red"), Green ("green"), BULE ("Blue"), White ("Bai"), Yello ("yellow");p rivate Color (String abbreviation) { this.abbreviation = abbreviation;} Public String getabbreviation () {return abbreviation;} private String abbreviation;}}


result Output



My Java Learning notes (11) About boxing, parameter mutable methods, and enumeration types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.