Basic knowledge of Java interviewing (2)

Source: Internet
Author: User
Tags bitwise

1. Can I include more than one class (not an inner class) in a ". Java" source file? What are the restrictions? There can be multiple classes, but only one public class, and the class name of public must match the file name. 2, talk about the difference between & and &&. Both & and && can be used as the logical AND operator, representing logic and (and), when the result of an expression on either side of the operator is true, the entire result is true, otherwise the result is false if one of the parties is false. && also has a short-circuit function, that is, if the first expression is false, the second expression is no longer evaluated, for example, for an if (str! = null&&!str.equals (")) expression, when STR is NULL, The following expression does not execute, so the nullpointerexception will not appear if you change && to & NullPointerException exception will be thrown. if (x==33 &++y>0) y grows, if (x==33 && ++y>0) does not grow & can also be used as a bitwise operator when an expression on either side of the & operator is not a Boolean type,& For bitwise AND operations, we typically use 0x0f to perform a & operation with an integer to get the minimum 4 bit bits of the integer, for example, 0x31 & 0x0f results in 0x01. 3. How do I jump out of the current multiple nesting loops in Java? In Java, to jump out of multiple loops, you can define a label in front of the outer loop statement, and then use a labeled break statement in the code of the inner loop body to jump out of the outer loop. For example:
1 OK: 2  for (int i=0;i<10;i++)     {3for (int j=0;j<10;j++)            {4 System.out.println ("i=") + i + ", j=" + j); 5 if  Break OK; 6 }7 }
View Code4. Can the switch statement function on a byte, can it function on a long string? In switch (EXPR1), EXPR1 can only be an integer expression or enumeration constant (larger font). An integer expression can be an int primitive type or an integer wrapper type, and since Byte,short,char can be implicitly converted to int, these types and the wrapper types of those types are also possible. Obviously, long and string types do not conform to the syntax of switch, and cannot be implicitly converted to int types, so they cannot be used in Swtich statements. (jdk1.7 after the string class can)  5, char-type variables could not be stored in a Chinese character? Why? Char variables are used to store Unicode encoded characters, and the Unicode encoding character set contains Chinese characters, so Char variables can of course store Chinese characters. However, if a particular Chinese character is not included in the Unicode encoding character set, then this char variable cannot store this particular character. Unicode encoding consumes two bytes, so variables of type char are also two bytes.  6, when you use the final keyword to decorate a variable, is the reference immutable, or does the referenced object not change? When you use the final keyword to decorate a variable, it means that the reference variable cannot be changed, and the contents of the object to which the reference variable is pointing can be changed. For example, for the following statement: Final StringBuffer a=new stringbuffer ("immutable"), execution of the following statement will report a compile-time error: A=new stringbuffer (""); The following statements can be executed by compiling: A.append ("broken!"); What is the difference between a  7, a static variable, and an instance variable? The difference between the syntax definitions: Static variables are added with the static keyword, and the instance variable is not added before. The difference when the program runs: instance variables belong to an object's properties, and an instance object must be created where the instance variable is allocated space before the instance variable can be used. Static variables are not part of an instance object, but belong to a class, so also known as class variables, as long as the program loads the class's bytecode, without creating any instance objects, static variables will be allocated space, static variables can be used. In summary, an instance variable must be created before it can be used by the object, and a static variable can be referenced directly using the class name. For example, for the following program, no matter how many instance objects are created, there is always only one staticvar variable assigned, and each instance object is created, the Staticvar adds 1, but each creation of an instanceLike, a Instancevar is assigned, that is, multiple instancevar may be assigned, and each Instancevar value is only added 1 times.
1  Public classvarianttest{2      Public Static intStaticvar = 0;3      Public intInstancevar = 0;4      Publicvarianttest () {5staticvar++;6instancevar++;7System.out.println ("staticvar=" + Staticvar + ", instancevar=" +Instancevar);8     }9}
View Code8. Is it possible to make a call to a non-static method from within a static method? No. Because the non-static method is to be associated with an object, you must create an object before you can make a method call on the object, and the static method call does not need to create the object, which can be called directly. That is, when a static method is called, there may not be any instance objects created, and if a call to a non-static method is emitted from a static method, which object does that non-static method relate to? This logic cannot be set up, so a static method is issued inside a call to a non-static method. 9. The difference between integer and int int is one of the 8 raw data types provided by Java. Java provides encapsulation classes for each primitive type, and integer is the wrapper class provided by Java for Int. The default value for int is 0, and the default value for integer is null, that is, integer can differentiate between unassigned and value 0, and int cannot express an unassigned condition, for example, if you want to express the difference between not taking an exam and the test score of 0, you can only use integer. 10. What's wrong with the code below? if (Username.equals ("Zxx") {}username may be null, it will report a null pointer error; equals (username) 11, interface can inherit interface? Can an abstract class implement (implements) interfaces? Can an abstract class inherit a specific class (concrete Class)? Is there a static main method in the abstract class? Interfaces can inherit interfaces. Abstract classes can implement (implements) interfaces, and abstract classes can inherit concrete classes. There can be static main methods in an abstract class. 12, when writing the Clone () method, usually have a line of code, what is it? Clone has a default behavior, Super.clone (), because the members of the parent class are first copied into place, and then the members are copied. 13. What is the mechanism for polymorphism in Java? A reference variable that is defined by a parent class or interface can point to a subclass or an instance object of a specific implementation class, and the method called by the program is dynamically bound at run time, that is, the method that refers to the specific instance object that the variable points to, that is, the method of the object that is running in memory, rather than the method defined in the 14. What are the output results of the following program?
1  Packagecom.zhang.test;2 3 Importjava.util.Date;4 5  Public classTestextendsdate{6 7      Public Static voidMain (string[] args) {8         Newtest (). Test ();9     }Ten  One      Public voidTest () { ASystem.out.println (Super. GetClass (). GetName ()); -System.out.println (Super. GetClass (). Getsuperclass (). GetName ()); -     } the}
View Code  Output results in the test method, the GetClass (). GetName () method is called directly, and the test class name is returned, because GetClass () is defined in the object class as final, the subclass cannot overwrite the method, so Calling the GetClass (). GetName () method in the test method is actually called the GetClass () method inherited from the parent class, which is equivalent to calling Super.getclass (). GetName () method, so, Super.getclass The (). GetName () method returned should also be test. If you want the name of the parent class, you should use the following code: GetClass (). Getsuperclass (). GetName ();  15, string is the most basic data type? Basic data types include byte, int, char, long, float, double, Boolean, and short. The Java.lang.String class is of the final type, so you cannot inherit the class or modify the class. In order to improve efficiency and save space, we should use StringBuffer class  16, String s = "Hello"; s = s + "world!"; After the execution of these two lines of code, did the content in the original string object change? No. Because string is designed as an immutable (immutable) class, all of its objects are immutable objects. In this code, s originally pointed to a string object, the content is "Hello", and then we have a + operation on S, then the object pointed to by S has changed it? The answer is no. At this point, S does not point to the original object, and point to another string object, the content is "Hello world!", the original object is still in memory, but s this reference variable no longer points to it. With the instructions above, it is easy to derive another conclusion that if you frequently make a variety of changes to a string, or if you do not anticipate the changes, then using string to represent strings can cause significant memory overhead. Because a string object cannot be changed after it is established, a string object is required for each different string. You should consider using the StringBuffer class, which allows you to modify instead of creating a new object for each different string. Also, the conversion of these two kinds of objects is very easy. At the same time, we can also know that if you want to use the same content string, you do not have to new a string each time. For example, we want to initialize a string reference variable named S in the constructor,Set it to the initial value, you should do this: public class Demo {private String s;public demo {s = "Initial value";}} Not public Demo {s = new String ("Initial Value");} The latter invokes the constructor every time, generates new objects, performs poorly and has a large memory overhead, and makes no sense, because the string object is immutable, so for a string of the same content, just one string object to represent it. It also says that multiple calls to the above constructor create several objects whose string type property s all point to the same object. The above conclusion is also based on the fact that, for string constants, if the content is the same, Java considers them to represent the same string object. Calling the constructor with the keyword new always creates a new object, regardless of whether the content is the same.

Basic knowledge of Java interviewing (2)

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.