Home  >  Article  >  Java  >  Memo for beginners learning Java (1)

Memo for beginners learning Java (1)

黄舟
黄舟Original
2016-12-20 13:44:251616browse


Although I occasionally read some books in the past, most of them were just scratching the surface and couldn't get into the main seats. I never dared to say that I knew Java. Contact with a new technology is the same as first love, it is the first time, but the difference is that the latter usually starts out very sweet, but ends very painfully, while the former often starts out very painful, but becomes more interesting as time goes by. I can't stop. Now I'm in this very painful stage. I can't even run the simplest Helloworld. I always prompt Excepion in thread "main" java.lan.NoClassDefFoundError. I have to go online to check and search. My memory is lost. It’s not good, so search it out and save it quickly so that you are always ready.

Generally speaking, after installing the JDK, you must follow the steps to configure it before it can be compiled and run correctly
(assuming that the jdk version is 1.4.0)
1. jdk1.4.0-Installed in the root directory of a certain drive letter of your own machine, for example, it can be installed under C:jdk.
***(The c:jdk that appears below is changed to the directory where you installed JDK)***
2. If your running environment is win98, in the root directory of drive C, in the autoexec.bat file, add the following Two statements:
set Path=%PATH%;c:jdk in
set CLASSPATH=.;c:jdklibdt.jar;c:jdklib ools.jar
After saving, restart the machine and complete the installation of jdk1.4 Install.
3. If your running environment is win2000, you need to add two user variables in the "Environment Variables" of the "Advanced" option under "System" in the "Control Panel".
The name of one of the user variables is "path" and the value is ".;d:j2sdk1.4.0_01 in",
The name of the other user variable is "CLASSPATH" and the value is ".;d"j2sdk1.4.0_01libdt .jar;d:j2sdk1.4.0_01lib ools.jar", click "OK". The installation of jdk1.4.0 is completed.

As for the meaning of this, I think it should be to let the Java system compile the words What kind of support is needed when editing code (.java)? If you don’t tell it where to put this thing, it will be stupid?!

I saw Hello world finally displayed on the screen. It’s of great significance. This is the first program I’ve written in the past year! I feel like I’m in another world when I embark on the road of programming again. I can’t find my place anymore. Fortunately, I learned some about C++ and oriented I haven’t forgotten the surface of the object, so after a little trouble and familiarity with the JDK environment, the next thing will be much easier to handle, and I will feel more at ease.

Using the String class to directly define string variables is less annoying than using C Pointers, I feel much better. I am used to Object Pascal. If I go back to count * *, I will really go crazy.

The definition of array seems to be slightly different from that of C and C++. I can’t remember clearly. First, Write it down and talk about it later

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

There is only so much to explain in this part of variables. Even though I am a newbie, I know it. , people who are always obsessed with grammar like Tan Haoqiang are simply idiots: in most cases, beautiful programs do not need unnecessary embellishments at all, they just need to be neat and clear in ideas.
But for the framework of Java programs, I I just want to take a note, a simple java program seems to have a framework like this

class PRogramName
{
public static void main(String[] args)
{
file://The main body of the program
}

public static int othermethod()
{
file://other method
}
}

The entire program is in a large class. The concept of this class should be similar to the unit in pascal. Like pascal, the file name is also the same. It must be the same as the unit name - in this case, the class name. Java has very strict requirements on capitalization. I made several syntax errors because of this.
A Java program consists of one or more or many methods in such a large
In the above code, the meaning of the parameters of the defined method are:

public means that this member function is public and can be called directly by other classes

static means that the main member function is in the ProgramName class It is unique among all objects, and Java will allocate permanent storage space for it

(January 17) Regarding Static, I would like to extend it a little further. Sometimes we create a class and hope that all instances of this class share a variable. That is to say, all objects of this class only have a copy of the instance variable. Then the memory of such a static instance variable cannot be used to create instances of the class. It is allocated at the time, because everyone uses this one and does not need to be reallocated. Therefore, Java allocates permanent storage space for it.
For example:
class Block{
static int number=50
}
After this definition, all instances of the Block class, whether Block1 or Block2, access the same number. This number is called a variable of the class, not an instance Variables. In fact, static variables are also called class variables.

(January 17) Continue to go deeper. Static member functions or static variables defined with Static can be directly called through the name of the class to which they belong. Why is this possible? Because since all objects of this class use this variable, then Of course I don't need to reference it from any of the objects, but just reference it through the class name. Doesn't this make it convenient to implement some global functions and global variables? Put all global functions or global variables Defined in a static class, you can easily access all global variables and global functions directly through this class name when calling.

(January 20) You need to use
to define global variables that all programs must access. public final static

In addition, I encountered a 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 a member variable declared as non-static is an object property, which is only referenced when the object exists, so if we call it in a static method when the object does not create an instance Non-static member methods are naturally illegal, so the compiler will give errors at this time.

Simply put, static methods can be called without creating an object, while non-static methods must have an instance of the object before they can be called. Therefore It is impossible to reference a non-static method in a static method, because which object's non-static method does it refer to? The compiler cannot give an answer, because there is no object, so an error will be reported.

Finally, let's see Looking at the incisive explanation in Think in Java, I think this issue is very, very clear

2.6.3 static keyword
Usually, when we create a class, we will point out the appearance and behavior of the objects of that class. Unless you use new to create an object of that class, you don't actually get anything. Only after new is executed, the data storage space will be formally generated and the corresponding methods can be used.
But in two extraordinary situations, the above method is not useful. One situation is when you only want to use a storage area to hold a specific data - no matter how many objects are created, or even no objects are created at all. Another situation is when we need an extraordinary method that is not associated with any object of this class. In other words, even if the object is not created, a method is needed that can be called. To meet these two requirements, the static keyword can be used. Once something is made static, the data or method is not associated with any object instance of that class. So even though an object of that class has never been created, you can still call a static method, or access some static data. Before that, for non-static data and methods, we had to create an object and use that object to access the data or methods. This is because non-static data and methods must know the specific object they operate on. Of course, before formal use, since static methods do not need to create any objects, they cannot simply call other members without referencing a named object, thereby directly accessing non-static members or methods (because non-static members and methods must be associated with a specific object).

Whoops! Now we should be back to the main thing

void means that the type of the value returned by the method is empty. If it returns a specific type, the method is actually a function, otherwise it is just a process.

I don’t know if these things have lost their teeth due to old age. If you want to smash it, just smash it. And let me ask the experts. Question, why is the compilation speed of jdk so slow?

The above is the content of the memo (1) for beginners learning Java. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn