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

2014年1月6日

Java: Byte Array to Int, Int to Byte Array

Java 對於 二進制 是採用2的補數方式,

1byte為8bit,也就是說可表示數字範圍為 -128 ~ +127

由於int為4byte,所以當byte轉成int的時候會被自動擴充成4個byte,

擴充的部分就由最左邊的bit一直擴充下去。

ex: 0x10 -> 0000 0000 0000 0000 0000 0000 0001 0000 -> 16

ex: 0x80 -> 1111 1111 1111 1111 1111 1111 1000 0000 -> -128

所以當我們想從byte得到正整數的時候就會先& 0x000000ff,讓擴充的部分為0。



byte b=(byte) 0x80;
int i=b;
System.out.println(i);
輸出: -128

byte b=(byte) 0x80;
int i=b&0x000000ff;
System.out.println(i);
輸出: 128

Byte Array to Int:
public static int ByteArray2Int(byte[] b){
int value=0;
   for(int i=3;i>-1;i--){
      value+=(b[i]&0x000000ff)<<(8*(4-1-i));
   }
return value;
}

Int to Byte Array
private byte[] InttoByte(int Num, int ByteSize) {
 byte[] OutByte = new byte[ByteSize];
 for (int i = 0; (i < 4) && (i < ByteSize); i++) {
    OutByte[ByteSize - 1 - i] = (byte) (Num >> 8 * i & 0xFF);
 }
 return OutByte;
}


Victor 筆記 2014/01/06

2013年12月21日

Java: protected 關鍵字

在開發java專案的時候常常會遇到某個方法或成員想被其他物件繼承,

但是又不想公開給大家使用,這時候就可以用 protected 關鍵字 來宣告。

例子如下:

Math.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package com.victorhsiao.blog.classes;

public class Math {

 public Math() {
 }

 public int Add(int n1, int n2) {
  return n1 + n2;
 }

 protected int Multiply(int n1, int n2) {
  return n1 * n2;
 }
}

main.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package com.victorhsiao.blog.classes;

public class main {
 private Math m = null;

 public int Quartic(int num) {
  m = new Math();
  return m.Multiply(num, num) * m.Multiply(num, num);
 }

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  main mm = new main();
  System.out.println(mm.Quartic(10));
 }

}

other.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package com.victorhsiao.blog.otherclasses;

import com.victorhsiao.blog.classes.Math;

public class other {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Math test = new Math();
  test.Multiply(10, 20); // error
 }
}

由於 Math 這個 class內的 Multiply 方法使用 protected,這使的

在套件 com.victorhsiao.blog.classes 下的物件都可存取它,
 
但是不同套件的物件卻看不到 (other.java)。 
 
當你有個方法想被繼承但又不想讓全世界都可存取,
 
這時棄用 public 改用 protected.
 
好的準則是: 將資料成員宣告為 private, 保留更動底層實作的權限,
 
透過protected method 來控制繼承者對你所撰寫的方法之存取權。 
 
 
 
Victor   2013/12/21 筆記  

2012年3月13日

JAVA:計算從今天到某天一共有多少天

import java.util.Calendar;
public class Date_Something {
public static void main(String[] args) {
Calendar today = Calendar.getInstance();
Calendar someday = Calendar.getInstance();
someday.set(2012,9,13);
System.out.println(today.getTimeInMillis());
System.out.println(someday.getTime());
System.out.println("離 "+someday.get(someday.YEAR)+"/"+(someday.get(someday.MONTH)+1)+"/"+someday.get(someday.DATE)+"剩"+(someday.getTimeInMillis()-today.getTimeInMillis())/ (1000 * 60 * 60 * 24)+"天");
}
}

JAVA:只有call by value

http://caterpillar.onlyfun.net/Gossip/JavaEssence/CallByValue.html

JAVA:JAVA本質,"參考"

http://caterpillar.onlyfun.net/Gossip/JavaEssence/EqualOperator.html

JAVA:不定長度引數

public class Variable_Length_Argument {
public static void main(String[] args) {
int result=0;
result=MathTool.Multiply(10,20,30,40,50,60);
System.out.println(result);

}
public static class MathTool {
public static int Multiply(int ... nums)
{
int sum=1;
for(int num=0;num sum = sum* nums[num];
}
return sum;
}
}

}

JAVA: for Loop

for(int num : nums) {
sum += num;
}

這等於

for(int num=0;num sum += nums[num];
}


int num : nums

跑num到nums的長度 這麼多次

2012年2月22日

Call By Reference In JAVA


public class Address {
public static void main(String[] args) {
Susan susan = new Susan(100);
int x=susan.getX();
System.out.println(x);
}
}

public class Susan {
private int x;
public Susan(int y){
this.x=y;
}
public int getX(){
return this.x;
}
}