Java-- face questions

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_45613931/article/details/102536268


I was a novice, to write blog for self-review, self-summary.
If wrong, please point out heavyweights.
Reference materials: zero-based learning Java


A, Java can be used to represent non-zero it true?

Java is a strongly typed language, it conditional expressions have very strict rules, only use a boolean data will be evaluated. If conditional number for non-zero integer, is embodied as a syntax error.

For example: if (100) {····} is wrong.
In other words if, such a statement for years, can only use two kinds of false and true values.
For example: if (100> 90) { ····}

In fact, which to some extent also to ensure the safety program and allows developers to define how to determine the conditions. If you use a non-zero conditions, it is possible the value of the developer is not required to run, such as the variable has not been initialized. Additionally Java type for boolean variables, initialization is required prior to use.
For example:
Boolean A
IF (A)} {···· is wrong.

Second, what StringBuffer and StringBuilder there is the role?

If by directly adding String string splicing efficiency is very low, which may produce a lot of redundant String objects, and if the number of strings required stitching thousands, then the JVM load is very large, seriously affect the performance of the program. In other words, in the face of splicing requires a lot of strings, then the role of StringBuffer and StringBuilder class is manifested, they are a complement to a String. This can improve performance:

public class StringBBTest{
    public static void main(String args[]){
        String a="a";
        String b="b";
        String c="c";
        StringBuffer sb=new StringBuffer();
        sb.append(a);
        sb.append(b);
        sb.append(c);
        String abc=sb.toString();
        System.out.println(abc);
    }
}

Third, the length of the two-dimensional array of whether fixed?

Java is actually a two-dimensional array: create a one-dimensional array, then the array elements in reference to another one-dimensional array. When using a two-dimensional array by two brackets "[]" to access the referenced dimension of each layer, until the final access to data.
example:

public class Array{
    public static void main(String args[]){
        int [][] arr=new int[3][];
        arr[0]=new int[]{4};
        arr[1]=new int[]{4,5};
        arr[2]=new int[]{4,5,6};
        for(int[] a:arr){
            for(int i:a){
                System.out.print(i+"\t");
            }
            System.out.println();
        }
    }
}

When the two-dimensional array is instantiated, the second dimension and length is not specified, there is no need to specify, since they can be of different lengths. Arr.length equal to the embodiment 3, but arr [i] .length not the same. Therefore, when traversing the two-dimensional array, the printed length is different.

Fourth, the data collection in line with what conditions can use a foreach loop?

foreach loop through a collection is in the original appeal, act as substitute iterator. Grammatically speaking, to achieve a class instance or an array Iterable interface, we can all be used foreach loop.
example:

List list=new ArrayList();
list.add("a");
list.add("b");
list.add("c");
for(String s:list){
    System.out.println(s);
}
String[] arr={"a","c","b"};
for(String s:arr){
    System.out.println(s);
}

Java is an array of things defined, it can only follow the syntax to use. But developers can customize a set of classes. The custom collection class needs to do the following things:
1) the definition of a class that contains a member variable integer index and a set of objects (such as an array or a linked list)
2) This class implements an interface Iterable
3) providing an implement interface Iterable or which itself implements the Iterator Interface
method 4) using the following set of member variables and subscript accomplished Iterator objects needed to interface

Example:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class MyForeach{
    public static void main(String args[]){
        MyList list=new MyList();
        list.getList().add("a");
        list.getList().add("b");
        list.getList().add("c");
        for(String s:list){
            System.out.println(s);
        }
    }
}
class MyList implements Iterator<String>,Iterator<String>{//自定义链表类
    private int loc=0; //当前的下标
    private List<String> list=new ArrayList<String>();//存储数据
    public boolean hasNext(){ //是否有下个元素
        return list.size()>loc;
    }
    public String next(){  //得到下一个元素
        return list.get(loc++); 
    }
    public void remove(){  //删除当前下标的元素
        list.remove(loc);
    }
    public List<String> getList(){
        return list;
    }
    public void setList(List<String> list){
        this.list=list;
    }
    public Iterator<String> iterator(){ //得到迭代器
        return this;
    }
}

Fifth, the difference between interfaces and abstract classes?

An abstract class is a class dysfunction, but a collection of static and abstract method declarations that can not be modified data interface, neither be instantiated. In a sense, interface is a special form of abstract class in the Java language, abstract class represents an inheritance, a class can only inherit an abstract class, but a class can implement multiple interfaces.

Guess you like

Origin blog.csdn.net/qq_45613931/article/details/102536268