Stack overflow's good question-Java Chapter

Source: Internet
Author: User

Questions & Answers

1. How do I exchange two fields in a string?

Q: Question description: How to swap foo and bar locations?

String word1 = "Bar"= "foo"= "Once Upon a time, there was a Foo and a bar."

Workaround:

1). How to use intermediate variables.

Story = Story.replace ("foo", "lala"= Story.replace ("Bar", "foo"= Story.replace ("Lala", "Bar");

Cons: A string that needs to be guaranteed as an intermediate variable does not appear; it would have been a one-time exchange, but this method performed three operations!

2). Using the StringUtils tool class method

New New string[]{"Bar", "foo"})

2. In a Java class, only variables are defined, and no value is assigned, will it consume memory?

A: Will. Even if there is no assignment, there will be a default value. for example int i; and int i=0; the effect is the same.

For local variables defined inside the method, no definitive answer is given. Someone has given this code:

 Public Static void Main (string[] args) {    int i;  // ignored    int j = 5;     = "abc";     // ignored}

Binary code:

  localvariabletable:        Start  Length  Slot  Name   Signature            0       6     0  args   [ljava/lang/String;             2       4     2     J   I            5       1     3     s   ljava/lang/String;   }

The JVM ignores i,snull, so some people think that local variables that are not assigned within the method do not consume memory;

However, it is also suggested that the slot only lists the 0,2,3 location, not the slot 1, and this location is reserved for i. So, he thinks that consumes memory.

3. What is wrong with calling an override method in the constructor?

First make it clear that this method of invocation does not necessarily give an error, and some of the requirements of the IDE are warning.

 Public classConstructormethod {Abstract Static classbase{Base () {info (); System.out.println ("Base class constructor running"); }        Abstract voidinfo (); }    Static classSonextendsbase{ Public voidinfo () {System.out.println ("Son class method Running"); } Son () {Super(); System.out.println ("Son class constructor running"); }    }     Public Static voidMain (string[] args) {Son s=NewSon (); /*Son class method running * Base class constructor running * Son class constructor running * Son category method is called before the son object is created.*/    }}

The above example, can be compiled, run, but in some cases, the constructor has a writable method, may throw an exception:

 Public classCONSTRUCTORMETHOD2 {Abstract Static classa{PrivateA; A () {add (a);//comment out this line to eliminate NULL pointer exceptions        }        Abstract voidAdd (a a); }    Static classBextendsa{List<A> blist=NewArraylist<a>(); Privateb b; B () {}voidAdd (a a) {Blist.add (b); }    }     Public Static voidMain (string[] args) {b b=NewB (); }}

In the above example, a null pointer exception was thrown: Java.lang.NullPointerException.

In short, the reason why you should try not to invoke the Overwrite method in the constructor is: We don't know how the subclass will go about implementing these methods!

From:

1.How can I replace the strings in a-a-does not-end up replacing the other?

2.Do unused local variables in a method acquire memory in JVM?

3.What ' s wrong with Overridable method calls in constructors?

Stack overflow's good question-Java Chapter

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.