[Java Study Notes], 5.1.5, Study Notes 2015.1.5

Source: Internet
Author: User

[Java Study Notes], 5.1.5, Study Notes 2015.1.5
1. "." In classpath indicates the current path, so remember not to forget "." When configuring classpath. If you do not pay attention to it, the. java file can be compiled but cannot be run when you run the program in cmd. The general classpath configuration is as follows:

.;%JAVA_HOME%/lib/tool.jar;%java_home%/lib/rt.jar

I mentioned this because I made this mistake during subsequent program tests. I forgot "." When configuring classpath, so I always reportedNoClassDefFoundErrorThen I found that I did not add the current path to the classpath of java, and the jvm certainly could not find the class I compiled.

2. About the role of args in the main method in the Java main class: args is also used to pass parameters here, for example. Assume that our demo is Member_and_local.java. The Code is as follows:
Public class Member_and_local {public static void main (String [] args) {// test the main function to pass the parameter System. out. println ("the 1st parameters are" + args [0] + "the 2nd parameters are" + args [1] + "the 3rd parameters are" + args [2]);}}

When we use the cmd command line to run the java programJavac Member_and_local.javaCompile and thenJava Member_and_localIn fact, args provides an interface for passing parameters. As long as there is a corresponding processing in our code, you can add a parameter to run it. In example 1, we executeJava Member_and_local Li Gen 1 232In this way, we can use args [0] in the program to access the string "Li Gen", args [1] to access "1", and args [2] to access "232 ".

Figure 1. args use test

The running result is as above. If no parameter is added during the first running, the program reports an error because: when no parameter is entered, in the program, I call args [0], args [1], and args [2]. Of course, the pointer overflows. After adding the parameter, the output is normal. In addition, we can set different parameter types. For example, if we replace String with int, we can get the int type parameters;
Public static void main( String[] int[] args){}
In summary, args is a method that facilitates programmers to transmit parameters. 3. I don't know how to accurately define the local variables and member variables in java, but my understanding is as follows: it refers to the variable defined in all methods (including the main method). It is valid only during method execution and exists in the stack. It must be initialized before it can be called. After the method ends, will be cleared. A member variable is also called a global variable. It refers to a variable defined in the class and exists in the heap. It is valid throughout the class. As long as the class is not recycled, it will always exist, and will be automatically initialized. Member variables can also be divided into: class variables and instance variables (1) class variables: static variables, which can be called directly by class names or objects, in addition, the same class variables of all objects share the same memory space. (2) instance variable: without static modification, it can only be called through objects, and the same instance variable of all objects shares different memory space. The following is an example: a. to initialize a local variable, the member variable is automatically initialized.
Public class Member_and_local {public static void main (String [] args) {// test the local variable // error: local variables must be initialed String name; int age; boolean sex; System. out. println (name + age + sex );}}

This program will report an error because name, age, and sex are local variables in the main method and will not be compiled when they are not initialized because they are not initialized.

Public class Member_and_local {public static void main (String [] args) {Human ligen = new Human (); ligen. getInfo () ;}} class Human {public static int population; private boolean sex; private String name; private int age; private Human partner; private double height; public void getInfo () {System. out. println (this. population + "" + this. sex + "" + this. name + "" + this. age); System. out. println ("" + this. partner + "" + this. height );}}

We have defined a Human class and defined six different types of member variables. They are not initialized, but we use the getInfo () method to directly access the member variables without errors, but the results are as follows:

0 false null 0 null 0.0
The following table lists the default values of each type:
Type) Int Double String (Object) Human (Object) Boolean
Default Value) 0 0.0 Null Null False
Other types can be queried during use, but here we still develop a good habit of automatically initializing member variables. B. class variables can be accessed by class names or object instances. instance variables can only be accessed by object instances (the same applies to class methods and instance methods ). Mark with the static keyword.
Public class Member_and_local {public static void main (String [] args) {// test class variables and instance variables System. out. println (Human. population); Human ligen = new Human (); System. out. println (ligen. population) ;}} class Human {public static int population; public Human () {this. population + = 1 ;}}

The result is 0 1, indicating that the class variable can be accessed by the class object at the same time.

 

4. The object address is not stored in the reference, but the heap address of the object can be found;

Public class Member_and_local {public static void main (String [] args) {// view what is stored in the Reference Human ligen = new Human (); System. out. println (ligen );}}

Result:

Human@459189e1

This content is not an address, but it should be used to find the address of the ligen object.

 

5. Passing java parameter values and references

The parameters of methods in java may be basic variables such as int, long, and boolean, or common packaging objects such as String and Object. When passing a parameter, the former is a value transfer, that is, copying the original variable to the parameter; the latter is a reference transfer, it is to copy the object reference and pass it to the parameter; this will cause some problems. If the method changes the passed parameters, passing the value will not affect the original variable, however, the original variable is affected when the parameter is passed by reference.

 

 

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.