* The following is something I find useful when preparing for a Java interview, some questions that the interviewer will probably ask.
* Content (in addition to the code) details from the network (when learning to encounter Baidu's (*^__^*))
* If you find something wrong, please let me know. Thanks!! ☆⌒ (*^-゜) v
Basic types of 1:java
The Java language provides eight basic languages
boolean |
char |
byte |
8-bit |
Short |
|
|
16 Guests |
Int |
Float |
|
32 Guests |
Long |
Double |
|
64 guests |
Note: The string itself is an object rather than a base data type, and the variable name of string is a reference to the string class
The difference between 2:string, StringBuffer and StringBuilder
2.1: The execution speed of the three: String<stringbuffer<stringbulider
2.2:string are string constants, meaning that values are not changed, and StringBuffer and StringBuilder are string variables that can manipulate values.
public class Stringtest {
public static void Main (String argv[]) {
String s = "abc";
s = s+ "Def";
System.out.println ("s=" +s);//s=abcdef
/*
* We obviously changed the string type of the variable s, why is it that there is no change?
* In fact, this is a kind of deception, the JVM is parsing this piece of code:
* First create object S, give an ABC
* And then create a new object s to execute the second line of code,
* That means we didn't change the object s before, so we say that the string type is an immutable object,
* Because of this mechanism, whenever strings are manipulated with string, the new objects are actually created constantly,
* and the original object will be garbage collected by GC, it is conceivable how low implementation efficiency.
*/
}
}
The difference between 2.3:stringbulider and StringBuffer
Stringbulider is thread-insecure and StringBuffer is thread-safe. The JVM does not guarantee that the StringBuilder operation is secure, and although he is fast, it is guaranteed that stringbuffer can determine the operation. However, because most operations are done in a single-threaded environment, it is most often recommended to use StringBuilder because of the speed problem.
2.4: Summarize the three: if you manipulate a small amount of data, use string
A single-threaded operation string buffer under a large amount of data using Stringbulider
Multi-threaded operation string buffer under large amounts of data using StringBuffer
3: Conversions between types
public class Stringtest {
public static void Main (String argv[]) {
Convert string to int float double
String s = "123";
int I=integer.parseint (s);
float F = float.parsefloat (s);
Double D = double.parsedouble (s);
System.out.println ("i=" +i+ "f=" +f+ "d=" +d);
System.out.println (integer.valueof (s));
//Note: Although valueof can also convert strings to int, the valueof call is Parse method, which returns an already encapsulated object, such as an integer
convert int float double to string
String S1 = string.valueof (i);
String s2 = string.valueof (f);
String s3 = string.valueof (d);
System.out.println ("s1=" +s1+ "s2=" +s2+ "s3=" +S3);
}
}
4: Usage of the date
The difference between 4.1:calendar and date
Date is a class, and the calendar is an abstract class. In JDK1.0, date is the only class that represents time, but since date is not internationalized, it is recommended to use the Calendar class for time and date processing, starting with the JDK1.1 version.
Import Java.util.Calendar;
Import Java.util.Date;
public class Datetest {
public static void Main (String argv[]) {
Calendar calendar = Calendar.getinstance ();
Date date = Calendar.gettime ();
Sun Jul 10:33:44 CST 2017 (interpretation: Sunday July 23 10:33 44 seconds 2017 years)
SYSTEM.OUT.PRINTLN (date);
Get Date
int year = Calendar.get (calendar.year);
int month = Calendar.get (calendar.month) +1;//date is 0~11 so add 1
System.out.println ("year=" +year+ "month=" +month);//year=2017 month=7
Get Date
int day1 = Calendar.get (calendar.day_of_year);//No. 204 Day of the year
int day2 = Calendar.get (calendar.day_of_month);//23rd Day of the month
int day3 = Calendar.get (Calendar.day_of_week);//First day of the week (starting from Sunday)
int day4 = Calendar.get (calendar.date);
System.out.println ("day1=" +day1+ "day2=" +day2+ "day3=" +day3+ "day4=" +day4); //day1=204 day2=23 day3=1 day4=23
Get Time seconds
int hour1 = Calendar.get (Calendar.hour_of_day);
int hour2 = Calendar.get (Calendar.hour);
int minute = Calendar.get (Calendar.minute);
int second = Calendar.get (Calendar.second);
hour1=10 hour2=10 minute=59 second=26 10 points 59 minutes 26 seconds
System.out.println ("hour1=" +hour1+ "hour2=" +hour2+ "minute=" +minute+ "second=" +second);
}
}
The difference between 4.2:dateformat and SimpleDateFormat
DateFormat can be used directly, but it is an abstract class, which can be displayed differently depending on the area specified by the locate. SimpleDateFormat is a subclass of DateFormat, and generally dateformat classes are rarely used directly, but are performed by the SimpleDateFormat class.
Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date;
public class Datetest {
public static void Main (String argv[]) {
Calendar calendar = Calendar.getinstance ();
Date date = Calendar.gettime ();
DateFormat df1 = Dateformat.getdateinstance ();
DateFormat DF2 = Dateformat.getdatetimeinstance ();
System.out.println (Df1.format (date));//2017-7-23
System.out.println (Df2.format (date));//2017-7-23 12:18:51
SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month DD day");
System.out.println (Sdf.format (date));//August 23, 2017
}
}
5: The difference between set list set map
The collection list is ordered, and the elements can be repeated
Set set unordered, where elements cannot be duplicated
A collection map is a collection of key objects and value object mappings, each containing a key and a value that does not allow repetition, but can map any number of keys to a value object.
6: File
I am very unfamiliar with the file, so this piece will be listed separately _ (: Зゝ∠) _
7: Object-oriented Programming features
7.1: Package
Encapsulation is to combine the object's attribute starvation operation into a separate whole, and hide the object's internal implementation details as much as possible
public class User {
private int id;
private String name;
public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
}
This is a typical encapsulation case where the property ID and name of the user class are hidden with private and can only be manipulated by the get and set methods
7.2: Abstract
The class that uses the keyword abstract declaration is called an abstract class. If a class contains one or more abstract methods, the class must be specified as abstract. An "abstract method", which belongs to an incomplete method, contains only one declaration and no method body.
Public abstract class Abstracttest {
public abstract void get ();
Public String set () {
Return "This is an abstract class";
}
}
7.3: Inheritance
Inheritance is the most significant feature of object-oriented. Inheritance is the derivation of new classes from existing classes, which can absorb data properties and behaviors of existing classes, and can extend new capabilities
public class Testextends {
public static void Main (String argv[]) {
b b = new B ();
B.get ();
Test result: Executing subclasses ... This is the parent class
}
}
Class a{
Public String toString () {
Return "This is the parent class";
}
}
Class B extends a{
public void get () {
System.out.print ("Executing subclasses ... "+tostring ());
}
}
7.4: polymorphic
Polymorphism can be divided into compiler polymorphism (mainly in overloading) and operation polymorphism (mainly embodied in inheritance)
8: The difference between overloading and rewriting
Overloading is a method in which the same class has a different parameter list with the same name
Overriding is a subclass that creates a method that is the same as the parent class except for the rest of the method body (method name, parameter list, return value), overriding the parent class method
It is important to note that an overridden method cannot throw a non-runtime exception that is not defined in the base class, but it can throw a run-time exception that is not defined in the base class
Reason: 1: Even if there is a runtime exception can be compiled, and if there is a non-runtime exception must be processed (throws or try{}catch{})
2: Subclass overriding Parent class method throws a category that cannot exceed the scope of the parent class
9: The difference between an interface and an abstract class
9.1: The methods inside the abstract class can be implemented concretely, but all the methods in the interface are abstract methods.
9.2: Abstract classes can have constructors, interfaces cannot have constructors
9.3: Abstract methods can have public,protected , and default modifiers, but the interface can only use the public modifier
9.4: Abstract methods can inherit a class and implement multiple interfaces, the interface can inherit only one or more other interfaces
To be Continued ... O (∩_∩) o~~
Java Foundation interview