- 博客(27)
- 收藏
- 关注
原创 Java enumerated types枚举类的创建和遍历
枚举(enum)类型是Java 5新增的特性,它是一种新的类型,允许用常量来表示特定的数据片段,而且全部都以类型安全的形式来表示。Example:public enum Color { Red, Green, Blue }public class ColorEnumDriver { public static void main(String[] args) { /* Poss
2018-01-18 15:33:05
775
原创 Java abstract class/method——抽象类/方法的应用
如何理解抽象类?举个例子,比如创建的母类名为“Shape”,它有子类“Circle”和“Rectangle”。母类定义了一个方法叫“drawMe()”,“画我”。然而圆和正方形都可以画出来,但是他们的母类,并不拥有一个能够画出来的具体形状。因此,母类的drawMe()无意义。但是作为要被子类继承的一种方法,它必须存在。这时我们就可以将母类定义为“抽象类”"abstract
2017-12-29 16:17:32
4716
原创 Java inheritance的upcasting和downcasting
upcasting一般是安全的,因为子类继承了母类所有的方法,不会报错。downcasting一般不被允许,非法downcasting会有run time error: ClassCastException。Example:/* This code generates a java.lang.ClassCastException */public class UpCastingDo
2017-12-29 14:58:48
1163
原创 Java late binding后期绑定与polymorphism多态的结合
若没有学习继承,可先前往以下网址先学习inheritance:inheritance继承类应用与子母类访问权限讨论Motivation: Consider the following example:Faculty carol = new Faculty("Carol Tuffteacher", 458, 1995);Person p = carol;System.out.pr
2017-12-29 14:16:55
579
原创 Java two dimensional array
two dimensional array 拥有两个不同的内存地址int[][] a = new int[3][];a[0] = new int[2];a[1] = new int[3];a[2] = new int[4];a[1][2] = 5;
2017-12-29 14:02:50
291
原创 Java inheritance继承类应用与子母类访问权限讨论
• Inheritance: is the process by which one new class, called the derived class, is created from another class, called thebase class.• The derived class is also called: subclass orchild class•
2017-12-29 13:36:30
567
原创 Java 将equals override为比较所有object的方法
Java 默认的equals只能比较同类型的object,一旦比较对象类型不同就会报错。下面是当类型不同时返回false避免报错的方法。Example:public class Student implements Comparable { private String name; private int id; public Student(String name, int
2017-12-29 13:00:38
1181
原创 Java switch的用法与case的穿透现象举例详解
switch和if-else的用法类似:if(x == 3) { }else if(x == 30) { }else if(x == -6) { }else { }等同于switch的:switch(x) {case 3: break;case 30: break;case -6: break;default: }如果switch的case中不包含
2017-12-28 15:04:07
6344
原创 Java运算符优先级
Unary Operator: !, ++, --, -, +;(此处+-代表特性,例如-a, +b)multiplicative: *, /, %;addition: +, -;comparative: , =;equality: ==, !=;and: &&;or: ||;assignment: %=, /=, *=, -=, +=, =;(以上优先级依
2017-12-28 14:59:08
222
原创 Java一行代码可声明多个同类变量
Java支持一句语句声明多个同类变量。Example:String a = "Hello", c = "hello";int x = 5, y = 5;
2017-12-28 14:57:34
40351
1
原创 Java 方法重载和重写
Java中的同一个class里可以有同名method,但是method的parameter类型必须不同。public void f(int x) { } public void f(float x) { }当输入为3,(3既为int也是float),both feasible,这是我们选择最先出现的method(第一个)。public void f(float x) { }
2017-12-28 14:08:19
228
原创 Java two dimensional array
two dimensional array 拥有两个不同的内存地址int[][] a = new int[3][];a[0] = new int[2];a[1] = new int[3];a[2] = new int[4];a[1][2] = 5;Example:package twoDimensionalArrays;public class Init
2017-12-27 10:49:59
1168
原创 Java深拷贝和浅拷贝举例详解
浅拷贝将原来对象的内存地址复制给一个新的对象,但不创建新的object。而深拷贝创建新的对象,并将需要拷贝的值复制到新的object里。Example:package CDExamples;public class CDCollectionOwner { private String name; private CD[] myFavorites; public CDCo
2017-12-27 10:24:21
347
原创 Java String和StringBuffer的区别
简要的说, String 类型和 StringBuffer 类型的主要性能区别其实在于 String 是不可变的对象, 因此在每次对 String 类型进行改变的时候其实都等同于生成了一个新的 String 对象,然后将指针指向新的 String 对象,所以经常改变内容的字符串最好不要用 String ,因为每次生成对象都会对系统性能产生影响,特别当内存中无引用对象多了以后, JVM 的 GC
2017-12-25 13:20:30
704
原创 Java try catch throw exception 和 finally用法举例详解
Example:import javax.swing.*;public class DateReader { public static int getYear(String d) { String yearString = d.substring(6, 10); System.out.println("Done with substring method..."); r
2017-12-25 12:26:04
3192
原创 Java run-off error(on float and double)
We can't store real 3.9 in Base 2.double a = 3.9;boolean b = false;if(a == 3.9){ b = true;}System.out.println(b);输出为:false(3.9)Base 10 = (1 1 . 1 1 1 00 1 1.
2017-12-25 11:38:37
288
原创 Java 变量x++ 和 ++x的区别
若为x++, 那么先执行x在本语句中的任务,执行完毕后+1。若为++x,则先+1然后再执行x在本语句中的任务。Example 1: (++x)int x = 7;System.out.println(++x);System.out.println(x);输出为:88Example 2: (x++)int x = 7;System.out.println(x+
2017-12-24 15:28:00
7929
原创 Java do-while的使用
do-while是while的特殊种类,do-while会先运行一次do里的代码,然后进行while的Boolean判定,然后返回按照do里的方法重新运行。(先执行,后判断的while loop)Example:public class SimpleDoWhile { public static void main(String[] args) { int index = 1
2017-12-24 15:09:50
19083
原创 Java final变量的声明和使用
final变量(never be changed):final int NUMBER_OF_CATS = 10;//never be changedfinal double PI = 3.1415927;final NUMBER_OF_WHEELS = 2;
2017-12-24 15:00:23
3512
3
原创 Java标识符的命名规则
Identifier:A-Z; a-z; 0-9; $ dollar sign; "_" underscore;Can't begin with a digit!Avoid keywordsCapitalization: Camel Case(驼峰式)Camel Case 驼峰式Variable/MethodClassnumberOfCats
2017-12-24 14:41:20
487
原创 Java Scanner的简单应用
Scanner 可以用于检测并捕捉键盘输入的各种字符串。Example 1:import java.util.Scanner;public class Scanner1 { /** * Shows basic use of the scanner */ public static void main(String[] args) { Scanner keyboa
2017-12-24 14:29:23
421
原创 Java中的除法结果与除数被除数的类型有关
3 / 4 = 07 / 3.0 = 2.333333.0 / 4.0 = 0.757 / 3 = 2
2017-12-24 13:58:43
3322
原创 Java数据类型内存大小与hierarchy
long - 8 bytesint - 4 bytesshort - 2 bytesdouble - 8 bytesfloat - 4 bytes
2017-12-24 13:55:11
475
原创 计算机语言简史
1950-1960Fortran - math/scienceCobol - businessLisp - AI1970'sPascal - education(elegant)early 1980'sC - (old fashion & simple)late 1980'sC++ - object oriented lang
2017-12-24 12:48:55
208
原创 进制转换方法大全
任意进制转十进制e.g. (253)Base 6 -> (?)Base 10= 2*6^2+5*6^1+3*6^0=105(136)Base 6 -> (?)Base 10= 1*7^2 + 3*7^1 + 6*7^0= 76十进制转任意进制e.g (76)Base 10 -> (?)Base 776/7 = 10······610/7 = 1······3
2017-12-24 11:58:11
3024
原创 内存的单位转换(从bit到terabite)
bit(字节) - either 1 or 0byte(B) - sequence of 8 bitskilobyte(KB) - 1024 bytes = 2^10 bytesmegabyte(MB) - 2^20 bytesgigabyte(GB) - 2^30 bytesterabyte(TB) - 2^40 bytes
2017-12-24 11:10:46
1538
原创 Java基础知识缩写大全
CPU - Central Processing UnitIDE - Integrated Development EnvironmentCVS - Concurrent Versioning SystemMain Memory: RAM - Random Access MemorySecondary Memory - hard drive(permanent), CD, DVD
2017-12-24 10:55:00
552
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人