Java Basics: Introduction to Java Knowledge

Source: Internet
Author: User
Tags class definition

First, Java basic syntax:

A Java program can be thought of as a collection of a series of objects that work together by invoking each other's methods. The following is a brief introduction to the concepts of classes, objects, methods, and instance variables.

Object : An object is an instance of a class that has state and behavior. For example, a dog is an object whose state is: color, name, variety, behavior: wagging the tail, barking, eating, etc.

class : A class is a template that describes the behavior and state of a class of objects.

method : The method is the behavior, a class can have many methods. Logical operations, data modifications, and all actions are done in a method.

instance variables : Each object has a unique instance variable, and the state of the object is determined by the value of these instance variables.

The following points should be noted when writing Java programs:

1, case sensitive:Java is case-sensitive, which means that the identifier Hello and hello are different.

2. class name : For all classes, the first letter of the class name should be capitalized. If the class name consists of several words, the first letter of each word should be capitalized, such as Myfirstjavaclass.

3. method Name : All method names should start with a lowercase letter. If the method name contains several words, the first letter of each subsequent word is capitalized.

4. source file Name: The source file name must be the same as the class name. When saving the file, you should use the class name to save the filename (remember that Java is case-sensitive), and the suffix of the file name is. java. (a compilation error is caused if the file name and the class name are not the same).

5. Main Method Entry : All Java programs are executed by the public static void main (String []args) method.

Second, Java objects and classes:

  Class can be seen as a template for creating Java objects. Use the following simple class to understand the definition of a class in Java:

 Public class dog{  String breed;   int Age ;  String color;   void Barking () {  }  void  hungry () {  }  void  sleeping (){  }}

A class can contain the following types of variables:

Local Variables : variables defined in a method, construction method, or block of statements are called local variables. Both the Declaration and initialization of variables are in the method, and the variables are automatically destroyed when the method ends.

member variables: member variables are variables that are defined in the class, outside the method body . This variable is instantiated when the object is created. Member variables can be accessed by methods, construction methods, and statement blocks of specific classes within a class.

Class variables : Class variables are also declared in the class, outside the method body , but must be declared as static types .

1. Construction Method:

Each class has a constructor method. If you do not explicitly define a construction method for a class, the Java compiler will provide a default constructor for the class. At least one constructor method is called when an object is created. The name of the constructed method must be the same as the class , and a class can have more than one constructor method. Here is an example of a construction method:

 Public class puppy{    public Puppy () {// constructor method name with the same name as Class     }    public   Puppy (String name) {        //  This constructor has only one parameter: name    }}

2. Create objects:

Objects are created from a class. In Java, use the keyword new to create a new object. Creating an object requires the following three steps:

declaration : Declares an object, including the object name and object type.

instantiation : Use the keyword new to create an object.

Initialize : When you create an object with new, the constructor method is called to initialize the object.

Here is an example of creating an object:

 Public classpuppy{ PublicPuppy (String name) {//This constructor has only one parameter: nameSystem. out. println ("the puppy's name is:"+name); }    Public Static voidMain (String []args) {//The following statement creates a puppy objectPuppy Mypuppy =NewPuppy ("Tommy" ); }}

3. Access instance variables and methods:

 Public classpuppy{intPuppyage;  PublicPuppy (String name) {//This constructor has only one parameter: nameSystem. out. println ("the puppy's name is:"+name); }    Public voidSetage (intAge ) {Puppyage=Age ; }    Public intGetage () {System. out. println ("The age of the puppy is:"+puppyage); returnPuppyage; }    Public Static voidMain (String []args) {/*Creating Objects*/Puppy mypuppy=NewPuppy ("Tommy" ); /*use the method to set age*/Mypuppy.setage (2 ); /*call another method to get the age*/Mypuppy.getage (); /*You can also access member variables as follows*/System. out. println ("Variable Value:"+mypuppy.puppyage); }}

Third, the source file declaration rules:

When you define multiple classes in one source file, and you also have import statements and package statements, pay particular attention to these rules.

    • There can be only one public class in a source file
    • A source file can have multiple non-public classes
    • The name of the source file should be consistent with the class name of the public class. For example: The class name of the public class in the source file is employee, then the source file should be named Employee.java.
    • If a class is defined in a package, then the packages statement should be in the first row of the source file.
    • If the source file contains an import statement, it should be placed between the package statement and the class definition. If there is no package statement, the import statement should be first in the source file.
    • The import statement and the package statement are valid for all classes defined in the source file. In the same source file, you cannot give different package declarations to different classes.

1. Import statement:

In Java, if you give a full qualified name, including the package name, the class name, the Java compiler can easily navigate to the source code or class. The import statement is used to provide a reasonable path so that the compiler can find a class. For example, the following command line will command the compiler to load all classes under the Java_installation/java/io path

Import java.io.*;

2. Java Package:

Packages are primarily used to classify classes and interfaces. When developing Java programs, you might write hundreds or thousands of classes, so it is necessary to categorize classes and interfaces.

四、一个 Java code Example:

 In this example, we create two classes:Employee and employeetest.

1. Note Save the file as Employee.java. The employee class has four member variables: name, age, designation, and salary. The class explicitly declares a constructor method that has only one parameter.

Employee.java File Code:

ImportJava.io.*;  Public classemployee{String name; intAge ;   String designation; Doublesalary; //constructor for Employee class    PublicEmployee (String name) { This. Name =name; }   //set the value of age    Public voidEmpage (intempage) { Age=Empage; }   /*set the value of the designation*/    Public voidempdesignation (String empdesig) {designation=Empdesig; }   /*set the value of the salary*/    Public voidEmpsalary (Doubleempsalary) {Salary=empsalary; }   /*Printing Information*/    Public voidPrintemployee () {System.out.println ("Name:" +name); System.out.println ("Age:" +Age ); System.out.println ("Position:" +designation); System.out.println ("Salary:" +salary); }}

2, the program is started from the main method of execution. In order to run this program, you must include the main method and create an instance object. The following is a employeetest class that instantiates an instance of 2 Employee classes and invokes a method to set the value of a variable. Note Save the file as Employeetest.java.

Employeetest.java File Code:

ImportJava.io.*; Public classemployeetest{ Public Static voidMain (String args[]) {/*create two objects using the constructor*/Employee Empone=NewEmployee ("RUNOOB1"); Employee Emptwo=NewEmployee ("RUNOOB2"); //call the member methods of both objectsEmpone.empage (26); Empone.empdesignation ("Senior Programmer"); Empone.empsalary (1000);       Empone.printemployee (); Emptwo.empage (21st); Emptwo.empdesignation ("Rookie programmer"); Emptwo.empsalary (500);   Emptwo.printemployee (); }}

Java Basics: Introduction to Java Knowledge

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.