The 2nd Lesson of "java Learning Series"--java syntax and object-oriented

Source: Internet
Author: User
Tags class definition php language modifiers volatile

Address of this article

Share an Outline:

1. Java program features

1.1 Basic syntax

1.2 Character creation

1.3 Variables

1.4 Java Array

1.5 Java Enumeration

1.6 java modifier

2. Java object-oriented

2.1 Java Classes and objects

Some points of attention in class 2.2

2.3 Java Number Class

3. Reference Documentation

This article mainly introduces the characteristics of Java program (different from Php) and some object-oriented features.

1. Java program features


"sample code"

1  public classHelloWorld {2     /*the First Java program3 * It will print the string Hello world4      */5      public Static voidmain (String []args) {6System.out.println ("Hello world");//Print Hello world7     }8}


The execution process is as follows (image demo):

C: > Javac helloworld.javac: > Java HelloWorld Hello World

  1) "basic grammar"

A) "case sensitive"

Java classes and functions are case-sensitive, and unlike php, Php's classes and functions are case Insensitive.

B) " source file name "

--"a file can have only one class" 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).

Whether the answer is as Follows:


PHP has no such requirement, because PHP itself is an interpreted language, does not need to compile, and does not need to generate a compiled file similar to The. class file

C) " Main method entry "

--must have the Main method (static method): All Java programs are executed by the public static void main(String []args) method.

--"there are exceptions."

      Java is a lot of knowledge, if it is pure javaapplication (java Application) should have the main () function as a portal, but like JSP or applets are not required main () function

D) "each variable must be defined first, and the type is set"

-this is also a different point from the PHP language

2) "string"

A) "must be quoted in double quotes"

B) the link symbol for the string in "connector" Java is "+", unlike PHP in "." because "." is a link to a variable in a class in Java.


3) "variable"

A) the variable must be of type and defined first

B) "kind"


        Java has two big data types, built-in data types and reference data types

        Built-in data types :


        The Java language provides eight basic types.   Six types of numbers (four integers, two floating-point types), one character type, and one Boolean type.  byteshortintlongfloatDoublebooleanChar 


        Reference Data Type:


    --in java, A variable of a reference type is very similar to a C + + Pointer. A reference type points to an object, and a variable that points to an object is a reference variable. These variables are specified at the time of Declaration as a specific type, such as employee, pubby, and so On. Once a variable is declared, the type cannot be Changed.   --objects, arrays are reference data Types.   --the default value for all reference types is Null.   --A reference variable can be used to refer to any type that is compatible with it.   --example: Site site = new site ("runoob").


C) "constant"

--use the final keyword in Java to modify constants in a way that is similar to variables


  4) "java array"

A) arrays are objects stored on the heap and can hold multiple variables of the same type .

5) "java enumeration"

A) Java 5.0 introduces enumerations, and enumeration limit variables can only be pre-set values. Use enumerations to reduce bugs in your Code.

B) "syntax"

Class Freshjuice {   enum freshjuicesize{SMALL, meduim, LARGE}   freshjuicesize size;}

  6) "java modifier"

-- access modifiers are categorized as follows

The defaultprivatepublicprotected modifier is specified, and is visible to classes and all subclasses within the same package.

-- non-access modifier :

a) "Synchronized modifier":--the method of Synchronized keyword declaration can only be accessed by one thread at a Time.     The Synchronized modifier can be applied to four access Modifiers. -- public synchronized voidshowDetails () {...} B) "Transient modifier":--the Serialized object contains thetransientwhen modifying an instance variable, the Java Virtual machine (JVM) skips that particular variable. --the modifier is included in the statement that defines the variable, which is used to preprocess the data type of the class and Variable. --Example public transient intLimit = 55;//does not persist        public intb//Persistence ofC) "volatile modifier"--volatilea decorated member variable forces the value of the member variable to be reread from shared memory each time it is accessed by the Thread. also, when a member variable changes, the thread is forced to write the value of the change back to the shared memory.   So at any moment, two different threads always see the same value for a member Variable. -avolatileThe object reference may beNULL. 


2. Java object-oriented

"code sample"

The file is named Testjavaclass. java, which corresponds to the class name of the only public class in the File.

The implementation of the function is to instantiate a Dog's object, while setting the Dog's age and getting that age, then Output.

Javac testjavaclass. java

Java Testjavaclass

1 //1. Entrance Test class2  public classTestjavaclass3{//{{{4      public Static voidmain (String []args)5{//{{{6         //Note Point 1: instantiate to develop type Testdog7         //Note that the string in the 2:java program must be double-quoted .8Testdog Testdog =NewTestdog ("Tom");9 ();Ten}//}}} one  a}//}}} -  - //2. Test Puppy class the classTestdog -{//{{{ - String name; -     intage ; +     //constructor Function -      publicTestdog (String Name) +{//{{{ aSystem.out.println ("this Dog is called" +name); at}//}}} -  -     //Run -      public  voidRun () -{//{{{ -System.out.println ("01 run \ n");
       // Note Point 3: the internal transfer function of the class, write directly setage (dogage) inSetage (10); - intDogage =getage (); toSystem.out.println ("02 Dog's Age is" +dogage); +}//}}} - the //Get * public intgetage () ${//{{{Panax Notoginseng returnage ; -}//}}} the + //Set a public voidSetage (intAgevalue) the{//{{{
      /// Note 4: member variable of Class's internal transfer class, write age directly
+Age =agevalue; -}//}}} $ $}//}}}

  1) "classes and objects for java"

A) "calling functions and variables"

Class inside the call function, directly is the function name itself, directly write Setage (dogage), the variable is also directly write age

B) "modification of the class"

The class in PHP is decorated in the abstract class at most, but there must be a public class in a file in java, and the name of the File.

C) "define The function of the class"

--common in PHP is the public function functionname ()

--in java, You do not need a description of fuction, but to perform a return value type, public void functionname ()

D) "construction method"

        The constructor method in--php is public function __construct () {}, and there can be only one

--java can have multiple constructs, and 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 construction method to implement different construction methods under different Circumstances.

For example:


public class A{public   A () {      System.out.println ("a parameterless constructor called");   }   Public a (String mess) {      System.out.println ("called a argument constructor \ n" +         "parameter content is:" +mess);}   }

2) "some attention points of the class"

A) "import statement"

--"location" 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 "scope" 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

3) "Java Number class"

A) "cause"

In the actual development process, we often encounter situations where objects need to be used rather than built-in data types. To solve this problem, the Java language provides the corresponding wrapper class for each built-in data type

B) "packing class"

All wrapper classes (Integer, Long, Byte, Double, Float, short) are subclasses of the abstract class Number.

C) "number Subclass method"

Xxxvalue (), CompareTo (), etc.


The 2nd Lesson of "java Learning Series"--java syntax and object-oriented

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: 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.