Internship Training--java Foundation (1)

Source: Internet
Author: User

Internship Training--java Foundation (1)

1. My first Java program

First of all, good configuration of the JDK environment, Baidu has a lot. Create the file Helloworld.java (the file name is the same as the class name) with the following code:

 Public class HelloWorld {    publicstaticvoid  main (string[] args) {        System.out.println ("Hello World");}    }

Open CMD,CD to the file directory, run Javac Helloworld.java, can pass, and generate file Helloworld.class, and then run Java HelloWorld, resulting in an error, "Unable to load the main class", because when I configure the JDK environment, Classpath content is not preceded by ".;" (Dots and semicolons), plus dots and semicolons, (.; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar) Like this, restart the computer, can run, the result is "HelloWorld".

2, JDK, JRE, JVM three relations

3. Java specification

Basic syntax

The following points should be noted when writing Java programs:

    • case-sensitive : Java is case-sensitive, which means that the identifier Hello is different from hello.
    • 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.
    • 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.
    • 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).
    • Main Method Entry : All Java programs are executed by the public static void main (String []args) method.
Java identifiers

All components of Java require a name. Class names, variable names, and method names are called identifiers.

For the Java identifiers, there are a few things to note:

    • All identifiers should start with a letter (A-Z or a-Z), a dollar symbol ($), or an underscore (_).
    • The first character can be a letter (A-Z or a-Z), a dollar symbol ($), an underscore (_), or any character combination of a number
    • Keyword cannot be used as an identifier
    • Identifiers are case-sensitive
    • Examples of legal identifiers: Age, $salary, _value, __1_value
    • Examples of illegal identifiers: 123abc,-salary

4. Java Objects and classes

Objects in Java

Now let's take a closer look at what an object is. Look around the real world, you will find a lot of objects around, cars, dogs, people and so on. All of these objects have their own state and behavior.

Take a dog for example, its state is: name, variety, color, behavior has: call, WAG tail and run.

Compare real objects with software objects, which are very similar.

Software objects also have states and behaviors. The state of the software object is the attribute, and the behavior is manifested by the method.

In software development, the internal state of the method operation object is changed, and the mutual invocation of the object is done by means of the method.

Classes in Java

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.

A class can have multiple methods, in the example above: Barking (), hungry (), and sleeping () are all methods of the dog class.

Reference: Beginner's tutorial-learning Java

Internship Training--java Foundation (1)

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.