JavaSE basic questions

2020Java interview questions and answers (personal understanding, for reference)

1. Object-oriented features and characteristics of the three

  • Package: a package is to hide the object properties and implementation details, only publicly interface control program attributes in the read and modify access level, abstract data obtained and acts (or function) combine to form an organic whole, i.e. the source data and the operational data organically combined to form a "class", the data and functions which are members of the class.
  • Inheritance: Inheritance is one of the basic features of object-oriented inheritance is the parent class subclass inherits characteristics and behavior, so that the subclass object (instance) instance fields and methods having a parent class, method, or subclass inherits from the parent class, such subclasses have the same parent class behavior.
  • Polymorphism: polymorphism having a plurality of capability or form of different forms of the same behavior. Achieve polymorphism has three necessary conditions: rewriting, inheritance, references to the parent class subclasses. Polymorphism is simply different subclasses respond differently to the same object.

2. JRE and JDK What is the difference

  • JDK: Java Development Kit for short, provides a Java development environment and runtime environment.
  • JRE: Java development environment for short, provides an environment for running Java, there are Java files that can be run

3. What is the difference == and equals

  • == basic data type is the comparison value for comparison reference type is a reference
  • equals comparison is a reference to equality
		//String重写了equals方法
	    String a =new String("a");
		String b =new String("a");
		String c="c";
		String d="c";
		String e="a";
		System.out.println(a==b);//false
		System.out.println(a.equals(b)); //true
		System.out.println(c==d);//true
		System.out.println(c.equals(d)); //true
		System.out.println(a==e); //false
		System.out.println(a.equals(e)); //true

4. String is not a primitive data type?

  • String is not a primitive data types, String is java.lang bottom (remember String is final, we can not inherit, but provides a number of static methods).
  • Basic data types: byte, char, short, int, folat, double, long, boolean

What is the role 5. final in Java

  • final modified class can not be inherited
  • final modified method can not be overridden
  • final modified variable called constant value after initialization constants can not be changed

Integer 6.int difference and the
Java provides basic data types and reference types, int belonging to the basic data types default value 0
Integer belonging to a reference type, the default value is null
Int no method, providing many ways Integer

7.String str = "i" and the String str = new String ( "i") the same?

  • Not the same, memory allocation is not the same. String str = "i" in the constant pool distribution, String str = new String ( "i") allocated heap memory.

Difference 8.String and StringBuffer, StringBuilder's

  • String: String statements are immutable objects, each operation generates a new String object, then the pointer to the new String object, and StringBuffer, StringBuilder may be operated on the basis of the original object, so the change in the regular string in the case of content is best not to use String.
  • StringBuffer and StringBuilder biggest difference is that, StringBuffer is thread-safe, but StringBuilder is not thread-safe, but the performance is higher than StringBuffer StringBuilder, so in single-threaded environment is recommended to use StringBuilder, StringBuffer is recommended to use a multithreaded environment.

9. say Servlet life cycle
after Servlet instantiated by the server, run init method of the container, request to the service process service
method for automatically invokes the appropriate method doget dopost or destroying objects when the server decides when destroy method is called in the manner requested

10.List, the difference set, Map of

Compare List Set Map
Inherited Interface Collection Conllection
Common implementation class AbstractList (common sub-category are: ArrayList, LinkedList, Vector) AbstractSet (common subclass hashSet, TreeSet, LinkedhashSet) Map (common subclasses: hashMap, hashtable)
element Repeatable Unrepeatable Unrepeatable
Sequence Ordered Disorderly
Thread Safety Vector hashTable

11.Collection and Collections of the difference.

  • Collection is a set of interfaces superior class with the Set List of
  • Collections help against a class set of classes, he offers a series of static methods to achieve a variety of search collection, sorting, and so thread-safe operations.

hashCode 12. two objects () are the same, equals () it must also be true

  • HashCode two identical objects not necessarily equals true;
		String aa="柳柴";
		String bb="柴柕";
		System.out.println(String.format("aa:%d | bb:%d",  aa.hashCode(),bb.hashCode()));
		System.out.println(aa.equals(bb));

Output: AA: 851 553 | bb: 851 553
false
This shows the same equals hashcode is not the same as true;

13. how to calculate the fastest 2-by-8

  • 2<<3

14. java in Math.round (-2.5) equal to how much?

  • Equal to -2, because the number of value axis, the intermediate value (0.5) rounding to the right, so 0.5 is positive rounding up negative 0.5 is directly discarded.

Common method 15.String class have those?

  • indexOf (): Returns the index of the specified character.
  • charAt (): Returns the character at the specified index.
  • replace (): replacement string.
  • trim (): removing a blank string ends.
  • split (): Split string, returns an array of strings after the division.
  • getBytes (): returns a string type byte array.
  • length (): returns the length of the string.
  • toLowerCase (): turn into lowercase string.
  • toUpperCase (): turn a string to uppercase characters.
  • substring (): string taken.
  • equals (): comparison string.

16. ordinary classes and abstract classes What are the differences? ?

  • Common classes can not contain abstract methods, abstract class may contain conventional method
  • General class can be directly instantiated abstract class can not be instantiated directly

17. talk about abstract classes can use the final modified it?

  • An abstract class can not be final modification, because the abstract class needs to be inherited, while the final modified class can not be inherited.
                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like  tool-clicked"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count">1</span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/xiaoyanghnl">
                <img src="https://profile.csdnimg.cn/E/9/C/3_xiaoyanghnl" class="avatar_pic" username="xiaoyanghnl">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/2x/1.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/xiaoyanghnl" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">Mr.JiaWan</a></span>
                                        </div>
                <div class="text"><span>发布了3 篇原创文章</span> · <span>获赞 1</span> · <span>访问量 240</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=xiaoyanghnl" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                </div>
                        </div>
                </div>
发布了1 篇原创文章 · 获赞 0 · 访问量 10

2020Java面试题及答案(个人理解,仅供参考)

1. 面向对象的三大特征及特点

  • 封装:封装就是隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别,将抽象得到的数据和行为(或功能)相结合,形成一个有机的整体,也就是将数据与操作数据的源代码进行有机的结合,形成“类”,其中数据和函数都是类的成员。
  • 继承:继承是面向对象的基本特征之一,继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为。
  • 多态:多态同一个行为具有多个不同表现形式或形态的能力。实现多态有三个必要条件:重写,继承,父类引用指向子类。多态简单来说就是不同子类对同一个对象做出不同的响应。

2. JRE与JDK有什么区别

  • JDK:简称Java开发工具包,提供了Java开发的环境和运行环境。
  • JRE:简称Java开发环境,提供了Java运行的环境,有这个就能运行Java文件

3. == 和equals有什么区别

  • ==对基本数据类型是值的比较,对引用类型比较的是引用
  • equals比较的是引用是否相等
		//String重写了equals方法
	    String a =new String("a");
		String b =new String("a");
		String c="c";
		String d="c";
		String e="a";
		System.out.println(a==b);//false
		System.out.println(a.equals(b)); //true
		System.out.println(c==d);//true
		System.out.println(c.equals(d)); //true
		System.out.println(a==e); //false
		System.out.println(a.equals(e)); //true

Guess you like

Origin blog.csdn.net/qq_45452081/article/details/104917733