***************************************************************************************************************************************************************************************
Java 数据类型
Java变量的命名: 开头只能是 $ _ 或是 字母 其他的部分只能是 $ _ 字母 或是 数字 (不能与关键字重复,不能有其他的符号)
数据类型的大小:
boolean 1bit
byte: 8-bit
short: 16-bit
char: 16-bit
int: 32-bit
long: 64-bit
float: 32-bit
double: 64-bit
如果一个变量在method里面声明 则必须手动进行初始化赋值
如果一个变量在method外面声明 则其自动进行初始化赋值
Boolean初始化自动赋值为false
在创建JAVA数组的时候 会自动给其元素赋值
***************************************************************************************************************************************************************************************
(-15)/(-5) = 3
(-15)%(-5) = 0
15 / (-5) = -3
(-15) / 5 = -3
15 % (-2)= 1
****************************************************************************************************************************************************************************************
Passing arguments into method
方法的传递参数有两种类型 primitive variables(原始类型) 或 reference variables (引用类型)
两种类型的参数都是变量的一个copy 并不是变量的本身
void ModifyStu(int i) // primitive variables
{
i = i + 10; //不会影响原始传递数据
}
void ModifyRef(Student st) // reference variables 可以改变原始的实例的properties
{
st.setScore(50);
}
Java中跳转到外循环的方法 (Continue)
OuterLoop: for(int a=0;a<5;a++)
{
for(int i=0;i<10;i++)
{
if(i==5)
{
continue OuterLoop;
}
System.out.println(i);
}
}
*****************************************************************************************************************************************************************************************
A static method cannot access non-static members of the class. 一个静态方法只能访问静态变量
****************************************************************************************************************************************************************************************
关于java继承(Inheritance)
// Super Class
class A {
A()
{
}
A(String message) {
System.out.println(message + " from A.");
}
}
//Sub-Class
class B extends A{
B() {
System.out.println("Hello from B.");
}
B(String message)
{
super(message); // Call Super class's constructor(This one must be in the first line of subclass constructor)
this(); // call other constructor in current class
System.out.println(message + " from BBB.");
}
}
class ucd3 {
public static void main(String[] args) {
B b = new B("hello");
}
}
注意: 1, 父类中必须包含默认构造函数
2,子类中构造函数call父类的时候 必须将super(xxx)放在第一行
3, 使用this() 可以在一个constructor里去调用另外一个本类的constructor
4,如果一个类不是父类 则可以不写constructor 编译器会自动添加default的constructor
5, Constructor不会被子类继承 如果一种constructor只有父类有 则子类不能使用
6, 如果子类的constructor没有添加super(xxx),则编译器会自动添加一个默认的 super();
Class A{}
Class B extends A{}
The Instance could be: A a = new B(); // 将classB转换为ClassA 但是其实a 仍然是B的一个object, 但是其也可以使用父类的元素
class SuperClass {
SuperClass() {
System.out.print(" I was in Super Class." );
}
protected void aMethod (int i) {
System.out.print (" The value of i is " + i );
}
protected void haha(int i)
{
System.out.print (" haha i is: " + i );
}
}
class SubClass extends SuperClass {
protected void aMethod(int j) {
System.out.print (" The value of j is " + j );
}
}
class ucd4 {
public static void main(String args[]) {
SubClass sub = new SubClass();
sub.aMethod(5);
System.out.print ("******************");
SuperClass sup = new SubClass();
sup.aMethod(5);
sup.haha(5);
}
}
****************************************************************************************************************************************************************************************
Java中 Interface 的 implement:
一个class可以implement 一个或多个 interfaces
一个 interface 也可以 implement 一个或多个 interfaces(使用extend)
interface只能implement interface 但是不能 extend class
如果一个类既extend一个类 又implement一个interface 则 extend必须在implement的前面 class A extend B implement C
关于java的接口(Interface)
interface 中声明的所有变量(variables)必须都是 public, final, static 之一的
interface中声明的所有的methods都是 public 或是 abstract的
Interface 中的变量 不能被实现其的类进行修改 只能使用
****************************************************************************************************************************************************************************************
Enum 枚举的格式 正确与不正确
//Correct
A. enum Day {Sunday, Monday, Tuesday}
//Illegal
B. enum Day {Sunday, Monday, Tuesday,
private String holiday;
}
//Correct
C. enum Day {Sunday, Monday, Tuesday;
private String holiday;
}
//Illegal
D. enum Day {private String holiday;
Sunday, Monday, Tuesday;
}
//Correct
E. enum Day {Sunday, Monday, Tuesday;
private String holiday;
Day(){
System.out.println("Hello");
}
Another sample of complex Enum
enum wasitsize{
Small(30), Medium(34), Large(40);
private int size;
wasitsize(int size) //constructor
{
this.size = size;
}
public int getSize(){
return size;
}
}
注意: 在Enum中 int的标准成员必须放到前边 其他的放在后边 中间用“;” 进行隔开
Enum可以像class一样 有 构造函数 方法 还有 字段
实例化一个Enum不能使用new, 例如上边的例子: wasitsize ws = wasitzie.Small;
*****************************************************************************************************************************************************************************************
内部类 (Nested Class)
一个类在另外一个类的里面, 有两种分类
1,Inner Class 嵌入类不是静态类型的
2,Static Nested Class 嵌入类是静态类型的
Inner Class
一个 Inner Class的实例只能生存在其外部类的实例中
一个inner class可以自由访问外部类的成员 即使是private
不能定义 一个 static成员在 inner class中
Static Nested Class
Static Nested Class 不能直接访问外部类中的 非静态的 变量 与 方法
The non-static variables or methods of an outer class cannot be directly accessed from inside a
static nested class of the outer class.
sample:
Class MyTopLevel{
。。。。。。。。
Class MyInner{
。。。。。。。。。
}
}
实例化内部类(非静态)
Class Test{
public static void main(String[] args)
{
//内部类的实例化的方法(非静态)
MyTopLevel mt = new MyTopLevel();
MyTopLevel.MyInner inner = mt.new MyInner();
//第二种实例化方法
MyTopLevel.MyInner inner = new MyTopLevel().new MyInner();
}
}
实例化内部类(静态)
public class MyOuterClass {
public static class MyNestedClass {
}
}
注意此时 内部类为 静态类
实例化方法:MyOuterClass.MyNestedClass mn = new MyOuterClass.MyNestedClass();
*****************************************************************************************************************************************************************************************
Classes Defined Inside Methods
嵌入一个方法中的类 可以访问所有所以的变量
Variables in the enclosing method are accessible only if those variables are marked
final
一个类如果定义在一个方法内
1, 这个内部类可以访问外部类的所有变量
2, 但是这个被嵌入类的方法中变量 只有final的 才能被内部类进行访问
*****************************************************************************************************************************************************************************************
Anonymous Class(匿名缺省类)
An anonymous inner class may either implement a single interface or extend a parent class other
than Object, but not both.(匿名类 只能实现一个接口 或是 继承一个类 但是不能同时进行)
****************************************************************************************************************************************************************************************
Java中的transient
- class T {
- transient int a; //不需要维持
- int b; //需要维持
- }
如果T类的一个对象写入一个持久的存储区域,a的内容不被保存,但b的将被保存。
*****************************************************************************************************************************************************************************************
JAVA类型转换(内置类型) 显式 与 隐式
隐式转换的一般规则是:
1, 不能将boolean和 非boolean类型的变量进行转换
2, 想要转换成的类型的size必须大于或者等于 被转换的变量
3, Short 和 Char 类型都是16bit 但是 他们不能进行隐式转换
****************************************************************************************************************************************************************************************
JAVA object类型的转换
1, 一个class要转换为另外一个class 则这个class必须是另外一个class 的subclass
2, 一个class要转换成一个interface 则这个class必须implement这个interface
*****************************************************************************************************************************************************************************************
OverLoad (重载)
两个重载的方法中的参数不能有同样的顺序
public int myMethod (double a, int i)
A. public int myMethod(int i, double a) { } // 合法
B. public double myMethod(double b, int j){ } // 不合法, 参数顺序相同
C. public int myMethod(double a, double b, int i){ } // 合法 参数不同
D. public double myMethod(int i, double b) {} //合法, 参数顺序不同
两个重载的方法的返回值类型 可以相同 也可以不同
public float aMethod(float a, float b)
A. public int aMethod(int a, int b) { } // YES
B. public float aMethod(float a, float b) { } // It is an override
C. public float aMethod(float a, float b, int c) throws Exception { } //YES
D. public float aMethod(float c, float d) { } // It is an override
E. private float aMethod(int a, int b, int c) { } // YES
*****************************************************************************************************************************************************************************************
Override (覆盖)
Override 的返回类型必须一样
Override的 参数类型的顺序必须一样
Override的 参数数量必须一致
Override的 access modifier 必须相同或是更开放 (例如: 父类中的方法是 protected, 则覆盖的方法需要是 public 或是 protected
父类中的方法是public, 则覆盖的方法必须是public)
Override的抛出异常(Throw Exception) 只能比父类少 比如 如果父类一个方法抛出的是 IOException 则如果子类覆盖方法抛出了整个Exception 就会出错
Valid override List
*****************************************************************************************************************************************************************************************
Constructors(构造函数)
如果一个类中有一个(或以上)手动设定的constructor, 则编译器不会自动创建默认构造函数
如果一个类中没有任何的constructor, 则编译器在运行时会自动创建一个默认的构造函数
*****************************************************************************************************************************************************************************************
.Equals() 与 == 的区别
.Equal() 两个object的值相等即可
== 不仅值必须相等 地址也要相等
Integer i = new Integer(10);
Integer j = i;
if(i==j)
System.out.println("i==j is true");
else
System.out.println("i==j is flase");
if(i.equals(j))
System.out.println("i equal j is true");
else
System.out.println("i equal j is false");
//################################################
Integer ii = new Integer(10);
Integer jj = new Integer(10);
if(ii==jj)
System.out.println("ii==jj is true");
else
System.out.println("ii == jj is false");
if(ii.equals(jj))
System.out.println("ii equal jj is true");
else
System.out.println("ii equal jj is false");
OUtPUT:
*****************************************************************************************************************************************************************************************
Access Modifier 访问控制符 && Usage modifier 使用控制符
Access Modifier: public, private, protected, default
Usage Modifier: abstract,static, final, native, transient
Final: 可以被 class, method 或是 variable进行使用
final class: 不能被继承(can not be extended)
final variable: 值不能被修改
final method: 不能被重写(override)
Static:可以被 variable , method, 或是 code block inside a class
不能被 top-level class 或是 class constructor 进行使用
一个static的元素(变量 variable)可以被该类的所有instance(实例化后的对象)进行访问
一个static的method只能被该类中static成员进行访问, 一个static的method不能被override成一个non-static的method (一个静态方法只能被静态方法进行override)
一个static block of code 会在class load time 进行执行 所以 一个类会执行完所有的static 区域后 才会进入主函数
Abstract(抽象) 只能用于class 或是 方法, 不能用于变量
abstract class不能被实例化
abstract class不需要必须有abstract method
abstract class 可以有finalize()方法
一个final类 不能有 abstract method
Native:
Native只能用于method
用于一个方法存在于JVM(java虚拟机)以外的library中, 或是其他的语言 如c++
transient: 只能用于variable 变量
A variable declared transient is not saved during serialization.
一个构造函数(Constructor)只能使用access modifier
一个类不能同时拥有两个usage modifier, 例如 abstract final class a{} 就是违法的 (private protected class xxx 是合法的)
与threads(多线程)相关的modifier: Volatile synchronized
*****************************************************************************************************************************************************************************************
JAVA中的垃圾处理(garbage collection)
Runtime rt = Runtime.getRuntime();
rt.freeMemory();
rt.gc(); // Invokes the garbage collection
garbage collection 不能被强制执行
Object中 使用 finalize() 进行垃圾处理
*****************************************************************************************************************************************************************************************
Static import(静态导入)
在一个类中导入其他的包中的类是, 使用其中的变量和方法 就像在本地定义一样
import static java.lang.System.out;
import static java.lang.Integer.*;
public class TestStaticImport {
public static void main(String[] args) {
out.println(MAX_VALUE);
out.println(toHexString(42));
}
}
静态导入之后 就不用写System.out.println......
*****************************************************************************************************************************************************************************************