顯示具有 JAVA-基本類 標籤的文章。 顯示所有文章
顯示具有 JAVA-基本類 標籤的文章。 顯示所有文章

2016年2月13日 星期六

Java 晉升 Promotion 與 轉型 Casting

一、晉升(Promotion)
    在宣告變數的資料型態其儲存範圍比指定的數值之資料型態為大時,
    則會發生數值型態晉升的動作
    簡單的說,就是較小的資料型態會自動提昇為符合變數之資料型態
    例:
    long money = 10;

二、轉型轉型(Casting)
    如果確定要將資料型態大的值指定給資料型態小的變數,
    則您必須明確進行轉型(Casting)的動作
    使用括號()包括目標資料型態的關鍵字
    例:
    long money = 10;
    int smallMoney = (int) money;























其它文章

Java 特性

這篇Java 特性是我整理下來的,
給大家參考參考。


一、Java程式
撰寫程式副檔名為.java ,編譯後為副檔名為.class

二、Java語言是簡單的(Simple)
參考其它程式語言(例如C/C++),並移去了較為複雜或不容易掌控的部份,                  
例如:指標(Pointer),goto語法。

三、Java 物件(Object)
是個支援物件導向程式設計(Object-oriented Programming)(OOP)的程式語言

四、Java 皆為物件Object理念
程式安全的(Secure)
驗證所有 Java 程式是否擁有合法的程式碼
禁止使用指標來操控記憶體

五、多執行緒的(Multi-threaded)
同一時間執行多個以上的事情

六、跨平台(Platform-independent)
最重要的特點就是跨平台運行,
Java原始碼程式(*.java)只需在Java虛擬機器上
編譯器(Compiler)生成為位元碼(位元組碼Bytecode )
(*.class)檔案,就可以在多種平台上不加修改地執行。

                                                          圖片來源網路找來的。
資料來源網路上整理來的,希望對大家有幫助學習。























其它文章

Java 程式的註解


一、Java 程式的註解說明
分為三種:
    單行註解 //
    多行註解 /* ….*/
    文件註解 /**….*/ (又稱doc 註解)


二、程式
public class HelloWorld{//類別區塊開始
   public static void main(String[ ] args){//方法區塊開始
  //程式區塊
    System.out.println("我第一支Java程式!!");
  }//方法區塊結束
}//類別區塊結束





















其它文章

初學 Java 的 HelloWorld 程式


撰寫一個JAVA程式

public class HelloWorld{
   public static void main(String[ ] args){
    System.out.println("我第一支Java程式!!");
  }
}

public class HelloWorld 宣告一個公用類別(class)物件
檔名.java需跟類別名稱一樣
main(String[] args)主要方法java程式進入點
System.out.println 輸出訊息到console
程式行需用 ; 當結束符號

初學 Java 的 HelloWorld 程式




















其它文章

2016年2月5日 星期五

Java Constructor 建構子方法的使用

前言:
本文相關用詞
類別物件 (class)
方法 (method)
參數 (parameter)
回傳值 (return value)

一、什麼是 Constructor
Constructor 稱為: 建構子、建構子方法、建構方法

簡說明:類別物件建立構造的方法
圖1

程式例:
public class TestDemo {
 public TestDemo() {//Constructor
  //這是基本Constructor
 }
}

你是否要在創建時,做一下些事情。
如果沒有也可以不寫Constructor。
例如: 當你沒寫Constructor,也是可以創建類別物件成功。
public class TestDemo {
}


註:
當你沒有一個Constructor,才會自動產生基本Constructor。


二、何時會用到呢?
當你建立類別物件的時後就會用到了。
圖2

以下程式 new TestDemo();
public static void main(String[] args) {
  TestDemo testDemo = new TestDemo();
}

就在new TestDemo();
的TestDemo()這個關鍵方法。
就是初時建立物件叫用TestDemo()的Constructor 。



三、可以很多個Constructor 嗎?
可以。但要不同的參數。

例如
package test;

public class TestConstructorDemo {
 int age;
 String name;
 private TestConstructorDemo() {//Constructor
  System.out.println("創建TestConstructorDemo");
 }

 public TestConstructorDemo(int age) {//Constructor
  this.age = age;
  System.out.println("創建TestConstructorDemo,初值建立 ,age : "+ age);
 }
 
