SlideShare a Scribd company logo
変数と型の怪しい関係
E.Nakamine
〜Enjoy to Object-oriented 〜
変数ってなんだ?
英語だとVariable(変えられる、変動する)
不定数(数学でとかやった↓これ)
ax² + bx + c = 0
プログラムでは箱として例えられることが多い
でも、
ただの箱じゃ無いんです
(なぜならば)
林檎の箱にスイカが入らないように、
スイカの箱に林檎が入らないように。
(入れれば入るけど傷んじゃう)
Integerに文字列は入らないし、
Stringに数値は入らない。
入れるものによって
箱の形が違う。
これが型。
Javaの型は
大きく分けると2種類
プリミティブ型
クラス型(参照型)
プリミティブ型
byte 1バイト整数(-128~127)
short 2バイト整数(-32768~32767)
int 4バイト整数(-2147483648~2147483647)
long
8バイト整数(-9223372036854775808~
9223372036854775807)
float 4バイト単精度浮動小数点数
double 8バイト倍精度浮動小数点数
char 2バイト文字データ(u0000~uffff)
boolean 論理値 trueまたはfalse
プリミティブ (primitive)とは、「原始的」「素朴な」「幼稚な」と
いう意味。(出典:Wikipedia)
値そのものを格納する箱
クラス型(参照型)
クラスのインスタンス(実体)に対す
る参照を格納する箱
参照とは実体に対するショートカットの様な物
なお、StringやIntegerの様にJava言語が用意していてくれてい
る物は組み込みクラスとも呼ぶ。
クラス型
変数
クラス型変
数の実体
クラス型
変数’
クラス型の例
String hogehoge = “piyopiyo”
new String(“piyopiyo”)
TextView tv = (TextView)findViewById(R.id.textView1)
MyCountDownTimer timer = new MyCountDownTimer(略)
クラスとは
基本的にはJavaプログラムの1ファイルのこと。
クラス
コンストラクタ
プロパティ
メソッド
もうちょっと正確に言うと
class className{
…
}
で囲われた部分。
クラスの中にクラスを作る事も可能。
(MyCountDownTimerはクラスの中の
クラス)
コンストラクタ
コンストラクタとはクラスからクラス型変数の実体
を生み出してクラス変数へ参照を返してあげる処理
。
クラス
コンストラクタ
プロパティ
メソッド
新世界の創造とゲートを作
る的な。。。
プロパティ
クラスは状態(変数)を持つことが出来る。
クラス
コンストラクタ
プロパティ
メソッド
例えばStringクラスは内部に
charの配列変数を持っておりこ
れに文字列が格納されている
。
メソッド
振る舞い。
ある程度の処理をまとめて記述したブロック。
クラス
コンストラクタ
プロパティ
メソッド
よく使っているfindViewByIdは
MainActivityのメソッド。
省略せずに書くと
this.findViewById(thisは自分自身)
MainActivityのプロパティとして存
在するView(TextViewとかEditView
とかこれもクラス)を取得するメソ
ッド。
クラス型変数はnewして作
る
MyCountDownTimer count = new MyCountDownTimer()
「new クラス名()」というのは該当クラスの
コンストラクタメソッドを実行する命令です。
MyCountDownTimerで言うと以下のメソッド。
public MyCountDownTimer(long millisInFuture, long
countDownInterval) {
super(millisInFuture, countDownInterval);
}
newしないこともある
newしないですでにあるクラスのイン
スタンスを取得する場合もある。
得にAndroidの場合は中の人が作ったの
を利用利用することが多く。
findViewByIdで取得した奴(TextView
とか)なんかはAndroidの中の人が画面
レイアウトをもとに作ってくれている
。
まとめ
変数には型がある
型はプリミティブ型とクラス型(参照型)
intやdoubleはプリミティブ型
TextViewやMyCountDownTimerはクラス型
プリミティブ型は数字しか入らない簡単な箱
クラス型はクラスという設計図を元に作った特注の
箱。しかも自分で動いたりするびっくり箱も作れる
変数にはスコープがあるよ
スコープってのは有効範囲
if (true) {
int a = 1;
}
System.put.println( a );
int a = 0;
if (true) {
a = 1;
}
System.put.println( a );
こっちはNGなパターン こっちはOKなパターン
Appendix
ご静聴ありがとうございまし
た
フォントはたぬき油性マジックを使用しています
http://tanukifont.com/

