02 常量 變數

2021-09-27 11:14:03 字數 4028 閱讀 8247

變數常量——在程式的執行過程中其值不發生改變的量。

之後,通過字首區分不同進製的資料

system.out.

println

(0b100);

//二進位制 以0b開頭

system.out.

println

(0100);

//八進位制 以0b開頭

system.out.

println

(100);

//十進位制 整數預設是十進位制的

system.out.

println

(0x100);

//十六進製制 以0x開頭

(理解計算機底層如何做運算——所有資料的運算都是採用補碼進行的)

有符號資料表示法的幾種方式:

(1)

: 數值型

整數型 位元組數 範圍

byte1(

8bit)

-128

~127

short2(

16bit)-2

^15~2

^15-1

int4

(32bit)-2

^31~2

^31-1

long8(

64bit)-2

^63~2

^63-1

浮點數float4(

32bit)

-3.403*10

^38~3.403*10

^38double8(

64bit)

-1.798*10

^308

~1.798*10

^308(2

): 字元型 char2(

16bit)0~

2^16-

1=65535(3

): 布林型 boolean

1

整數預設是int型別的 , 浮點數預設是 double型別的

long(值後加字尾l:long num2=1000l)

float(值後加字尾f:float f=3.14f)

char(值用單引號引起來 只能是乙個字元:char ch=『a』)

boolean(boolean flag=true;boolean flag2=false)

//定義不同型別的變數並且輸出

//定義變數的格式:資料型別 變數名=值

//變數名的命名規範:通用規範 變數名一般小寫 多個單詞 首字母小寫 name username username

byte a=1;

a=20;

//重新給變數賦值

byte c=

127;

int num=

10000

;long num2=

1000l;

//定義long型別的變數的時候,注意值得後面加上乙個字尾l 表示這是乙個long型別值

float f=

3.14f

;//定義float型別時,值得後面加上字尾f 表示這是乙個float型別得值,不加語法報錯

double d=

3.14

;char ch=

'a';

//char 型別得值,要用單引號 引起來,只能是乙個字元

boolean flag=

true

;boolean flag2=

false

;//通過變數名,輸出該變數得值

system.out.

println

(a);

system.out.

println

(c);

system.out.

println

(s);

system.out.

println

(num)

; system.out.

println

(num2)

; system.out.

println

(f);

system.out.

println

(d);

system.out.

println

(ch)

; system.out.

println

(flag)

; system.out.

println

(flag2)

;

//定義變數得一些注意事項  //作用域:域,範圍  一對{} 所括起來得這對範圍,我們稱之為作用域

//1.在同乙個作用域 裡面不能定義兩個同名變數

int a=

100;

a=200;

//重新賦值

//2.變數必須賦值才能使用

int b;

b=10;

//3.在一行上建議只定義乙個變數

int e;

int f;

e=20;

f=20;

byte by=1;

byte by2=2;

//unicode碼——國際標準 漢字編碼方式都遵守這個國際標準

//char型別兩個位元組; gbk編碼乙個漢字佔兩個位元組; utf-8編碼乙個漢字佔三個位元組

char ch=

'你';

//gbk

byte a=1;

char ch=

'a';

int r=a+ch;

//r為98 //ascii碼表中 'a'對應97 1+97為98

//ascii 值 'a' 97 'b' 98 'a' 65 '0' 48 '1' 49

在運算中自動轉換資料型別

(1): boolean型別不能轉換為其他的資料型別

(2): byte,short,char —int— long—float—double

只有byte、short、char資料型別進行運算時,資料型別將自動轉化為int型別。

若還有long、float、double型別存在時,將以byte,short,char—int—long—float—double順序進行轉換。

(3): byte,short,char之間不轉換,他們參與運算首先轉換為int型別

//整數預設是int型別 小數預設是 double型別

float f=

3.14f

;double dd=

3.14

;long num=

1l;/*

型別的自動提公升:byte short char 在參與運算時,會自動提公升為int 型別

如果還有 long float double 型別參與運算 則會相應的提公升為 long float double

布林型別不參與數**算

byte short char 他們之間不轉換

*/byte b=3;

byte a=3;

int r=a+b;

//byte r=a+b 變為int r=a+b//byte short char 在參與運算時 自動提公升為int 型別

byte c=20;

long num=

1l;float f=

1.1f

;double ss=c+num+f;

//有 long float double 型別參與運算 相應的提公升為 long float double

格式:目標資料型別 變數名=(目標資料型別)(被轉換的資料);

盡量不要使用強制型別轉化,因為可能存在損失精度的問題。

byte a=3;

byte b=

127;

//int r=a+b;

//強制型別轉換,把結果強制轉換成某種型別,但是可能會有精度損失,因為會丟棄掉多餘位元組

short r=

(short

)(a+b)

;

20150527常量變數

main.c ios150527 created by peng junlong on 15 5 27.include void changliangbian int main int argc,const char argv 常量 變數 常量 是c語言中最基本的元素,包括 字元常量,整型常量,浮點...

Swift 常量變數

main.swift swift 常量變數 created by dingkang on 15 12 15.import foundation 常量 在程式執行期間,不可以改變的量,稱之為常量。變數 在程式執行過程中,其值可以任意改變的量稱之為變數。變數和常量一樣,在使用之前都要進行生宣告和自定義 ...

04 常量 變數

按步驟編寫 效果如圖所示 編寫步驟 1.定義類 test1 2.定義 main方法 3.控制台輸出5行字串型別常量值 4.控制台輸出5行字元型別常量值 參 public class test1 按步驟編寫 效果如圖所示 編寫步驟 1.定義類 test2 2.定義 main方法 3.控制台輸出5行整數...