Java instance-assigning departments to new employees

Source: Internet
Author: User

Technical key: 1, the use of switch multi-branch statement, the statement only supports the determination of constants, 2, the hash code of the string to judge, that is, the String class Hashcode () method return value as the switch syntax expression, The case keyword follows a hash code integer value for various string constants. Knowledge Points:Hash Codein Java, a hash code represents the characteristics of an object. Can be based on their own needs, the use of different algorithms produced by an int type number. algorithms for several commonly used hash codes:1, the object class Hashcode. Returns the memory address of the object after the processing of the structure, because each object's memory address is different, so the hash code is not the same. 2. The hashcode of the string class. Based on the contents of the string contained in the strings class, the hash code is returned according to a special algorithm, as long as the string contains the same heap space, and the hash code returned is the same. 3, the integer class, the returned hash code is the integer object contains the value of the whole number.
Implementation process:
1  Packagetest;2 ImportJava.util.Scanner;3  Public classExample {4      Public Static voidMain (string[] args) {5Scanner scan =NewScanner (system.in);6System.out.println ("Please enter the name of the new employee:");7String name = Scan.nextline ();//Receive Employee name8System.out.println ("Please enter the programming language for new hires:");9String language = Scan.nextline ();//receive a programming language for employee recruitmentTen          One         //determine the Department of employee assignment according to the programming language A         Switch(Language.hashcode ()) { -              Case3254818://hash code for Java -              Case2301506://hash code for Java the              Case2269730://hash code for Java -SYSTEM.OUT.PRINTLN ("Employee" +name+ "is assigned to the Java Program Development Department. "); -                  Break; -              Case3104://hash code for C # +              Case2112://hash code for C # -SYSTEM.OUT.PRINTLN ("Employee" +name+ "is assigned to the C # Project maintenance group. "); +                  Break; A              Case-709190099://hash code for ASP. at              Case955463181://hash code for ASP. -              Case9745901://ASP. NET's hash code -SYSTEM.OUT.PRINTLN ("Employee" +name+ "is assigned to the ASP. "); -                  Break; -             default: -System.out.println ("The company does not need the" + language + "language of the program developers.) "); in         } -     } to}
In accordance with the code in the book to type, I can not understand the beginning of the switch statement where the value of the case, Baidu only understand that the original is to set up a variety of languages, such as Java Java, Java, Java Three kinds of writing, The value of obtaining the hash code in advance by the Hashcode method is commented out, and the process is not given in the book. The commented out code is as follows:
System.out.println ("Java". Hashcode ()); System.out.println ("Java". Hashcode ()); System.out.println ("JAVA". Hashcode ()); System.out.println ("C #". Hashcode ()); System.out.println ("C #". Hashcode ()); System.out.println ("ASP. Hashcode ()"); System.out.println ("ASP. Hashcode ()"); System.out.println ("ASP. Hashcode ()");
By running, we can get the hash code corresponding to different languages and put it into case for subsequent judgment. The extended practice in this example is to assign interest groups to students, and we can find out the hash codes of different interest groups, such as football, athletics, literature, etc., and then put them in the switch statement for matching judgment, based on the example of Employee Assignment department.

Java instance-assigning departments to new employees

Related Article

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.