Java Basics faq_php Tutorial

Source: Internet
Author: User
Java Basics FAQ

Java Basics FAQ

Third, I/O Chapter

How do I add a startup parameter to a Java program, just like dir/p/w?
Answer: Remember public static void main (string[] args)? The args here are your startup parameters.
At run time you enter Java Package1.class1-arg1-arg2,args there will be two string, one is Arg1, the other is arg2.

How do I enter a int/double/string from the keyboard?
A: Java I/O operations are a bit more complex than C + +. If you want to enter from the keyboard, the sample code is as follows:

BufferedReader cin = new BufferedReader (new InputStreamReader (system.in))
;
String s = cin.readline ();


So you get a string, if you need a number, plus:

int n = integer.parseint (s);


Or

Double D = double.parsedouble (s);



20 How do I output a int/double/string?
A: When the program starts to write:

PrintWriter cout = new PrintWriter (System.out);


Write when needed:

Cout.print (n);


Or

Cout.println ("Hello")


Wait a minute.

21 I found that some books are directly input and output with system.in and System.out, which is much simpler than yours.
A: Java uses Unicode, which is double-byte. and System.in and System.out are single-byte stream. If you want to input and output double-byte text such as Chinese, please use the author's practice.

Iv. key word Articles

How do I define macros inside Java?
A: Java does not support macros because macro substitution does not guarantee type safety. If you need to define a constant, you can define it as a static final member of a class. See 26 and 30.

There is no const in Java.
Answer: You can use the final keyword. For example, final int m = 9. A variable that is declared final cannot be assigned another value. It can also be used to declare a method or class that cannot be inherited by a method or class that is declared final. Note that const is a reserved word for Java to augment.

It is also not possible to use Goto in Java.
A: Even in a process-oriented language, you don't have to goto at all. Please check that your program flow is reasonable. If you need to jump out of a multi-layered loop, Java enhances the functionality of break and continue (compared to C + +).
For example:

Outer:
while (...)
{
Inner:
For (...)
{
... break inner; ...
..... continue outer; ...
}
}


As with const, GOTO is a reserved word for Java to augment.

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

inI have a new object, but I can't delete it.
A: Java has an automatic memory recovery mechanism, known as Garbarge Collector. You no longer have to worry about pointer errors.

-I want to know why the main method must be declared public static?
A: The declaration is public for this method can be called externally, the details meet to object article 37.
Static is used to associate a member variable/method to a class rather than an instance (instance). You do not need to create an object to directly use the static members of this class, and a static member of Class B in Class A can use the b.staticmember notation. Note that a static member variable of a class is unique and is shared by all of the class objects.

toWhat is the difference between throw and throws?
A: Throws is used to declare which exceptions a method throws. Throw is an action that throws an exception in the method body. If you throw an exception in the method and do not declare it in the method declaration, the compiler will error. Note that the subclass of error and RuntimeException is an exception and no special declaration is required.

+What is an exception?
A: Exceptions are first introduced in the ADA language and are used to dynamically process errors and recover in a program. You can intercept the underlying exception in the method and handle it, or you can throw it to a higher-level module to handle it. You can also throw your own exception to indicate that something is abnormal. The common interception processing code is as follows:

Try
{
...//The following is a code that may have an exception
...//exception thrown, execution process interrupted and turned to intercept code.
......
}

catch (Exception1 E)//If Exception1 is a subclass of Exception2 and should be handled in a special order, it should be in front
{
Intercepted by this section when Exception1 occurs
}
catch (Exception2 e)
{
Intercepted by this section when Exception2 occurs
}
Finally//This is optional
{
Executes this code regardless of whether the exception occurred
}

-What is the difference between final and finally?
Answer: Final please see 26. Finally for the exception mechanism, see 32.


v. Object-oriented articles

theWhat's the difference between extends and implements?
A: Extends is used for (single) inheritance of a class, and implements is used to implement an interface (interface). The introduction of interface is to partially provide multiple inheritance functions.
Only the method header is declared in interface, and the method body is left to the implementation class. Instances of these implementations of class can be treated as instances of interface. Interestingly, the relationship between interface can also be declared as extends (single inheritance).

*How does Java implement multiple inheritance?
Answer: Java does not support explicit multiple inheritance. Because in explicit multiple-inheritance languages such as C + +, subclasses are forced to declare ancestor virtual base class constructors, which is a violation of the object-oriented encapsulation principle. Java provides interface and implements keywords to partially implement multiple inheritance. See 34.

$What is abstract?
A: The method declared as abstract does not need to give the method body, leaving the subclass to implement. And if there is an abstract method in a class, the class must also be declared abstract. A class that is declared abstract cannot be instantiated, although it can define a construction method for use by subclasses.

Panax NotoginsengWhat's the difference between public,protected,private?
A: These keywords are used to declare the visibility of classes and members.
Public members can be accessed by any class,
Protected members are limited to their own and sub-class access,
Private members are limited to their own access.
Java also provides a fourth type of default visibility, commonly called package private, when there are no public,protected,private modifiers, members are visible within the same packet. Classes can be decorated with public or by default.

-What is the difference between override and overload?
A: Override refers to the inheritance of methods between the parent and child classes that have the same name and parameter type. Overload is a relationship between different methods in the same class that can be defined in a subclass or in a parent class, which have the same name and different parameter types.

theI inherited a method, but now I want to invoke the method defined in the parent class.
A: You can call the parent class method in a subclass using Super.xxx ().

+What do I do if I want to invoke the constructor of the parent class in the constructor of the subclass?
A: The first line of the subclass construction method calls Super (...). Can.

AI have defined several construction methods in the same class and want to invoke the other one in a constructor method.
A: This (...) is called on the first line of the construction method.

theWhat happens if I don't define a construction method?
A: Automatically get a parameterless construction method.

+I failed to invoke the parameterless construction method.
A: If you define at least one construction method, there is no way to automatically provide a parameterless constructor. You need to explicitly define a parameterless construction method.

-How do I define a destructor method (destructor) similar to C + +?
Answer: Provide a void Finalize () method. This method is called when the object is reclaimed by Garbarge collector. Note that it is actually difficult to tell when an object will be recycled. The author has never felt the need to provide this method.

$I want to convert a parent class object to a subclass object What do I do?
A: Coercion of type conversions. Such as

public void Meth (a a)
{
b b = (b) A;
}


If a is not actually an instance of B, it throws classcastexception. So make sure that a is really an instance of B.

$In fact, I am not sure if a is an example of B, can you deal with the situation?
Answer: You can use the instanceof operator. For example

if (a instanceof B)
{
b b = (b) A;
}
Else
{
...
}

-I modified the value of an object in the method, but I found the value of the object unchanged after exiting the method!
A: It is possible that you have re-assigned the incoming parameter to a new object, such as the following code will cause this error:

public void Fun1 (a A)//a is a local parameter that points to an external object.
{
A = new A (); A points to a new object, decoupling it from the external object. If you want to have a as an outgoing variable, don't write this sentence.
A.setattr (attr);//Modify the value of the new object, the external object has not been modified.
}


This is also the case with basic types. For example:

public void fun2 (int a)
{
A = 10;//is only used for this method, the outside variable does not change.
}



Six, java.util article

http://www.bkjia.com/PHPjc/508516.html www.bkjia.com true http://www.bkjia.com/PHPjc/508516.html techarticle Java Basics FAQ Java Basics FAQ Three, I/O Chapter 18 How do I add a startup parameter to a Java program, like dir/p/w? Answer: Remember public static void main (string[] args)? The args here are ...

  • 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.