Simple computing (Java entry series (2 ))

Source: Internet
Author: User

First

We have known before that Java is an object-oriented language, so we should have an object-oriented thinking in writing the first program from the very beginning, in this way, we won't go back and think about what object orientation is later.

In Java, the program we write uses *. the java file is saved as * after the code is written *. after the java file is compiled, a corresponding * is generated through the compilation of the Java compiler *. class bytecode file. Java program execution is actually explained by the Java interpreter to execute this *. class file.

In this tutorial, the first chapter does not show how to write "Hello World ". it may be a little difficult for 0-based students, but I believe there will be good results in the future. next let's look at our first program (the http://www.hanbinit.com.cn/405.html mentioned at the end of the previous Article ):

Code
/** Simple calculator (to understand the data type seen in Java) */package com. icer. chapter1;/***** @ author icer */public class Calculator {private int number1 = 1; private int number2 = 3; /*** simple addition ** @ param number1 * @ param number2 * @ return */public int doAdd (int number1, int number2) {int result = number1 + number2; System. out. println (number1 + "+" + number2 + "=" + result); return result ;} /*** simple subtraction ** @ param number1 * @ param number2 * @ return */public int doSub (int number1, int number2) {int result = number1-number2; System. out. println (number1 + "-" + number2 + "=" + result); return result ;} /*** simple multiplication ** @ param number1 * @ param number2 * @ return */public int doMul (int number1, int number2) {int result = number1 * number2; System. out. println (number1 + "*" + number2 + "=" + result); return result ;} /*** simple division ** @ param number1 * @ param number2 * @ return */public int doDiv (int number1, int number2) {int result = number1/number2; System. out. println (number1 + "/" + number2 + "=" + result); return result ;} /*** the main method acts as a class entry * @ param args */public static void main (String [] args) {Calculator cal = new Calculator (); int number1 = cal. getNumber1 (); int number2 = cal. getNumber2 (); System. out. println ("+++ -->" + cal. doAdd (number1, number2); System. out. println ("+++ -->" + cal. doSub (number1, number2); System. out. println ("+++ -->" + cal. doMul (number1, number2); System. out. println ("+++ -->" + cal. doDiv (number1, number2);} // The following four methods are the call interface public int getNumber1 () {return number1;} provided by the member variables of the class ;} public void setNumber1 (int number1) {this. number1 = number1;} public int getNumber2 () {return number2;} public void setNumber2 (int number2) {this. number2 = number2 ;}}

  

Don't be scared by such a long code. After analyzing the code at, there is no difficulty.

Here are several concepts:

1. Java class declaration

 
1 PublicclassCalculator {}

The above code identifies and declares a public class named Calculator;

2. access modifier in Java:

1. public (public): it can be accessed by the class and non-class members.

2. private: It can only be accessed by members of the class, and no member other than the class can access it. (Data is hidden to ensure data security)

3. protected: only the quilt class and the class itself can access the same class in the same package.

3. Notes in Java:

// Single line comment

/*****/Multi-line comment

4. Java package

In the java file header, use the package keyword to define the same package name.

Let's take a look at the Features Shown in the code above:

Then let's talk about the above Code:

At the beginning, we wrote a comment to illustrate the main functions of this class.

Then, the package is used to identify the package path of the class.

Then there is a class. Two member variables and several methods are written in the class ....

How can I explain the content in a specific class.

IT beauty address: http://www.hanbinit.com.cn/418.html

IT dish this article address: http://www.itcaicai.com/thread-1550-1-1.html

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.