 public TestConstructorDemo(String name) {//Constructor
  this.name = name;
  System.out.println("創建TestConstructorDemo,初值建立 ,name : "+ name);
 }
 
 public TestConstructorDemo(int age,String name) {//Constructor
  this.age = age;
  this.name = name;
  System.out.println("創建TestConstructorDemo,初值建立, age : "+ age+", name : "+ name);
 }
 
 public void msg(){
  System.out.println("age : " + age + ", name : " + name);
 }

 public static void main(String[] args) {
  System.out.println("創建物件測試1 開始:");
  TestConstructorDemo testDemo1 = new TestConstructorDemo();
  testDemo1.msg();
  System.out.println("創建物件測試1 結束----------------");
  
  System.out.println("創建物件測試2 開始:");
  TestConstructorDemo testDemo2 = new TestConstructorDemo(25);
  testDemo2.msg();
  System.out.println("創建物件測試2 結束----------------");
  
  System.out.println("創建物件測試3 開始:");
  TestConstructorDemo testDemo3 = new TestConstructorDemo("levin");
  testDemo3.msg();
  System.out.println("創建物件測試3 結束----------------");
  
  System.out.println("創建物件測試4 開始:");
  TestConstructorDemo testDemo4 = new TestConstructorDemo(25,"levin");
  testDemo4.msg();
  System.out.println("創建物件測試4 結束----------------");
  
 }
}


測試結果:
圖3



相關同類參考:


Java 快速導覽 - 物件導向概念 建構子



Java Gossip: 建構方法(Constructor)



下一篇:
Java this方法的使用









































其它文章

2015年5月28日 星期四

Java split 解決方法 之 直線 點

Java split 解決方法 之 直線 點


直線 | ,在 String 的 split 不用值接用
split("|")

點 . ,在 String 的 split 不用值接用
split(".")

例問題1程式:
package com.test;
public class TestSpilt1 {
 public static void main(String[] args) {
  String strData = "a|b|c|d";
  String[] strArr = strData.split("|");
  for(String str: strArr){
   System.out.println(str);
  }
 }
}

圖1


解決問題1程式1
package com.test;
public class TestSpilt1_1 {
 public static void main(String[] args) {
  String strData = "a|b|c|d";
  String[] strArr = strData.split("\\|");
  for(String str: strArr){
   System.out.println(str);
  }
 }
}

圖2

解決問題1程式2
package com.test;
public class TestSpilt1_2 {
 public static void main(String[] args) {
  String strData = "a|b|c|d";
  String[] strArr = strData.split("[|]");
  for(String str: strArr){
   System.out.println(str);
  }
 }
}

圖3



例問題2程式:
package com.test;
public class TestSpilt2 {
 public static void main(String[] args) {
  String strData = "a.b.c.d";
  String[] strArr = strData.split(".");
  for(String str: strArr){
   System.out.println(str);
  }
 }
}

圖4

解決問題2程式1
package com.test;
public class TestSpilt2_1 {
 public static void main(String[] args) {
  String strData = "a.b.c.d";
  String[] strArr = strData.split("\\.");
  for(String str: strArr){
   System.out.println(str);
  }
 }
}

圖5

解決問題2程式2
package com.test;
public class TestSpilt2_2 {
 public static void main(String[] args) {
  String strData = "a.b.c.d";
  String[] strArr = strData.split("[.]");
  for(String str: strArr){
   System.out.println(str);
  }
 }
}


圖6


參考
JAVA教學目錄

























其它文章

2014年3月25日 星期二

[JAVA]Java如何查看版本位元


使用 java -version
如果jdk是64位元,
在最後一行Java HotSpot(TM) 64-Bit ....
32位元
在最後一行
Java HotSpot(TM)  ....
如下圖






















其它文章

2014年2月19日 星期三

[JAVA]createTempFile產生暫存檔



使用createTempFile產生暫存檔

windows systems 預設檔案位置: C:\Users\USER\AppData\Local\Temp

