Memorandum for beginners in Java _ MySQL

Source: Internet
Author: User
Although I have read some books in the past or before, most of them are irrelevant and cannot attend the forum. I dare not say that I understand Java. It is the first time you get into touch with a new technology, just like your first love, but the difference is that the latter is generally very sweet at the beginning, but the ending is very painful, while the former is often very painful at the beginning, the more it becomes, the more delicious it is. now I am in this very painful stage. although I have read some books in the past or some others, I am not able to attend the forum, but I dare not say that I understand Java. It is the first time you get into touch with a new technology, just like your first love, but the difference is that the latter is generally very sweet at the beginning, but the ending is very painful, while the former is often very painful at the beginning, the more it becomes, the more delicious it is. now I am in this very painful stage. even the simplest Helloworld cannot run, and I always prompt the ion in thread "main" java. lan. noClassDefFoundError. I had to go online and search. the memory is not good, so I found it and saved it quickly.

Generally, after installing JDK, you must follow the configuration steps below to compile and run it correctly (assuming that the jdk Version is 1.4.0 ).

1. install jdk 1.4.0 in the root directory of a drive letter on your machine. for example, you can install jdk in C: \ jdk.

* ** (Change c: \ jdk to your own JDK installation directory )***

2. if your running environment is win98, add the following two statements to the autoexec. bat file in the C root directory:

Set Path = % PATH %; c: \ jdk \ bin
Set CLASSPATH =.; c: \ jdk \ lib \ dt. jar; c: \ jdk \ lib \ tools. jar save and restart the machine to complete jdk1.4 installation.

3. if your running environment is win2000, you need to add two user variables in "environment variables" of "advanced" option under "system" of "control panel.

The name of a user variable is "path" and the value is ".; d: \ j2sdk1.4.0 _ 01 \ bin. the name of another user variable is "CLASSPATH" and the value is ".; d "\ j2sdk1.4.0 _ 01 \ lib \ dt. jar; d: \ j2sdk1.4.0 _ 01 \ lib \ tools. jar, and click OK. Jdk1.4.0 is installed.

As for the significance of doing so, I think the Java system should compile the byte code (. java). If you don't tell where it is stored, it will be silly ?!

It makes a lot of sense for me to see Hello world on the screen. this is the first program I wrote in the last year! Setting foot on the path of the program again is just like the feeling of the world, it's almost impossible to find my position. Fortunately, I learned a little bit about C ++ and object-oriented, so after I became familiar with the JDK environment, the next thing would be much easier, I feel more steadfast.

Using the String class to directly define String variables is much better than the annoying pointer in C. I am used to Object Pascal. if I go back to the number, I am crazy.

The definition of the array is slightly different from that of C and C ++. you can't remember it first.

Int [] number = new int [5]
String [] message = new String [5]

There are so many variables to be explained. Although I am a Cainiao, I also know that people who are always stuck in grammar like Tan Haoqiang are simply idiots: in most cases, beautiful programs do not need to be decorated with unnecessary accessories, the idea is clear.

I 'd like to forget the Java program framework. a simple java program seems to be like this:

Class ProgramName
{
Public static void main (String [] args)
{
File &: // subject of the program
}

Public static int othermethod ()
{
File &: // Other methods
}
}

The whole program is in a large class, and the concept of this class should be similar to the unit in pascal. Like pascal, the file name must be the same as the unit name-here it is the class name-. Java is very strict in case sensitivity. I have made a mistake in the syntax several times because of this.

A Java program is composed of one or more methods in such a category.

In the code above, the parameters for defining methods are as follows:

Public indicates that this member function is public and can be directly called by other classes. static indicates that the main member function is unique among all objects in the ProgramName class, and Java will allocate permanent storage space for it.

For more information about Static, I would like to extend it. Sometimes we create a class and want all instances of this class to share a variable. that is to say, all objects of this class only have one Copy of the instance variable. therefore, the memory of such a static instance variable cannot be allocated when the class instance is created, because it is used by everyone and does not need to be re-allocated. therefore, Java allocates permanent storage space for it.

For example:

Class Block {
Static int number = 50
}

After this definition, all Block class instances, whether Block1 or Block2, access the same number. this number is called a class variable, not an instance variable. in fact, static variables are also called class variables.

Further dive: Static member functions or Static variables defined by Static can be called directly through the class name. why? Since all objects in this class use this variable, I naturally don't need to reference it from any of them, but just use the class name to reference it. isn't this easy to implement some global functions and global variables? Define all global functions or global variables in a static class. you can use this class name to conveniently access all global variables and global functions.

To define global variables to be accessed by all programs, use

Public final static

Another problem that beginners often encounter

Non-static variable mainframe cannot be referenced from a static context

That is, non-static variables cannot be referenced in static methods.

Why?

Because we know that static methods can be used when no instance is created, and member variables declared to be non-static are an object attribute, which is referenced only when the object exists, therefore, if we call the non-static member method in the static method when the object is not created, it is naturally invalid, so the compiler will give errors in this case.

To put it simply, static methods can be called without creating objects. non-static methods can be called only when an object instance is available. therefore, it is impossible to reference a non-static method in a static method, because which object does it reference? The compiler cannot give an answer because there are no objects, so an error is reported.

Finally, let's take a look at the incisive explanation in Think in Java. I Think this question is very clear.

   Static keywords

Generally, when creating a class, we will point out the appearance and behavior of the class object. Unless you use new to create an object of that class, you don't actually get anything. The data storage space will be formally generated and corresponding methods can be used only after new is executed.

However, in two special cases, the above method is not usable. One case is that you only want to use a storage area to save a specific data-no matter how many objects you want to create, or even no object at all. Another scenario is that we need a special method that is not associated with any objects in this class. That is to say, a method that can be called is required even if no object is created. To meet these two requirements, you can use the static keyword. Once something is set to static, data or methods will not be associated with any object instance of that class. So even if you have never created an object of that class, you can still call a static method or access some static data. Before that, for non-static data and methods, we must create an object and use that object to access data or methods. This is because non-static data and methods must know the specific objects they operate on. Of course, before formal use, since the static method does not need to create any objects, they cannot simply call other members without referencing a named object, to directly access non-static members or methods (because non-static members and methods must be associated with a specific object ).

Whoops! Now we should go back to the trunk.

Void indicates that the type of the value returned by the method is null. If a specific type is returned, the method is actually a function. Otherwise, it is a process.

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.