之前在面Tripadvisor的时候面了很简单的一道java基础知识,结果没答出来,决定做一些小总结,经常更新更新自己复习看看。
1. String方面
String s = "abc";
String s = new String("abc");
这两个有什么区别
在java中,用heap和stack存储 The heap stores all objects including arrays, and all class variables
The stack stores all local variables and primitive types.
Primitive types有8个:
byte: 8-bit integer -128 - 127
short: 16-bit integer -32768-32767
int: 32-bit integer -2147483648 - 2147483647
long: 64-bit integer
double: 64-bit floating point number
float: 32-bit
boolean: true/false
char: a character
所以第一个会在stack中创建三个char type: ‘a’, ‘b’, ‘c’,然后在String pool中创建一个String "abc"
第二个除了以上两个步骤,还需要在heap中创建String对象。
这个String pool的用处在于,如果下一次创建新的String对象,比如String x = "abc"; 它会先在字符串池里搜索有没有这个String, 有的话直接引用就可以了。
《String的特性》
1、String类是final的,不可被继承。
2、String类是的本质是字符数组char[], 并且其值不可改变。
3、String类对象有个特殊的创建的方式,就是直接指定比如String x = "abc","abc"就表示一个字符串对象。而x是"abc"对象的地址,也叫做"abc"对象的引用。
4、String对象可以通过“+”串联。串联后会生成新的字符串。
5、Java运行时会维护一个String Pool(String池),JavaDoc翻译很模糊“字符串缓冲区”。String池用来存放运行时中产生的各种字符串,并且池中的字符串的内容不重复。而一般对象不存在这个缓冲池,并且创建的对象仅仅存在于方法的堆栈区。
6、创建字符串的方式很多,归纳起来有三类:
其一,使用new关键字创建字符串,比如String s1 = new String("abc");
其二,直接指定。比如String s2 = "abc";
其三,使用串联生成新的字符串。比如String s3 = "ab" + "c";
object types | primitive types | |
contains a | reference | value |
how defined | class definition | built in java |
hwo created | "new" | "6", "3.4", "true" |
how initialized | constructor | default value |
how used | methods | operations("+") |
2. Java I/O classes
Objects in System class for interacting with a user:
System.out is a PrntStream object that outputs to the screen.
System.in is an InputStream object that reads from the keyboard.
readLine method is defined on BufferedReader objects.
- How do we construct a BufferedReader? With an InputStream reader.
- How do we construct an InputStreamReader? With an InputStream.
- How do we construct an InputStream? System.in is one.
InputStream reads raw data.
InputStreamReader composes it into characters.(typically 2 bytes)
BufferedReader composes chars into entire lines of text.
BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
System.out.println(keybd.readLine());
3. JVM
What is JVM ? Why is Javacalled the “Platform Independent Programming Language” ? A Java virtual machine (JVM) is aprocess virtual machine that can execute Java bytecode. Each Java source file is compiled into a bytecode file,which is executed by the JVM. Java was designed to allow application programsto be built that could be run on any platform, without having to be rewrittenor recompiled by the programmer for each separate platform. A Java virtualmachine makes this possible, because it is aware of the specific instructionlengths and other particularities of the underlying hardware platform.
Every Java program is first compiled into an intermediate language called Java bytecode. The JVM is used primarily for 2 things: the first is to translate the bytecode into the machine language for a particular computer, and the second thing is to actually execute the corresponding machine-language instructions as well. The JVM and bytecode combined give Java its status as a "portable" language – this is because Java bytecode can be transferred from one machine to another.
Type a source code ---> Save as .java ---> compiler compiles source code into.class file, which is made up of bytecodes. ---> JVM runs .class file.
4. Object-Oriented
Java is a computer programming language that is concurrent, class-based and object-oriented. The advantages of object oriented software development are shown below:
- Modular development of code, which leads to easy maintenance and modification.
- Reusability of code.
- Improved reliability and flexibility of code.
- Increased understanding of code.
Encapsulation
Encapsulation provides objects with the ability to hide their internal characteristics and behavior. Each object provides a number of methods, which can be accessed by other objects and change its internal data. In Java, there are three access modifiers: public, private and protected. Each modifier imposes different access rights to other classes, either in the same or in external packages. Some of the advantages of using encapsulation are listed below:
- The internal state of every objected is protected by hiding its attributes.
- It increases usability and maintenance of code, because the behavior of an object can be independently changed or extended.
- It improves modularity by preventing objects to interact with each other, in an undesired way.
Polymorphism
is the capability to provide multiple implementations of an action and to select the correct implementation based on the surrounding context. For example, a class might define two versions of a method with different parameters. Or the same method might be defined both in a parent class and a subclass, the latter overriding the former for instances of the subclass.
Inheritance
Inheritance provides an object with the ability to acquire the fields and methods of another class, called base class. Inheritance provides re-usability of code and can be used to add additional features to an existing class, without modifying it.
Abstraction
Abstraction is the process of separating ideas from specific instances and thus, develop classes in terms of their own functionality, instead of their implementation details. Java supports the creation and existence of abstract classes that expose interfaces, without including the actual implementation of all methods. The abstraction technique aims to separate the implementation details of a class from its behavior.
5. public, protected, private
public is visible to everywhere.
protected field/method is visible to declaring class & all its subclasses.(between public and private)
private不能被该class以外的class用 private aren't visible to subclasses.
declaration | in the same package | in a subclass | everywhere |
public | yes | yes | yes |
protected | yes | yes | |
package | yes | ||
private |
6. Object versus Class
So, we can say that whereas a class is a general concept (like an Animal), an object is a very specific embodiment of that class, with a limited lifespan (like a lion, cat, or a zebra). Another way of thinking about the difference between a class and an object is that a class provides a template for something more specific that the programmer has to define, which he/she will do when creating an object of that class.
7. abstract class and interface
A subclass can modify a superclass in 3 ways:
1. It can declare new fields.
2. It can declare new methods.
3. It can override old methods with new implementations.
An abstract class is a class whose sole purpose is to be extended. A method is declared abstract when it has a method heading, but no body – which means that an abstract method has no implementation code inside curly braces like normal methods do.
An abstract class may provide some methods with definitions – so an abstract class can have non-abstract methods with actual implementation details. An abstract class can also have constructors and instance variables as well. An interface, however, can not provide any method definitions – it can only provide method headings. Any class that implements the interface is responsible for providing the method definition/implementation.
Java interfaces
= public method prototype & behaviors
difference:
- All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain both abstract and non-abstract methods.
- A class may implement a number of Interfaces, but can extend only one abstract class.
- In order for a class to implement an interface, it must implement all its declared methods. However, a class may not implement all declared methods of an abstract class. Though, in this case, the sub-class must also be declared as abstract.
- Abstract classes can implement interfaces without even providing the implementation of interface methods.
- Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
- Members of a Java interface are public by default. A member of an abstract class can either be private, protected or public.
- An interface is absolutely abstract and cannot be instantiated. An abstract class also cannot be instantiated, but can be invoked if it contains a main method.
1.相同点
A. 两者都是抽象类,都不能实例化。
B. interface实现类及abstract class的子类都必须要实现已经声明的抽象方法。2. 不同点
A. interface需要实现,要用implements,而abstract class需要继承,要用extends。
B. 一个类可以实现多个interface,但一个类只能继承一个abstract class。
C. interface强调特定功能的实现,而abstract class强调所属关系。
D. 尽管interface实现类及abstract class的子类都必须要实现相应的抽象方法,但实现的形式不同。interface中的每一个方法都是抽象方法,都只是声明的 (declaration, 没有方法体),实现类必须要实现。而abstract class的子类可以有选择地实现。
这个选择有两点含义:
一是Abastract class中并非所有的方法都是抽象的,只有那些冠有abstract的方法才是抽象的,子类必须实现。那些没有abstract的方法,在Abstrct class中必须定义方法体。
二是abstract class的子类在继承它时,对非抽象方法既可以直接继承,也可以覆盖;而对抽象方法,可以选择实现,也可以通过再次声明其方法为抽象的方式,无需实现,留给其子类来实现,但此类必须也声明为抽象类。既是抽象类,当然也不能实例化。
E. abstract class是interface与class的中介。
interface是完全抽象的,只能声明方法,而且只能声明public的方法,不能声明private及protected的方法,不能定义方法体,也 不能声明实例变量。然而,interface却可以声明常量变量,并且在JDK中不难找出这种例子。但将常量变量放在interface中违背了其作为接 口的作用而存在的宗旨,也混淆了interface与类的不同价值。如果的确需要,可以将其放在相应的abstract class或Class中。
abstract class在interface及Class中起到了承上启下的作用。一方面,abstract class是抽象的,可以声明抽象方法,以规范子类必须实现的功能;另一方面,它又可以定义缺省的方法体,供子类直接使用或覆盖。另外,它还可以定义自己 的实例变量,以供子类通过继承来使用。
final方法不能被子类的方法覆盖,但可以被继承。
final成员变量表示常量,只能被赋值一次,赋值后值不再改变。
final不能用于修饰构造方法。
注意:父类的private成员方法是不能被子类方法覆盖的,因此private类型的方法默认是final类型的。