顯示具有 Java 標籤的文章。 顯示所有文章
顯示具有 Java 標籤的文章。 顯示所有文章

[Java] Primitive type 基本型態的長度

Java 的型態分為 Primitive type 基本型態與 Class type 類別型態

基本型態分為
整數
short 2位元組
int 4位元組
long 8位元組

位元組
1位元組,可表示 -128到127的整數

浮點數
float 4位元組
double 8位元組

字元
char 2位元組

布林
true/false

Java 編譯器 Javac 參數列表

-sourcepath 告訴編譯器原始碼所在位置
-d 指定編譯完成的位元碼(.class) 存放的資料夾
-verbose 輸出編譯的過程
-classpath 告訴編譯器到何處尋找先前已編譯好的位元碼存放位置
-source 檢查使用語法是否有超過指定的版本號
-target 指定編譯出來的位元碼,符合指定平台允許的版本號 (-target 必須大於等於 -source)
-bootstrap 指定類別載入器,搭配source與target使用,類別載入器會參考到rt.jar,可避免因為舊版JRE沒有新API造成無法執行的情形。
-version 執行版本資訊


範例
javac -sourcepath src -d classes src/Main.java

說明
使用java編譯器javac,編譯src/Main.java,到sourcepath指定的src目錄下尋找有用到原始檔,編譯完畢將檔案放置在classes目錄下

完整參數列表
Usage: javac <options> <source files>
where possible options include:
  -g                         Generate all debugging info
  -g:none                    Generate no debugging info
  -g:{lines,vars,source}     Generate only some debugging info
  -nowarn                    Generate no warnings
  -verbose                   Output messages about what the compiler is doing
  -deprecation               Output source locations where deprecated APIs are u
sed
  -classpath <path>          Specify where to find user class files and annotati
on processors
  -cp <path>                 Specify where to find user class files and annotati
on processors
  -sourcepath <path>         Specify where to find input source files
  -bootclasspath <path>      Override location of bootstrap class files
  -extdirs <dirs>            Override location of installed extensions
  -endorseddirs <dirs>       Override location of endorsed standards path
  -proc:{none,only}          Control whether annotation processing and/or compil
ation is done.
  -processor <class1>[,<class2>,<class3>...] Names of the annotation processors
to run; bypasses default discovery process
  -processorpath <path>      Specify where to find annotation processors
  -parameters                Generate metadata for reflection on method paramete
rs
  -d <directory>             Specify where to place generated class files
  -s <directory>             Specify where to place generated source files
  -h <directory>             Specify where to place generated native header file
s
  -implicit:{none,class}     Specify whether or not to generate class files for
implicitly referenced files
  -encoding <encoding>       Specify character encoding used by source files
  -source <release>          Provide source compatibility with specified release

  -target <release>          Generate class files for specific VM version
  -profile <profile>         Check that API used is available in the specified p
rofile
  -version                   Version information
  -help                      Print a synopsis of standard options
  -Akey[=value]              Options to pass to annotation processors
  -X                         Print a synopsis of nonstandard options
  -J<flag>                   Pass <flag> directly to the runtime system
  -Werror                    Terminate compilation if warnings occur
  @<filename>                Read options and filenames from file

[Java] 字串比較Method compareTo

compareTo 可用來比較兩字串是否相同,它屬於 Class String。當回傳值為0時,表示兩字串相等。

範例
String string1;
String string2;
if (string1.compareTo(string2) == 0)
兩字串相等
else
兩字串不相等


  • compareTo

    public int compareTo(String anotherString)
    Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns0 exactly when the equals(Object) method would return true.This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:
     this.charAt(k)-anotherString.charAt(k)
     
    If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case,compareTo returns the difference of the lengths of the strings -- that is, the value:
     this.length()-anotherString.length()
     
    Specified by:
    compareTo in interface Comparable<String>
    Parameters:
    anotherString - the String to be compared.
    Returns:
    the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.

Object Class

Class Overview


The root class of the Java class hierarchy. All non-primitive types (including arrays) inherit either directly or indirectly from this class.

Public Constructors
Object()
Constructs a new instance of Object.

能改寫的Public Methods 有equals(Object o),hashCode(),toString()。其中equals和hashCode要同時改寫。
Public Methods
booleanequals(Object o)
Compares this instance with the specified object and indicates if they are equal.
final Class<?>getClass()
Returns the unique instance of Class that represents this object's class.
inthashCode()
Returns an integer hash code for this object.
final voidnotify()
Causes a thread which is waiting on this object's monitor (by means of calling one of the wait() methods) to be woken up.
final voidnotifyAll()
Causes all threads which are waiting on this object's monitor (by means of calling one of the wait() methods) to be woken up.
StringtoString()
Returns a string containing a concise, human-readable description of this object.
final voidwait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
final voidwait(long millis, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires.
final voidwait(long millis)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires.

Protected Methods
Object    clone()
Creates and returns a copy of this Object.
void    finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.



Java this與super關鍵字 (草稿)

this 表示物件本身
super表示父類別

使用super的時機在於,當父類別的成員被遮蔽或者覆寫時,子類別會無法使用父類別中的成員,此時可以透過使用super關鍵字解決此類問題。

使用this的時機在於你要引用物件本身的方法、成員或者類別本身的建構子。

this只能在類別中使用,他儲存的是物件本身的參考。Java程式編譯時會在成員加上this關鍵字。

在類別中呼叫this(),表示要執行此類別的建構子(Contructor)。





Java Access Control 存取修飾元 private, protected, public

資料來源:http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

publice:全部人都可以存取
protected:同一個Class,同一個Pacakage,相同Package的子類別,不同Package的子類別可以存取
no modifier(Default):同一個類別,同一個Package可以存取
private:只有同一個類別才可以存取





Jave annotation @Override

參考資料
http://en.wikipedia.org/wiki/Java_annotation
http://docs.oracle.com/javase/tutorial/java/annotations/

Annotation 中文稱為註解

在Java language中 Annotation有以下幾種

Annotations applied to Java code:
@Override- Checks that the method is an override. Causes a compile error if the method is not found in one of the parent classes or implemented interfaces.
@Deprecated - Marks the method as obsolete. Causes a compile warning if the method is used.
@SuppressWarnings - Instructs the compiler to suppress the compile time warnings specified in the annotation parameters.
@SafeVarargs - Suppress warnings for all callers of a method or constructor with a generics varargs parameter, since Java 7.
@FunctionalInterface - Specifies that the type declaration is intended to be a functional interface, since Java 8.

Annotations applied to other annotations:
@Retention - Specifies how the marked annotation is stored—Whether in code only, compiled into the class, or available at runtime through reflection.
@Documented - Marks another annotation for inclusion in the documentation.
@Target - Marks another annotation to restrict what kind of Java elements the annotation may be applied to.
@Inherited - Marks another annotation to be inherited to subclasses of annotated class (by default annotations are not inherited to subclasses).
@Repeatable - Specifies that the annotation can be applied more than once to the same declaration.


其中在開發Android APP最常見的莫過於@Override。我們需要覆寫某個父類別的方法時,可以在Method前加上@Override,如此編譯器在編譯時會確認覆寫方法的命名,是否與父類別一致,如果不一致便會產生Error提醒我們,因為名稱不一致就沒有達到覆寫的目的。