Java Programming Ideas Fourth Edition Chapter two answers to the exercises

Source: Internet
Author: User

Exercise 1: Create a class that contains an int field and a char field, none of which are initialized. Print their values to verify that Java performs the default initialization

 Public classjavathinking {Private Static inti; Private Static CharC;  Public Static voidMain (string[] args) {System.out.println (i);                System.out.println (c); //the default value for char here is ' \u0000 ' to output thisSystem.out.println (c = = ' \u0000 ');//The result is true to prove that the default value of char is ' \u0000 '    }}

Operation Result:

0true Note: In Java, only member variables are assigned values, and there is no default value for local variables. The second line of output is blank because the code of the white space character is a control character that is not visible below 0x20,0x20

Exercise 2: In this example of Hellodate.java, create a "Hello,world" program that simply outputs the sentence. The class you are writing requires only one method (the "main" method). Executed when the program is started). Remember to set it to static and specify a list of parameters-even if the table is not used at all. Compile with Javac, and then run it in Java. If you're using a different development environment than the JDK, learn how to compile and run it in your environment

 Public class javathinking {        publicstaticvoid  main (string[] args) {        System.out.println ("Hello,world");}    }

Exercise 3: Find the code snippet that contains the atypename, rewrite it as a complete program, and then compile, run

class atypename{    public  atypename () {        System.out.println ("full Atypename Code");}    }  Public class javathinking {        publicstaticvoid  main (string[] args) {         New  atypename ();    }}

Exercise 4: Rewrite the dataonly code snippet into a program, and then compile, run

classdataonly{inti; DoubleD; Booleanb;} Public classjavathinking { Public Static voidMain (string[] args) {dataonly data=Newdataonly (); DATA.I= 47; DATA.D= 1.1; DATA.B=false;        System.out.println (DATA.I);        System.out.println (DATA.D);    System.out.println (DATA.B); }}

Exercise 5: Modify the previous exercise to assign the data in the DataOnly to the main () method and print it

Answer reference Exercise Four

Exercise 6: Write a program that contains the code snippet for the storage () method defined in this chapter and call it.

Java Programming Ideas Fourth Edition Chapter two answers to the exercises

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.