Java FAQ (4)

Source: Internet
Author: User

4. Keyword

Q4.1 how to define Macros in Java?
A: Java does not support macros, because the replacement of macros cannot guarantee type security. To define a constant, you can define it as a static final member of a class. See q4.2 and q4.6.

Q4.2 const cannot be used in Java.
A: You can use the final keyword. For example, final int M = 9. Variables declared as final cannot be assigned another value. The only exception is blank final, as shown in the following example:
Public class myclass1 {
Private Final int A = 3;
Private Final int B; // blank final

Public myclass1 (){
A = 5; // invalid. The final variable cannot be assigned another value.
B = 4; // valid. This is the first time B is assigned a value.
B = 6; // invalid. B cannot be assigned again.
}
}
Final can also be used to declare methods or classes. Methods or classes declared as final cannot be inherited.
Note that const is a reserved word of Java for expansion.

Q4.3 Java cannot use goto either.
A: You do not need to use goto in process-oriented languages. Check whether your program process is reasonable. If you need to quickly jump out of multi-tier loops, Java has enhanced (compared with C ++) Break and continue functions and supports label.
For example:
Outer:
While (...)
{
Inner:
For (...)
{
... Break inner ;...
... Continue outer ;...
}
}
Like const, Goto is a reserved word of Java for expansion.

Q4.4 can I overload operators in Java?
A: No. The + sign of string is the only built-in overload operator. You can implement similar functions by defining interfaces and methods.

Q4.5 I have a new object, but cannot delete it.
A: Java has an automatic memory recycle mechanism, namely, garbarge collection. You do not need to delete objects. You no longer have to worry about pointer errors and memory overflow.

Q4.6 I want to know why the main method must be declared as public static? Why cannot I call non-static members in the main method?

A: The public method is declared to allow external calls to this method. For details, see q5.4.
Static is used to associate a member variable/method to a class rather than an instance ). You can directly use the static member of this class without creating an object. Therefore, you cannot call non-static members in the static member because the latter is associated with the object instance. You can use B. staticmember to call static members of Class B in Class. Note that the static member variables of a class are unique and shared by all the class objects.
Especially careful. Class static members are initialized when the class is first loaded by JRE. You can use the following method to use non-static members:
Public Class
{
Private void somemethod () // non-static member
{}
Public static void main (string ARGs)
{
A A = new A (); // create an object instance
A. somemethod (); // now you can use a non-static method.
}
}

What is the difference between q4.7 throw and throws?

A: throws is used to declare the exceptions thrown by a method. Throw actually throws an exception in the method body. If you throw an exception in the method but do not declare it in the method declaration, the compiler will report an error. Note that the subclasses of error and runtimeexception are exceptions and do not need to be declared.

Q4.8 what is an exception?

A: exceptions were first introduced in the Ada Language for dynamic error handling and recovery in the program. You can intercept and handle underlying exceptions in the methods, or send the exceptions to higher-level modules for processing. You can also throw your own exception to indicate that some exceptions have occurred. The common interception processing code is as follows:
Try
{
The following code may cause exceptions:
... // The exception is thrown by you or the lower-layer API. the execution process is interrupted and the code is blocked.
......
}
Catch (exception1 e) // If prediction1 is a subclass of prediction2 and requires special processing, it should be placed first.
{
// When prediction1 occurs, it is intercepted by this segment.
}
Catch (exception2 E)
{
// When prediction2 occurs, it is intercepted by this segment.
}
Finally // This is optional
{
// This code segment is executed no matter whether an exception occurs or not.
// Even if a new exception is thrown out in the catch segment, the finally segment will be executed.
}

 

From: http://www.cndw.com/tech/page/200602154468.asp

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.