Java Interview Collection (vii)

Source: Internet
Author: User
Tags binary to decimal decimal to binary stringbuffer

Objective:

Java Interview Collection (vi)
The review, for final can modify constants, methods, and classes, once the constants are defined and immutable, and methods, used final to modify the method, the method cannot be overloaded, inherited, rewritten, final used to modify the class, the class cannot be inherited.

Abstract abstract , cannot have the object, namely cannot instantiate, but constructs the method, in the abstract adornment class, this abstract class, does not have the abstract method, but in the class has the abstract method, then this class must be the abstract class.

In all abstract methods in an abstract class, once the subclass inherits the abstract class, it is necessary to rewrite all the abstract methods in the parent class (that is, the abstract class), but even the subclass is abstract, and if the subclass inherits the abstract parent class, the subclass is also an abstract class.

Abstract methods are not method bodies, because abstract methods are abstract functions, implemented in subclasses, abstract methods can be overloaded, that is, parameters and so on. Abstract methods can no longer be used static/final/private to modify.

interfacethe abstract method of an interface is decorated with public abstract attributes public static final .

The interface is not instantiated, and there is no construction method

The inner class is classified as the inner class of the method, the inner class of the member, the static inner class, and the anonymous inner class.

The foundation of the package ( package ) and the garbage collection mechanism.

1. Object objects

The parent class of all classes, Object because any class is a direct or indirect inheriting class, is a subclass of the Object Object Object class hierarchy that exists in the root class java.lang.Object .

getClassYou can get the actual type of the object
toStringYou can return the address of an object
equalsYou can tell if two objects are equal

The relationship between the object and the class is judged by:instanceof

class A {}class B extends A {}interface D{}class C extends B implements D {}
B b = new C();b instanceof A --- trueb instanceof B --- trueb instanceof C --- true b instanceof D --- true

Construction Method:public Object()

    • finalize()Method: Garbage collection time in Java
    • Object的toString()Method: Returns the string form of the object
    • Object的equals()Method: Compare two objects for equality

Review:

    • Binary: 以0b/0B作为开头 (0~1, full 2 in 1)
    • Octal: 以0作为开头 (0~7, full 8 in 1)
    • Decimal: 没有特殊标识 (0~9, full 10 in 1)
    • Hex: 以0X/0x作为开头 (09,AF, full 16 in 1)

Conversion between the binary systems

    • Decimal to binary: constantly dividing by 2, then taking the remainder
    • Binary to decimal: Multiply from the lowest bit by 2 to the power of precedence, then sum
Similarly:
十进制转其他进制:除以对应的进制数,然后取余数其他进制转十进制:从最低位依次开始,按位次乘以进制的位次次幂,然后求和

Binary to octal, but also from the low start, every three bits of binary for a group, the generation of an octal number, the highest bit less than three, on 0, together three bits can be.

Formula: three Change one

Octal binary system, each octet number will produce three-bit binary digits, less than three 0 can be.

Formula: one change three

Similarly
二进制转十六位进制:口诀为四变一十六位进制转二进制:口诀为一变四

The bin is binary, the OCT is octal, Dec is decimal, hex is hex

2. String class

In Java the string that belongs to the object, the Java String class is provided to create and manipulate the string, that is, the object is used, because the String class-decorated character once created is immutable, so when modifying the string, you need to use the StringBuffer and StringBuilder class.

StringA class is used to modify a string, and a string is a special object that, once initialized, cannot be changed, and a String modified string variable cannot be changed.

Example:

//定义一个字符串String str = "hello world";String str = new String("hello world");

For a String class, java.lang.String in existence, a String class that represents a string, and how to implement the literal value of a string, is to use this class to instance.

Stringclass, the class that represents the string, and how the string is String the object. A string is a constant, enclosed in double quotation marks, that is initialized and cannot be changed. So here's an example of the effect.

String i = "123";System.out.println("i="+i);//结果为i=123如果添加以下String i = "123";i = "12"System.out.println("i="+i);//结果为i=12// 看到这个效果,你会认为不是改了吗?// 其实不是的,而是新创建了一个对象而已。

In String , objects are immutable, but can be shared. So how to understand is shared? This leads to the concept of a constant pool, as follows:

//多个引用指向同一个字符串String str1 = "dashu"String str2 = "dashu";System.out.println(str1==str2);//结果true