UNIX systems 預設檔案位置 : "/tmp" or "/var/tmp" 






 private File tempFile;
    public void createFile(InputStream is) throws IOException {
        tempFile = File.createTempFile("tempFile", ".scv");
        OutputStream os = new FileOutputStream(tempFile);
        int n;
        byte[] buffer = new byte[1024];
        while ((n = is.read(buffer)) > -1) {
            os.write(buffer, 0, n); // Don't allow any extra bytes to creep in,
                                    // final write
        }
        os.close();
    }















其它文章

2013年2月21日 星期四

[JAVA]用二個變數,將值交換,用XOR在做swap功能



使用
int x = 10;

int y = 20;

以上兩個變數,

要如何不使用第三個變數將x與y的值互換,

即輸出結果為
x = 20,y = 10。

簡單的方法:
x = x+y;
y = x-y;
x = x-y;

可縮寫為:
x=x+y-(y=x);

用XOR的方法:
x = x ^ y;
y = x ^ y;
x = x ^ y;

可縮寫為:
x = (x = x ^ y) ^ (y = x ^ y);


XOR 的原理

語法
result = expression1 ^ expression2

Xor 運算子的語法具有以下幾個單元:

單元 說明
result 任意的數字變數。
expression1 任意的運算式。
expression2 任意的運算式。


如果運算式中只有一個的結果是 True,則 result 是 True。
然而,若運算式中有一個是 Null,則 result 也將會是 Null。
當兩個運算式都不是 Null,
 result 將根據下表來決定:

如果 expression1 為 且 expression2 為 則 result 為
True True False
True False True
False True True
False False False

Xor 可同時執行邏輯與位元運算子的運算。
兩運算式的位元比對會如下表所示,以"互斥-或"的邏輯來得到結果:

如果 expression1 為 且 expression2 為 則 result 為
0 0 0
0 1 1
1 0 1
1 1 0


^ 運算子會強制轉型以配合資料型別。
然後 ^ 運算子會檢查以二進位表示的兩個運算式值在兩運算式上執行位元互斥 OR 運算。
運算式資料型別決定此運算子傳回的資料型別。

     0101   (expression1)
^   1100   (expression2)
-----------------
    1001   (result)

二進位XOR快速記法:
    相同為0
    不同為1


其它網站參考:
http://msdn.microsoft.com/zh-tw/library/y2hf3412(v=vs.80).aspx

2013年1月19日 星期六

[JAVA]使用跳脫字元(Escape Sequence)



使用字串時,有一些特殊字元無法表示,可以將「\」搭配一些文字使用,來表達特殊字元。

如表:

跳脫字元
說明
\b
倒退字元(backspace)
\t
水平定位(tab)
\n
換行
\f
換頁 (form feed)
\r
歸位(return)
\’
單引號
\”
雙引號
\\
倒鈄線
\uhhhh
十六進制 hhhh 字元碼(h 0~7 的數字與 A~F 的英文字母)
所表示的文字碼(Unicode)
System.out.println("\u0041"); A
\ooo
八進制 ooo 字元碼(o 0~7 的數字)




















其它文章

2012年8月17日 星期五

[JAVA]流程控制 三元運算子 :?



三元判斷式    判斷式?成立程式區塊:不成立程式區塊

三元判斷式 ,
可以說是 if else 的簡寫版,
如果你的if else判斷是簡單的判斷,
可使用這三元判斷式,
多個if else if 判斷,
不建議使用三元判斷式,
因為會使程式複雜化。

// if()
  score = 70;
  if (score == 70) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score:" + score);
  }
  else {// 條件不成立 進入此程式區塊
   System.out.println("不條件成立  score:" + score);
  }
  
  //判斷式?成立程式區塊:不成立程式區塊 
  score = 70;
  System.out.println(score == 70 ? "條件成立  score:" + score : "不條件成立  score:" + score);
  


返回[JAVA]流程控制語法目錄

2012年8月8日 星期三

[JAVA]流程控制 if-else


說明:
一、

if(true){
//true 條件成立 進入此程式區塊
}

二、
if(true){
//true 條件成立 進入此程式區塊
}else{
//false 條件不成立  進入此程式區塊
}
三、
if( 條件判斷)
{
  條件判斷為true
}else{
 條件判斷為false
}