More Related Content

Viewers also liked

Menu biba's Restaurant & Pizza
Menu biba's Restaurant & PizzaMenu biba's Restaurant & Pizza
Menu biba's Restaurant & Pizza
SILVIA RENSI
 
Top 4 Benefits of Having Pearly, White Teeth
Top 4 Benefits of Having Pearly, White TeethTop 4 Benefits of Having Pearly, White Teeth
Top 4 Benefits of Having Pearly, White Teeth
FelippWindsor13
 
WOMEN'S LIVELIHOOD PROJECT
WOMEN'S LIVELIHOOD PROJECTWOMEN'S LIVELIHOOD PROJECT
WOMEN'S LIVELIHOOD PROJECT
monaps1
 
2015 10 21_expo_sem_6-nancy soto - shotcrete
2015 10 21_expo_sem_6-nancy soto - shotcrete2015 10 21_expo_sem_6-nancy soto - shotcrete
2015 10 21_expo_sem_6-nancy soto - shotcrete
Instituto del cemento y Hormigón de Chile
 
Workers mobility within Schengen: acomparative study of 17 countries requirem...
Workers mobility within Schengen: acomparative study of 17 countries requirem...Workers mobility within Schengen: acomparative study of 17 countries requirem...
Workers mobility within Schengen: acomparative study of 17 countries requirem...
Marco Mazzeschi
 
Effective communication
Effective communicationEffective communication
Effective communication
Dr. Chandra Shekhar Singh
 
BIMO Forum 2015 - Stemys.io
BIMO Forum 2015 - Stemys.ioBIMO Forum 2015 - Stemys.io
BIMO Forum 2015 - Stemys.io
Artionet Web Agency
 
Electronic Anti-Pollution Brochure v2
Electronic Anti-Pollution Brochure v2Electronic Anti-Pollution Brochure v2
Electronic Anti-Pollution Brochure v2
Quinn Ryder
 

Viewers also liked (8)

Menu biba's Restaurant & Pizza
Menu biba's Restaurant & PizzaMenu biba's Restaurant & Pizza
Menu biba's Restaurant & Pizza
 
Top 4 Benefits of Having Pearly, White Teeth
Top 4 Benefits of Having Pearly, White TeethTop 4 Benefits of Having Pearly, White Teeth
Top 4 Benefits of Having Pearly, White Teeth
 
WOMEN'S LIVELIHOOD PROJECT
WOMEN'S LIVELIHOOD PROJECTWOMEN'S LIVELIHOOD PROJECT
WOMEN'S LIVELIHOOD PROJECT
 
2015 10 21_expo_sem_6-nancy soto - shotcrete
2015 10 21_expo_sem_6-nancy soto - shotcrete2015 10 21_expo_sem_6-nancy soto - shotcrete
2015 10 21_expo_sem_6-nancy soto - shotcrete
 
Workers mobility within Schengen: acomparative study of 17 countries requirem...
Workers mobility within Schengen: acomparative study of 17 countries requirem...Workers mobility within Schengen: acomparative study of 17 countries requirem...
Workers mobility within Schengen: acomparative study of 17 countries requirem...
 
Effective communication
Effective communicationEffective communication
Effective communication
 
BIMO Forum 2015 - Stemys.io
BIMO Forum 2015 - Stemys.ioBIMO Forum 2015 - Stemys.io
BIMO Forum 2015 - Stemys.io
 
Electronic Anti-Pollution Brochure v2
Electronic Anti-Pollution Brochure v2Electronic Anti-Pollution Brochure v2
Electronic Anti-Pollution Brochure v2
 

変数と型