Java语言基础
整数类型
指没有小数的部分,可以是整数可以是负数,整型数据根据它占的内存大小不同,分为byte、short、int、long4中类型。
8位等于一个字节,byte占一个字节,short占两个字节,int占四个字节,long占八个字节。
由于long型的取值范围比int型大,且属于高精度数据类型,赋值时要与int做出区分,需要在整数后面加上L。
浮点类型
用来存储含有小数部分的数值,Java语言浮点类型分为但精度浮点类型(float)和双精度浮点类型(double)。
在默认情况下小数看作double,使用float型小数,要在小数后面加上F。定义float型变量时,不加F,系统会认为是double类型。
字符类型char
字符类型char用于存储单个字符,占两个字节,在定义字符型变量,要用''表示,如“s“表示的是一个字符串,由于使用双引号,他表示字符串而不是字符。
需要的字符可以搜索unicode:
char c1='\\'; //将转义字符'\\'赋值给变量c1
char char1='\u2605'; //将转义字符'\u2605'赋值给变量char1
布尔类型
布尔类型称逻辑类型,通过关键词boolean来定义布尔类型的变量。布尔类型只有true和false两个值,代表真和假。布尔类不能与整型类进行转换。布尔类型在流程控制中作为判断条件。
public class yzx {
public static void main(String[] args) {
// TODO Auto-generated method stub
byte b = 3;
System.out.println(b);
short s=456;
System.out.println(s);
int i= 987654321;
System.out.println(i);
long l=123456789L; //long型的取值范围比int型大,属于高精准数据类型,所以在赋值时要和int做出区分,在整数后面加个L。
System.out.println(l);
float f=0.3f; //默认情况下小数被看作double类型,使用float类需在后面加f,否则看作double类型
System.out.println(f);
double d=3.14;
System.out.println(d);
}
}
/*定义一个变量,名字叫b,类型为byte,赋值为3,定义一个变量,名字叫s,类型为short,赋值为456,定义一个变量,名字叫i,类型为int,赋值为987654321,定义一个变量,名字叫l,类型为long,赋值为1234567890123456,定义一个变量,名字叫f,类型为float,赋值为0.3,,定义一个变量,名字叫d,类型为double,赋值为3.14*/
![](https://img-blog.csdnimg.cn/img_convert/fe71ab66cc07c89e9f531b6fc7e3660e.png)
public class sgtz {
public static void main(String[] args) {
// TODO Auto-generated method stub
double height=1.74;
int weight=65;
double exponent=weight/(height*height);
System.out.println("你的身高为:"+height);
System.out.println("你的体重为:"+weight);
System.out.println("你的BMI指数为:"+exponent);
System.out.print("你的体重属于:");
if(exponent<18.5) {
System.out.println("体重过轻");
}
if(exponent<18.5&&exponent<24.9) {
System.out.println("正常范围");
}
if(exponent<24.9&&exponent<29.9);{
System.out.println("体重过重");
}
if(exponent>=29.9) {
System.out.println("肥胖");
}
}
}
/*判断ex是否<18.5,如果是,那么执行System.out.println("体重过轻"),如果ex不<18.5,则判断是否>=18.5&&ex<24.9,如果是,那么执行System.out.println("正常范围"),如果不是则在判断ex>=24.9&&ex<29.9如果是,那么执行System.out.println("体重过重"),如果不是则在判断ex>=29.9那么执行System.out.println("肥胖")*/
![](https://img-blog.csdnimg.cn/img_convert/ab70e6a364d697812209d004c0f7b380.png)
public class c {
public static void main(String[] args) {
// TODO Auto-generated method stub
char c1='\\'; //将转义字符'\\'赋值给变量c1
char char1='\u2605'; //将转义字符'\u2605'赋值给变量char1
System.out.println(c1);//输出结果
System.out.println(char1);//输出结果
}
}
![](https://img-blog.csdnimg.cn/img_convert/cf8f8b21a66d00181d58dd5b09b09e1d.png)
package third;
public class b {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean b1=true;
boolean b2=Boolean.FALSE;
System.out.println(b1);
System.out.print(b2);
}
}
/*定义一个变量,名字叫c1,类型为char,将\赋值给c1,定义一个变量,名字叫c2,类型为char,将u2504赋值给c2,这是一个unicode码,就是万国码,,输出c1,输出c2,定义一个变量,名字叫b,类型为boolean,将结果true赋值给b,定义一个变量,名字叫b2,类型为boolean,将结果false赋值给b2,输入大写有错误,输入小写不会,或者大写前面加Boolean,注意B一定要大写,输出b1,输出b2*/
![](https://img-blog.csdnimg.cn/img_convert/06ad4d377aeaa9cf8add8d72e59cee93.png)
Java常量与变量
标识符用来标识类名、变量名、方法名、数组名、文件名的有效字符序列。标识符由字母,下划线,美元符号和数字组成。
数字开头不能用;关键词(在代码中为彩色)不能用
![](https://img-blog.csdnimg.cn/img_convert/2019867bc22b0431b9f621560e162034.png)
就近原则:输出局部变量4。
输出成员变量在times前加类名
![](https://img-blog.csdnimg.cn/img_convert/ee30ea9c7a69aa34bae088c165899d12.png)
public class yu {
static int age=3; //定义成员变量
public static void main(String[] args) {
// TODO Auto-generated method stub
final int times=4; //定义局部变量
System.out.println(times);
System.out.println(age);
}
}
/*static int age=3; //定义成员变量,final int times=4; //定义局部变量,最后输出结果times,age*/
![](https://img-blog.csdnimg.cn/img_convert/ec6bef90da22d5857c32da58a32b2095.png)
public class yu {
static int age=3; //定义成员变量
static final double pi=3.14; //定义常量,常量只能赋值一次
public static void main(String[] args) {
// TODO Auto-generated method stub
final int times=4; //定义局部变量
System.out.println(times);
System.out.println(age);
System.out.println(pi);
}
}
/*static int age=3; //定义成员变量,static final double pi=3.14; //定义常量,常量只能赋值一次,final int times=4; //定义局部变量,最后输出结果times,age,pi*/
![](https://img-blog.csdnimg.cn/img_convert/7abaac4c76c71fa752f29c14f849c301.png)