My thinking in Java Study Notes (2)

Source: Internet
Author: User
Build the first Java program
Name visibility
Some people don't think the name is a big problem. I started to think it is not a big problem. But when you write a lot of classes one day, you will find that, the name is really important. If you have such a class, he needs to call several other classes. When these classes are the same, the problem arises.
How can we differentiate them and keep their names in conflict? After all, it is impossible to remember the names of each declared class so that they can be distinguished when other names are declared next time. In this case, we need to use a package. A package is a namespace. The namespace uses the following rules: to reverse the Internet domain name. For example, my blog is blog.csdn.com/maoerzuozuoyou can use package maoerzuozuo.com. csdn. Blog in a similar way ~ Each package is a unique namespace. Note: When a package is used to describe a package, the package hierarchy must be the same as that of the file directory! C:/maoerzuozuo/COM/csdn/blog
Use other components
The package must be used after it is defined. How can this problem be used? Use the import keyword at the beginning of the file. If I want to import a file named hello. class component. I only need to import maoerzuozuo.com. csdn. blog. hello; I imported the package I created. Of course, you can also be lazy and use import maoerzuozuo.com. csdn. blog. *;
Import all the packages in this folder (maybe some of your packages cannot be used at all). However, the downsides of this solution are that the compilation speed is slower than directly specifying the package Compilation speed.
Static keyword)
A class can only be used by the outside world when you use new to generate objects. However, in section 2, the above method cannot be used.
1. No matter how many objects are generated or no objects are generated, some specific data storage only has one copy;
2. A function in the class does not want to be bound with an object. I do not want to generate an object, but I also want to use a function in the class;
Generally, if you want to use a non-static member, you must first generate an object and use the object to call the data or function, so you must know which object this function/data belongs to, because static can be used without generating objects, therefore, you cannot directly call a non-static function or variable in a static function.
Solution 1: place the static keywords before the member variables or function definitions to make them static.
Class staticdemo
{
Static int I = 888;
Public static void main (string ARGs [])
{
Staticdemo sd1 = new staticdemo ();
Staticdemo SD2 = new staticdemo ();
System. Out. println (sd1. I); // 888
System. Out. println (sd2. I); // 888
Staticdemo. I ++;
System. Out. println (sd1. I); // 889
System. Out. println (sd2. I); // 889

}
}
Now, even if you generate n staticdemo objects, I only has the original one. No matter whether you are sd1. I or sd2. I, it is actually the same I, all referenced from this staticdemo. so, you only need to change staticdemo. the data stored in I changes the sd1 and sd2 I, because all their I handles point to staticdemo. i, the memory space, is equivalent to a person who works in Company A and works in company B as a part-time employee. If he dies, the two companies will be affected at the same time, the same principle applies to static functions.
Solution 2: To use a function without generating an object, you must add the static
Class staticdemo
{
Public static void show (string S)
{
System. Out. println (s );
}
Public static void main (string ARGs [])
{
Show ("one"); // call method 1
Staticdemo. Show ("two"); // call method 2
New staticdemo (). Show ("three"); // call method 3
}
}
For method 1, it actually hides a key sub-item, that is, this. Its complete form should be this. Show ("one"). The usage of this will be learned later.
For method 2, classname is used. method (), which is a typical form of static function calls. Non-static functions cannot be called using this method!
For method 3, this method is the general method for calling a function. It creates an object and calls a function by an object. This method is also applicable to static and non-static methods. When a member variable is declared as static, its creation method changes greatly, but the static function does not, the biggest use of a static function is to call a function without creating an object, just like the main function we often see.
Your first Java program
By default, the Java program imports the packages under java. Lang into the file you created. We do not need to use import again. The name of a public class must be the same as the file name, and only one public class is allowed in a class. The supplementary program cannot be compiled. If you want to run this class, there must be a main () function in this class. Its fixed format is public static void main (string ARGs []), where, public indicates that this is a function publicly used by the outside world, and it is a static function that does not allow return values. The input main () parameter must be a string object array, even if you have never done so, the string array of argS [] can store cmd parameters.
Compilation and execution
Use javac file name. java is used to compile the Java file and Java classname is used to execute the classname class. Note 2: When you use Java commands to execute a class, the class must have a main function; the second classname is not necessarily the same as the file name. If there is a public class in a Java file, the class name must be the same as the file name. In other cases, you can name it as needed.
Comments and embedded documents
// Comment on a single line/*... */comment on multiple lines. Multi-line comment cannot be nested in multiple lines.
Document Description
The javadoc command can extract documents embedded in program code, generate HTML files, and view them in a browser.
Syntax
I jumped over this part. If you are interested, check it out. It's very easy.
Encoding Style
When writing a class name, it should be written in uppercase. If the name is composed of multiple words, the words are written together, and capitalized their first letter, for example, public class maoerzuozuo.
Summary:
This chapter has finally been KO ~ In fact, this chapter is not difficult. You just need to read the book carefully to understand the essence of tij.
Exercise answers in this Chapter
The exercises in this chapter are not difficult in general, but the only question is that we have used the knowledge we learned later to solve it as a problem.
"Write a program so that it can accept the three arguments passed in by the command line. Therefore, you need to index the string array representing the arguments of the command line"
Public class solve
{
Public static void main (string ARGs [])
{
System. Out. println (ARGs [0]);
System. Out. println (ARGs [1]);
System. Out. println (ARGs [2]);
}
}
This program can accept three arguments and display them. Some may ask: how can we pass in the arguments. When running, you need to add a space behind Java solve and add three arguments separated by spaces, for example, in Java solve aaa bbb ccc, the program will receive the three arguments and display them. When the number of input leads does not match the number of accepted arguments, the arrayindexoutofboundsexception error is returned!
If you have any questions during your learning, contact me in this way.
E-mail molmd@163.net/163.com QQ: 31349283 we can learn together!
Welcome to my blog, http://blog.csdn.com/maoerzuozuo has more learning content!
To be continued ...............
 

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.