Basic Java FAQ

Source: Internet
Author: User
Java-based FAQJava-based FAQ 3. I/O article 18 how do I add startup parameters to java programs, just like dir/p/w? A: Do you still remember publicstaticvoidmain (String [] args? Here, args is your startup parameter. At runtime you enter javapackage1.class1-arg1-arg2, args Java basics FAQ

Basic Java FAQ

III. I/O

18
How can I add a startup parameter to a java program, just like dir/p/w?
A: Do you still remember public static void main (String [] args? Here, args is your startup parameter.
When you enter java package1.class1-arg1-arg2 at runtime, there will be two strings in args, one is arg1 and the other is arg2.

19How do I input an int/double/string from the keyboard?
A: java I/O operations are a little more complex than C ++ operations. The sample code is as follows:

BufferedReader cin = new BufferedReader (new InputStreamReader (System. in ))
;
String s = cin. readLine ();


In this way, you get a string. if you need a number, add:

Int n = Integer. parseInt (s );


Or

Double d = Double. parseDouble (s );



20 How can I output an int/double/string?
A: start writing in the program:

PrintWriter cout = new PrintWriter (System. out );


Write as needed:

Cout. print (n );


Or

Cout. println ("hello ")


And so on.

21 I found that some books directly use System. in and System. out input and output, which is much simpler than you.
A: java uses unicode, which is a double byte. System. in and System. out are single-byte streams. If you want to input and output double-byte text such as Chinese, use the author's practice.

4. keyword

25
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 26 and 30.

26Const 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. It 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.

27Goto cannot be used in java.
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 enhances the break and continue features (compared with C ++.
For example:

Outer:
While (...)
{
Inner:
For (...)
{
... Break inner ;...
... Continue outer ;...
}
}


Like const, goto is a reserved word of java for expansion.

28Can 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.

29I have a new object, but cannot delete it.
A: java has an automatic memory reclaim mechanism, namely, Garbarge Collector. You no longer have to worry about pointer errors.

30I want to know why the main method must be declared as public static?
A: It is declared as public so that this method can be called externally. for details, see Article 37 of object.
Static is used to associate a member variable/method to a class rather than an instance ). You can directly use the static members of this class without creating an object. calling static members of Class B in Class A can be written in B. staticMember. Note that the static member variables of a class are unique and shared by all class objects.

31What is the difference between throw and throws?
A: throws declares 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.

32What 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, the execution process is interrupted, and the code is redirected to interception.
......
}

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

33What is the difference between final and finally?
A: For final information, see 26. Finally is used for exception mechanisms. For more information, see 32.


V. object-oriented

34What is the difference between extends and implements?
A: extends is used to (single) inherit a class, while implements is used to implement an interface ). The interface is introduced to partially provide the multi-inheritance function.
In the interface, you only need to declare the method header and leave the method body to the implemented class. These implemented class instances can be treated as interface instances. Interestingly, interfaces can also be declared as extends (single inheritance) relationships.

35How does java implement multi-inheritance?
A: java does not support explicit multi-inheritance. In explicit multi-inheritance languages such as c ++, the subclass is forced to declare the ancestor's virtual base class constructor, which violates the object-oriented encapsulation principle. Java provides the interface and implements keywords to partially implement multi-inheritance. See Section 34.

36What is abstract?
A: The method declared as abstract does not need to be given as a method body and is left to the subclass for implementation. If an abstract method exists in a class, the class must also be declared as abstract. Classes declared as abstract cannot be instantiated, although they can define constructor for subclass.

37What is the difference between public, protected, and 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 restricted to access by themselves and sub-classes,
Private members are limited to their own access.
Java also provides the fourth default visibility, which is generally called package private. when there is no public, protected, or private modifier, the members are visible within the same package. Class can be modified using public or default.

38What is the difference between Override and Overload?
A: Override refers to the inheritance relationship between the parent class and sub-classes. these methods share the same name and parameter type. Overload refers to the relationship between different methods in the same class (which can be defined in subclass or parent class). These methods have the same name and different parameter types.

39I inherited a method, but now I want to call the method defined in the parent class.
A: You can use super. xxx () to call the parent class method in the subclass.

40What should I do if I want to call the constructor of the parent class in the constructor of the subclass?
A: Call super (...) in the first line of the subclass constructor.

41I have defined several constructor methods in the same class and want to call the other in one constructor.
A: Call this (...) in the first line of the constructor (...).

42What if I didn't define the constructor?
A: automatically obtain a construction method without parameters.

43I failed to call the construction method without parameters.
A: If you define at least one constructor, there will be no automatically provided parameter-free constructor. You need to explicitly define a construction method without parameters.

44How can I define a destructor similar to the destructor in C ++ )?
A: A void finalize () method is provided. This method is called when Garbarge Collector recycles this object. Note that it is difficult to determine when an object will be recycled. The author never felt the need to provide this method.

45How can I convert a parent class object into a subclass object?
A: Forced type conversion. For example

Public void meth (A)
{
B B = (B);
}


If a is not a B instance, ClassCastException is thrown. Therefore, make sure that a is indeed a B instance.

46In fact, I'm not sure whether a is a B instance. can I handle it in different situations?
A: You can use the instanceof operator. For example

If (a instanceof B)
{
B B = (B);
}
Else
{
...
}

47I modified the value of an object in the method, but after exiting the method, I found that the value of this object has not changed!
A: It is very likely that you have assigned a new object to the input parameter. for example, 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 and is decoupled from the external object. If you want a to be used as an output variable, do not write this sentence.
A. setAttr (attr); // modified the value of the new object. the external object is not modified.
}


This also happens to the basic type. For example:

Public void fun2 (int)
{
A = 10; // only applies to this method, and the variables outside will not change.
}



6. java. util

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.