变量的使用&运算符
1.基本数据类型(8个)
|
|
|
|
---|---|---|---|
|
| 1 |
|
|
| 2 |
|
|
| 4 |
|
|
| 8 |
|
java中书写的整数默认为int类型
long型变量的值末尾要追加L或l
|
|
|
---|---|---|
|
| 4 |
|
| 8 |
java中书写的小数默认为double类型
float类型的变量需要在值的末尾追加F/f
|
|
|
---|---|---|
|
| 2 |
值:一般使用单使用单引号引起来,如果不带单引号,只能赋值ascii码中char
2.引用数据类型
String 字符串 值使用双引号引起来
3.声明的使用
java中的所有变量在使用之前必须完成赋值
语法规则:数据类型 变量名 [=值]
主要是数值类型和char字符类型
byte --> short --> int --> long --> float --> double
char向int以及之后的高等级数据类型遵循自动转换的原则
char和byte、short需要相互强制转换
小数转换为整数的时候,丢失精度,只保留整数部分
整数转换为小数的时候,自动提升精度等级
按照箭头方向是自动转换,反之需要强制转换
自动转换(隐式转换):转换自然发生
强制转换:把赋值符右边变量的类型和左边的变量类型变得一致
public class TypeConversion {
public static void main(String[] args) {
/*
* 数据类型的转换(boolean不参与转换)
* 主要是数值类型和char字符类型
* byte-->short-->int-->long-->float-->double
* char向int及以后的高级数据类型转化遵循自动转换的原则
* char和byte、short需要相互强制转换
* 小数转换为整数的时候,丢失精度,只保留整数部分
* 整数转换为小数的时候,自动提升精度等级
* 按照箭头方向是自动转换,反之需要强制转换
*
* 自动转换(隐式转换):转换自然发生
*/
/*
* 算数运算符:
* + - * / %(取余) ++ --
*/
byte b1=10;
short s1;//声明一个short类型的变量
//把b1赋值给s1
s1 = b1;//自动转换
//打印输出s1
System.out.println(s1);
short s2 = 100;
byte b2;
b2 = (byte)s2;//强制转换,需要程序员自行判断是否会出现溢出,溢出不好
System.out.println(b2);
/*float和double*/
float f1 = 1.5f;
double d1;
d1 = f1;//自动转换
double d2 = 1.5;
float f2;
f2 = (float)d2;
/*long和float*/
long len1 = 100;
float f3;
f3 = len1;
System.out.println(f3);
float f4 = 100.9f;
long len2;
len2 = (long)f4;
System.out.println(len2);
/*char和int*/
char ch1 = '哈';
int int1;
int1 = ch1;
int int2 = 100;
char ch2;
ch2 = (char)int2;
/*char和short*/
char ch3 = '呵';
short s5;
s5 =(short)ch3;
short s6 = 10;
char ch4;
ch4 = (char)s6;
}
}
1.算数
+(加) -(减) *(乘) /(除) %(取余) ++(自增1) --(自减1)
1、当不同数据类型参与运算的时候,结果会向高等级靠拢
2、int之前的数据类型,运算的结果都是int类型
3、++/--,使用在变量之前,先自增1/自减1,后运算。使用在变量之后,先运算,后自增1/自减1
4、+使用在字符串的前后表示连接符,连接之后会生成一个新的字符
public class Arithmetic {
public static void main(String[] args) {
/*
* 算数运算符:
* +(加) -(减) *(乘) /(除) %(取余) ++(自增) --(自减)
*
* 1、当不同的数据类型参与运算的时候,结果都会想高等级靠拢
* 2、int之前的数据类型,运算的结果都是int类型
* 3、++/--使用在变量之前,先自增1/自减1,后运算;使用在变量之后,先运算,后自增1/自减1
*/
byte b1 = 10;
short s1 = 20;
int num = b1+s1;
//double的有效位数是小数位16位
double num1 = 2.0;
double num2 = 1.1;
System.out.println(num1-num2);
System.out.println(10/3);//整数类型相除只保留整数部分
System.out.println(10%3);//1
System.out.println("-----------------------");
int a = 1;
int b = a++;//先运算,后自增1
System.out.println(a+"\t"+b);
int aa = 1;
int bb = ++aa;//先自增1后运算
System.out.println(aa+"\t"+bb);
/*运算符的使用*/
double dou = 1.2356789;//使用四舍五入的方法,保留2位小数
dou = dou*100;//123.56789
dou = dou+0.5;//124.06789
int n = (int)dou;
dou = n/100.0;
System.out.println(dou);
int x = 3;
int y = x+++x+(--x)+x;//3+4+3+3
System.out.println(y);
//声明并赋值一个字符串
String str = "哈\t哈,\n你真逗!";
System.out.println(str);
String str2 = "哈哈"+3+5;//+使用在字符串的前后表示连接符,连接后会生成一个新的字符串
System.out.println(str2);
String str3 = 3+5+"哈哈";
System.out.println(str3);
}
}
2.比较
> < >= <= ==(恒等于) !=(不等于)
* 1、比较运算符的结果是boolean类型
public class Compare {
public static void main(String[] args) {
/*
* 比较运算符:
* > < >= <= ==(恒等于) !=(不等于)
* 1、比较运算符的结果都是boolean类型
*
*/
System.out.println(5>3);
System.out.println(5==3);
System.out.println(5!=3);
}
}
3.赋值
= += -= *= /= %=
1、既有运算的过程,也有赋值的过程
2、赋值运算符的左边一定是变量,运算之后,吧结果重新赋值给运算符左边的变量
3、必须遵循数据类型的特征
public class Assignment {
public static void main(String[] args){
/*
* 赋值运算符:
* = += -= *= /= %=
* 1、既有运算的过程,也有赋值的过程
* 2、赋值运算的左边一定是变量,运算之后把结果重新赋值给运算符左边的变量
* 3、必须遵循数据类型的特征
*/
byte b1 = 10;
short s1 = 20;
b1+=s1;//相当于b1 = (byte)(byte+s1);
System.out.println(b1);
System.out.println(s1+=1.5);
int a = 10;
System.out.println(a%=3);
}
}
4.逻辑
&(与 and) |(或or) ^(异或) !(非,取反) &&(短路与) ||(短路或)
1、逻辑运算符的结果是boolean类型
2、短路,当在运算的过程中。遇到对整个结果起决定性作用的值时,结束运算
&&:false
||:true
3、&的运算等级高于|
5.三元
语法:值的数据类型 变量名 = 条件表达式?值1:值2;
值的类型必须保持一致
6.位
& | ^
现将数值转换为对应的二进制,末尾对齐,不足的位置使用0补位,然后逐位运算,1--true,0--false
public class Logic {
public static void main(String[] args){
/*
* 逻辑运算符:
* &(与and) |(或or) ^(异或) !(非,取反) &&(短路与) ||(短路或)
* 1、逻辑运算符的结果是Boolean类型
* 2、短路,当在运算的过程中,遇到对整个结果起决定性作用的值时,结束运算
* &&:false
* ||:true
* 3、&的运算等级比|高
*/
System.out.println(true&false);//false
System.out.println(true|false);//true
System.out.println(true^false);//true
System.out.println(true^true);//false
System.out.println(!true);//false
//true
System.out.println(false ^ false ^ true ^ false ^ false ^ false);
int a = 1;
System.out.println(false & ++a > 0);
System.out.println(a);
int aa = 1;
System.out.println(false && ++aa > 0);
System.out.println(aa);
// &和|谁的等级高?
System.out.println(true | true & false);// true
/*
* 三元运算符
*
* 语法:值的数据类型 变量名 = 条件表达式?值1:值2;
* 值的类型必须保持一致
* */
int day = 5;
String str = day>6?"休息":"学习";
int age = 18;
String str2 = age>=18?"成年":"未成年";
System.out.println(str2);
/*
* 位运算符:
* & | ^
* 现将数值转换为对应的二进制,末尾对齐,不足的位置使用0补位,然后逐位运算,1--true,0--false
*/
int num = 5&3;
/*
* 0101
* &0011
* 0001
*/
System.out.println(num);
System.out.println("------------------");
int num2 =20^15;
//把数值转换为二进制
System.out.println(Integer.toBinaryString(20));
/*
* 00010100
* &00001111
* 00011011
*/
System.out.println(num2);
}
}