To true represent them at the same time pointing to a string, which is the object. An object is created and the str1 string is "dashu" common sense, so when you create an object again, the commonsense name is the same, and the same is found in the constant pool "dashu" , pointing to a value at the same time.

Chang is a constant, if you have the same value, you do not have to create the object, the first created string is placed in the constant pool, if you want to use, it is used.

// 字符串在底层是以字符数组形式来存储的String str = “ab”; String str = new String(“ab”);
String s = “a”;s = s + “b”;
//内容相同,但是创建方式不同的情况String str3 = "abc"String str4 = new String ("abc");System.out.println(str3==str4);//falseSystem.out.println(str3.equals(str4));//true//结果falsetrue
// 100个元素拼接成一个字符串,使用+进行拼接// 整个过程产生301个String[] arr = { /*100个元素*/ };// 为 1String str = “”;for(String s : arr){str += s; // 每拼接1次,要多产生3个对象。// 一共100个元素,拼接100次,意味着要多产生300个元素}

StringRepresents a class that is a string, and the string itself is a constant, the string is stored as a character array at the bottom, and the string is shared, in a constant pool

StringBuffer

For a string is a constant, its value cannot be changed after it is created, but the string buffer supports a mutable string.

StringBuffeThe R class is java.lang medium, StringBuffer is a string buffer, is a thread- StringBuffer safe variable character sequence, similar to String a string buffer, the buffer cannot be changed, but inside can be changed, through a method can change the length and content of the sequence.

StringBufferProvides the main two methods, one, append() two,inset()

StringBufferAs a string buffer, relative to a container, the length is variable, can store any type of data, is to convert arbitrary data into a string to store, StringBuffer provides a lot of data operation function.

Example:

StringBuffer sb = new StringBuffer();sb.append("da");sb.append("shu");System.out.println(sb);//sb.append("da").append("shu");

If you want to manipulate the data, convert to a string. StringBufferall stored elements are converted to strings for use.

String str = sb.append("da").append("shu").toString();

Inserts an element at a specified location

sb.insert(2,"hehe");//插入

StringBuilderThe efficiency is much higher than with "+", need to stitch multiple strings, recommended to use StringBuilder .

The difference between StringBuffer and StringBuilder

StringBuilderis thread insecure and thread- StringBuffer safe

StringBuilderFor a java.lang class, is a variable sequence of characters, provided with StringBuffer compatibility API , and StringBuffer the StringBuilder method is identical.

StringBuilderOut of sync, unsafe. If there append(),delete(),insert(), are errors at the same time, multi-threaded access is unsafe, add adornments synchronized . jdk1.5after the release, the rollout StringBuilder is used as a StringBuffer simple replacement for a string buffer when used by a single thread.

3. Box packing and unpacking

Encapsulation classes are: Byte , short , Integer , Character , long , Float , Double Remember these classes are all you can, these are Number subclasses.

Packing class

byteByteshortShortintIntegerlongLongfloatFloatdoubleDoublecharCharacterbooleanBooleanvoidVoid

Automatic carton sealing/automatic packing

The underlying default calling valueOf method is to marshal the box

Automatic unpacking

Use ***Value() to disassemble the case

Automatic encapsulation, assigning a variable of the base type to the corresponding reference type Object
Automatic unpacking, assigning variables of reference types to corresponding base type variables

public class Test{ public static void main(String[] args){  int i = 5;  Integer integer = new Integer(i);//装箱  //拆箱  int i2 = integer.intValue(); }}//public class Test{ public static void main(String[] args){  int i = 5;  Integer integer = new Integer(i);  Interger i2 = i;//自动装箱  int i3 = integer;//自动拆箱 }}//public class Test{ public static void main(String[] args){  char c = 'Vic';  character c2 = c;  c3 = c2;  }}
4. Class Date

DateClass representing the date

How date is constructed

date (): to assign a Date object and initialize the object
Date(int year, int month, int date)

Classes for calendar calendars

CalendarClass is an abstract class

public abstract class Calendar extends Object//Calendar 提供了一个类方法 getInstance Calendar rightNow = Calendar.getInstance();
Conclusion
    • Below I will continue to Java the Android other knowledge, in-depth explanation, interested can continue to follow
    • A little gift to walk or like.

Java Interview Collection (vii)

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.