/*

变量的使用

1.Java定义变量的格式:数据类型 变量名 = 变量值;

2.变量注意

Java中的每个变量都必须声明后使用

使用变量名来访问这块区域的数据

变量的作用域:其定义所在的一对{}内

变量只有在其作用域内才有效

同一个作用域内,不能定义重复的变量

java定义的数据类型

1.变量按照数据类型分:

基本数据类型:

整型:byte(1字节 = 8bit) / short(2字节) / int(4字节) /long(8字节)

byte (范围:-128 - 127)

浮点型:float  单精度 4字节   表示的范围比long还大/ double 双精度 8字节


字符型:char   定义时通常使用一对'',内部只能有一个字符  或者   转义字符  /t   /n

或者 直接用Unicode值来表示字符型常量 '/u0043' = c

布尔型: boolean  只能取两个 值之一    true   和    false

条件判断 循环结构中使用 

引用数据类型:

类(class)  

接口(interface)

数组(array)

2.变量在类中声明的位置:

成员变量 vs 局部变量

*/

class  VariableTest

{

public static void main(String[] args) 

{

int myAge = 12;//变量的定义

//byte myS = 128;  超过 byte的定义范围

System.out.println(myAge);//变量的使用

int myNumber = 12456246;

System.out.println(myNumber);

long myNa = 54646654l;//long 类型 结尾必须加"l"或者"L"

System.out.println(myNa);

float aA = 123.4f;

System.out.println(aA);//float 后面要加”f“或者"F"  通常使用 double

}

}

//*******************************************************************************************************

/* 

基本数据类型之间的运算规则:

前提:主要讨论7钟基本数据类型变量间的运算。不包含boolean类型

1.自动类型提升:

当容量小的数据类型的变量与容量大的数据类型的变量做运算时,结果自动提升为容量大的数据类型。

byte 、 char 、 short -->int -->long -->float -->double

特别的 当 byte char short  三种类型做运算时,结果为int型

2.强制类型转换:

自动类型提升运算的逆运算。

(1).需要使用强转符:()

(2).强转类型转换,可能导致精度损失

*/


class VariableTest1 

{

public static void main(String[] args) 

{

//精度损失案例1

double d1= 12.3;

int i1 = (int)d1;//截断操作

System.out.println(i1);

//没有精度损失

long l1 = 123;

short s2 = (short)l1;

System.out.println(s2);

//精度损失案例2

int i2 = 128;

byte b = (byte)i2;

System.out.println(b);//-128

}

}

//************************************************************************************************************************************

/*

String 类型变量的使用

1.String属于引用数据类型,翻译为:字符串

2.声明String类型变量时,使用一对""

3.String可以和8种基本数据类型变量做计算,切运算只能是连接运算:+

4.运算的结果仍然是String类型


*/

class  VariableTest2

{

public static void main(String[] args) 

{

String s1 = "Hello World";

System.out.println("s1");

String s2 = "a";

String s3 = "";

//char c =  ''; 编译不通过  char 里必须有 字符或者 空格


//******************

int number = 1001;

String numberStr = "学号:";

String info = numberStr + number;//+:连接运算

System.out.println(info);

//*****************

//练习1

char c = 'a';// a=97;A=65;

int num =10;

String str = "hello";

System.out.println(c + num + str);//107hello

System.out.println(c + str + num);//ahello10

System.out.println(c + (num + str));//a10hello

System.out.println((c + num) + str);//107hello

System.out.println(num + str + c);//10helloa


//******************

//练习2

System.out.println("**");//**

System.out.println('*' + '\t' + '*');//93

System.out.println('*' + "\t" + '*');//**

System.out.println('*' + '\t' + "*");//51*

System.out.println('*' + ('\t' + "*"));//**


//**************

//练习3

String str2 = 3.5f +"";

//String str2 = 123;编译不通过,""缺失

System.out.println(str2);

short s = 5;

s = (short)(s-2);

}

}

/*

判断是否能通过编译

1)short s = 5;

s = s - 2;  //判断: 不行  2是int型  s是short  正确 s = (short)(s-2);


*/

//****************************************************************************************************************************************************

/*

计算机钟不同进制的使用说明

对于整数,有四种表示方式:

》二进制(binary): 0,1,满2进1,以0b或0B开头;

》十进制(decimal): 0-9 ,满10进1;

》八进制(octal): 0-7,满8进1,以数字0开头表示;

》十六进制(hex):0-9及A-F,满16进1,以0x或者0X开头表示。此处的A-F不区分大小写。

如  : 0x21AF +1 = 0X21B0


*/class BinaryTest {

public static void main(String[] args) {

int num1 = 0b110;

int num2 = 110;

int num3 = 0127;

int num4 = 0x110A;

System.out.println("num1 = " + num1); // 6 2^2+2^1

System.out.println("num2 = " + num2); //110  10^2+10^1

System.out.println("num3 = " + num3); //87 1*8^2+2*8^1+7

System.out.println("num4 = " + num4); //4362 16^3+16^2+10

}

}


©著作权归作者所有:来自51CTO博客作者爱睡觉的牛的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 前沿: nature刊掀起DAG热, 不掌握就遭淘汰无疑!因果关系研究的图
  2. 男神毛咕噜最新Top5大作, 另外, 有序因变量依然使用OLS回归!
  3. 拐点回归设计RKD概览, 及其开展实证研究的经典示例
  4. 三张图秒懂, 混淆, 中介, 调节, 对撞, 暴露, 结果和协变量的复杂
  5. 万能cmp程序, 有了他, 建议把其他程序全删掉!
  6. 什么时候应该使用回归分析?控制变量意味着什么?
  7. 应用计量经济学现状: 因果推断与政策评估最全综述
  8. 选择模型真用对了吗? 扒一扒75篇顶级外刊的真相, 并献上最权威的
  9. 主流: KHB法测度中介效应, 无论线性还是非线性模型, KHB都能分解

随机推荐

  1. Android 基本空间划分
  2. android访问服务器端上传及服务器端接收
  3. Android中mpchartlib柱状图的详细属性以
  4. Android的android:clipChildren属性
  5. android 按钮点击的两种方法以及长按事件
  6. Android开发视频教程大总结—23部视频教
  7. Android版块开发资源大汇总
  8. 启动app闪屏问题以及Android自带主题
  9. Android之layout属性介绍
  10. 玩转Android---UI篇---TextView(文本框)