例:
// if() {} Test 1 -- 條件成立
  int score = 80;
  if (score >= 70) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 70:" + score);
  }
  // if() {} Test 2 -- 條件不成立
  score = 50;
  if (score >= 70) {// 不成立 條件 不進入此程式區塊
   System.out.println("條件成立 score >= 70:" + score);
  }
  // 不成立 條件 會跳過上述程式區塊

  // if() {} else {} Test 1 -- 條件成立
  score = 60;
  if (score >= 60) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 60:" + score);
  }
  else {// 條件不成立 進入此程式區塊
   System.out.println("不條件成立  score:" + score);
  }
  // if() {} else {} Test 2 -- 條件不成立
  score = 59;
  if (score >= 60) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 60:" + score);
  }
  else {// 條件不成立 進入此程式區塊
   System.out.println("不條件成立  score:" + score);
  }

  // if() {} else if() {} else {} Test 
  score = 59;
  if (score >= 60) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 60:" + score);
  }
  else if (score == 59) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score == 59:" + score);
  }
  else { // 條件都不成立 進入此程式區塊
   System.out.println("不條件成立  score:" + score);
  }
  // if() {} else if() {} else {}  可以很多個else if()  Test 
  score = 70;
  if (score >= 90) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 90:" + score);
  }
  else if (score >= 80) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 80:" + score);
  }
  else if (score >= 70) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 70:" + score);
  }
  else if (score >= 60) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 60:" + score);
  }
  else if (score >= 50) {// 條件成立 進入此程式區塊
   System.out.println("條件成立  score >= 50:" + score);
  }
  else { // 所有條件都不成立 進入此程式區塊
   System.out.println("不條件成立  score:" + score);
  }


返回[JAVA]流程控制語法目錄

2011年12月26日 星期一

[JAVA]一元運算子、二元運算子、三元運算子、=運算子




一元運算子:
             只有一個運算子,如:-2

二元運算子:
           有兩個運算子,如:4+5

三元運算子
          有三個運算元的運算子。
          如: (A < 0) ? "true":"false"

=運算子
        如: a = 5+5;
                 5+5是二元運算子
                 a=是指派a等於5+5的值
                 a=10















其它文章

[JAVA]運算子-運算子的優先順序


優先值 
運算子
同等順序
1
括號:()[]
由右至左
2
遞增++、遞減--、負號-NOT!、補數~
由左至右
3
乘*、除/、取餘數%
由左至右
4
+、減-
由左至右
5
位元左移<<、位元右移>>
無正負性位元右移>>>
由左至右
6
小於<、大於>、小於等於<=、大於等於>=
由左至右
7
等於==、不等於!=
由左至右
8
AND: &
由左至右
9
XOR:^
由左至右
10
OR:|
由左至右
11
簡化比較次數的AND:&&
由左至右
12
簡化比較次數的OR:||
由左至右
13
條件選擇?:
由右至左
14
指定運算 =
由右至左
15
+=-=、*=/=%=&=|=^=
由右至左
優先值:1代表最高優先值 ,15代表最低優先值 。
同等順序:指運算式中遇到同等級優先值時的運算處理。














其它文章

[JAVA]運算子-指定運算子(Assignment Operators)


指定運算子
用途
例子:A=5
相等式子
運算結果
註解
+=
A+=3+2
A=A+(3+2)
A=10
先運算+=右邊的式子,再和A相加
-=
A-=5-4
A=A-(5-4)
A=4
先運算-=右邊的式子,再和A相減
*=
A*=2*3
A=A*(2*3)
A=30
先運算*=右邊的式子,再和A相乘
/=
A/=10/5+3
A=A/(10/5+3)
A=1
先運算/=右邊的式子,再和A相除
%=
求餘數
A%=15%4
A=A%(15%4)
A=2
先運算%=右邊的式子,再和A求餘數


指定運算子
用途
例子:A=5
相等式子
運算結果
註解
&=
AND
A &= 5-3
A =A&(5-3)
A=0
先運算&=右邊的式子,再和AAND運算
|=
OR
A |= 2
A=A|2
A=7
先運算|=右邊的式子,再和AOR運算
^=
XOR
A^=2+1
A=A^(2+1)
A=6
先運算^=右邊的式子,再和AXOR運算

















其它文章

標籤

Oracle (150) Oracle DB (144) Oracle_DB (143) Oracle SQL (135) JAVA (84) css-基本類 (65) MySQL (59) CSS Selector (58) jQuery (49) JavaScript-基本類 (39) Spring Boot (38) JavaScript (37) JavaScript HTML DOM (37) JavaScript-HTML_DOM (36) CSS3 (30) JAVA-基本類 (28) jQuery UI (27) Apache (23) Oracle GROUP BY (20) datepicker (20) Android (18) Oracle Date (17) c (17) JAVA-lang套件 (16) Linux (16) Oracle Sub Query (16) Spring-基本類 (16) jQuery-基本類 (16) MySQL-進階系列教學 (15) Android基本類 (14) Grails (14) Oracle join (14) SQLite (13) Spring (13) WIN7-基本類 (13) grails-基本類 (13) linux cent os (13) CKEditor (12) JAVA-流程控制類 (12) JAVA_Spring (12) PHP (11) Spring MVC (11) MySQL-基本系列教學 (10) Notepad (10) Notepad++ (10) SQLite for java (10) Windows (10) c/c++ (10) eclipse (9) jQuery-Selector (9) sqldeveloper (9) DB_Toad (8) JAVA_IDE_Eclipse (8) JavaScript-String類 (8) MySQL DB Toad (8) MySQL-DATE相關 (8) MySQL-函式相關 (8) Spring Bean (8) Android Studio (7) HTML5 (7) Hibernate (7) JAVA-OCWCD (7) JavaScript-陣列類 (7) Docker (6) JAVA-程式分享 (6) JAVA.util套件 (6) JavaScript-數學類 (6) MinGw (6) MySQL-其它類 (6) Servlet (6) centos (6) Apache_Tomcat (5) Apache套件_POI (5) CSS (5) JavaScript-Date物件 (5) JavaScript-其它類 (5) PostgreSQL (5) httpd (5) log4j (5) 基本資訊 (5) 開發工具 (5) CSS Properties (4) Dev-C++ (4) IntelliJ IDEA (4) Oracle DDL (4) Sublime (4) TortoiseSVN (4) apache_Maven (4) Android NDK (3) Eclipse IDE for C/C++ (3) Hibernate-基本類 (3) JAVA-問題 (3) JAVA-綀習分享 (3) JVM (3) Linux 指令 (3) Proxy Server (3) Spring Mobile (3) Spring web (3) Squid (3) VirtualBox (3) maven (3) zk (3) 生活其它 (3) Bootstrap (2) Filter (2) JAVA_IO (2) JAVA_其它_itext套件 (2) JBoss-問題 (2) JSP (2) Jboss (2) Listener (2) MySQL-語法快速查詢 (2) Spring AOP (2) Spring Batch (2) Spring Boot Actuator (2) Spring i18n (2) Subversive (2) Tomcat 8 (2) UML (2) WebJars (2) WinMerge (2) c++ (2) c語言綀習題 (2) jQuery Mobile (2) jQuery-事件處理 (2) jQuery-套件類 (2) putty (2) svn (2) weblogic (2) Apache_JMeter (1) Apache套件_BeanUtils (1) Apache套件_StringUtils (1) Base64 (1) Google API (1) HTML5-基本類 (1) Heap (1) JAVA 7 (1) JAVA SE 、JAVA EE、JAVA ME (1) JAVA 日期 (1) JAVA-OCJP (1) JAVA-WEB (1) JAVA_IDE (1) JAVA其它 (1) JBoss Server (1) JDK (1) JMX (1) JRE (1) Java RMI (1) Java String (1) Joda Time (1) Linux_其它 (1) MySQL教學 (1) Oracle_VirtualBox (1) SQL Server (1) SWT (1) Session (1) Stack (1) Struts 2 (1) Tool (1) ZK Studio (1) csv (1) grails-其它類 (1) jQuery-進階 (1) java mail (1) java web (1) java8 (1) jsoup (1) mockmvc (1) modules (1) tomcat (1) win10 (1) 其它類 (1) 圖片工具 (1) 模擬器 (1) 讀書分享 (1) 開發資訊 (1)

精選文章

初學 Java 的 HelloWorld 程式

撰寫一個JAVA程式 public class HelloWorld{ public static void main(String[ ] args){ System.out.println("我第一支Java程式!!"); } } ...