一 . java的第一个程序
Public class Helloworld {
public static void main (string[] args) {
system .out.println("hello!");
}
}
分别代表什么?
- 类名 : Helloworld 因为有class修饰 Helloword 当这个类名(被public修饰时)和文件名是一样的
-
一个文件可以有多个类,一个类会产生一个字节码文件,建议一个java文件只用一个类。
- 类的里面是方法(函数)
(1)main方法(函数)只有一个,main 函数在写的时候public static是固定的不能修改.
void为返回值 ,main为方法名,()参数列表组成 。
{} 花括号内位方法体,它的代码风格右括号且必须写后面且要先空格 左花括号对齐开头 即可。
(2)String [] agrs 是一个数组,可能学了c语言之后会不太适应觉得奇怪,但恰恰java更好 理解。
在java中 int [] arr int[]类型 arr为变量。
(3)方法是放到类里面的。
二.数据类型
1.类型:
(1)基本数据类型/内置类型:整数型:byte short int long
小数型:float double
字符型:char
布尔类型:boolean
(2)引用数据类型:string 数组 接口 类……(这些类型定义的变量都是存储地址的变 量)
2.基本数据类型的分类(如图)
可移植性:例如不管你在多少位操作系统下数据类型所占用的字节数是一定的。
在Java中,所有的类型定义的数据都是有符号的。
三.变量
1.什么是局部变量:定义在方法(函数)内部的变量。
public class Main {
public static void main(String[] args) {
int a=10;//a定义在main方法内的局部变量
System.out.println(a);
}
}
2.语法格式 数据类型 变量名(标识符) = 初始值(初始值必须有!)
3.java中不允许未初始化的局部变量。
public class Main {
public static void main(String[] args) {
int a;
System.out.println(a);//error
}
}
4.初始化:
public class Main {
public static void main(String[] args) {
int a=4;//a为变量也称为标识符,里面存放的是4
int b;//初始化可以不定义在同一行
b=9;
int c1=2,c2=4,c3=9;//多个变量可以在同一行初始化
System.out.println(C1);
System.out.println(C2);
System.out.println(C3);
System.out.println(b);
System.out.println(a);
}
}
5.整型变量
Integer(开头要大写)其实就是int的plus版本,只不过在java中有个术语叫做:包装类型 (可以被 定义)不可以修改 。(后面会学)
public class Main {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);//2^31-1
System.out.println(Integer.MIN_VALUE);//-2^31
Integer a=10;
System.out.println(a);
}
}
6.长整型变量(包装类型:Long )
public class Main {
public static void main(String[] args) {
long b=10L;//一般情况使用long是初始值后要加L与int型做区别
System.out.println(b);
System.out.println(Long.MAX_VALUE);//2^63-1
System.out.println(Long.MIN_VALUE);//-2^63
}
}
7.短整型变量
public class Main {
public static void main(String[] args) {
short a=8;
System.out.println(Short.MAX_VALUE);//2^15-1
System.out.println(Short.MIN_VALUE);//-2^15
}
}
8.字节型变量(byte是1B,包装类型是Byte)(当初始值超出正常范围则会报错)
public class Main {
public static void main(String[] args) {
byte a=10;
System.out.println(a);
System.out.println(Byte.MAX_VALUE);//2^7-1 127
System.out.println(Byte.MIN_VALUE);//-2^7 -128
//java处理当赋值字面值超出当前数据类型能表示最大范围之后那就直接error报错
byte b=180;//超出-128~127
}
}
9.浮点型类型
(1)双精度浮点类型(double)
public class Main {
public static void main(String[] args) {
double date=18.3;
System.out.println(date);
int a=1;
int b=2;
System.out.println(1 / 2);//0
System.out.println(1 * 1.0 / 2);//0.5
//那这样的代码会是1.44吗
double ab=1.2;
System.out.println(ab * ab);//是
//那这样的代码会是1.21吗
double cc=1.1;
System.out.println(cc*cc);//不是,是1.2100000000000002
//因为小数是没有精确的数字的
}
}
①如果整除需要得到小数则用:double类型计算
②特殊情况:double 类型的内存布局遵守 IEEE 754 标准(和C语言一样), 尝试使用有限的内存空 间表示可能无限的小数, 势必会存在一定的精度误差,因此浮点数是个近似值,并不是精确值
(2)单精度浮点类型 (float)
public class Main {
public static void main(String[] args) {
float b=18.3f;//float类型在初始值后要加上f/L,来区分double
float a=1;//为什么没加f,因为1为int,int为4B,float为4B
System.out.println(b);
System.out.println(Float.MAX_VALUE);//2^31-1
System.out.println(Float.MIN_VALUE);//-2^31
}
}
10.字符型变量
public class Main {
//字符型变量
public static void main(String[] args) {
char d='1';
char fc='a';
char ch='往';//因为一个汉字的大小是2个字节,char为2字节
System.out.println(ch);
//对应的包装类型
Character b='a';
System.out.println(Character.MAX_VALUE);//2^15-1
System.out.println(Character.MIN_VALUE);//-2^15
System.out.println(b);//属于unicode字符集可以表示拉丁文
}
}
10.布尔型变量(只有两个结果 true false)
public class Main {
public static void main(String[] args) {
//在java中boolean只有两个值,要么是true,要么是false
//没有所谓的true为1,false为0
boolean bool=true;
System.out.println(bool);
//boolean类型没有明确的大小JVM没有明确的规定,也没有专门用来处理boolean的字码指令,在
//Oracle公司的虚拟机实现中,boolean占1个字节。
}
}
四.类型转换(虽然强制类型转换编译会通过,单都会丢失数据)
public class Main {
public static void main(String[] args) {
int a = 10;
long b = 100L;
a = b;//范围由大-小 隐式转换
//error报错,不允许8个字节给4个字节,在c中可进行编译,但会提示截断
//修改方法
b = a;
a = (int)b;//强制类型转换
}
}
五.类型提升(小类型转大类型)
public class Main {
public static void main(String[] args) {
int i = 10;
long j = 100L;
// int c = i + j;//error,因为在相加之前i先整型提升到8字节,8个字节相加也是8个字节,不可能与4个字节相等
//解决
int c = (int)(i+j);
long c1 = i + j;//这个更好,c类型提升
byte b1=10;
byte b2=20;
//byte b3=b1+b2;//error报错,当表达式进行相加时,如果表达式的值小于4个字节,先提升为4个字节,在进行运算所以想加之后时4个字节
//改
int b3 = b1+b2;
//short char也一样小于4个字节也都先整型提升4个字节等号左边必须是int类型
}
}
六.字符串类型(后面会细学)
public class Main {
public static void main(String[] args) {
String str="hello";//实际存的是字符的首地址h,为引用数据类型,str为引用变量,
//因为它为引用类型,首字母要大写!
System.out.println(str);
String str1="world";
System.out.println(str1);
//拼接
String str2=str+str1;//这个+就相当于把两个数给粘起来
System.out.println(str2);//结果helloword
//和整数进行拼接
int a=10;
System.out.println("a=="+10);//java中字符串和其他数据类型进行+操作运算 就是拼接的意思
int b=20;
System.out.println("a+b="+a+b);//a+b=1020
System.out.println(a+b+"=a+b");//30=a+b +就是拼接黏在一起哈
System.out.println("a+b="+(a+b));//a+b=30
}
}
七.字符串类型
public class Main {
public static void main(String[] args) {
//String转换为int
String str="123";
int val=Integer.parseInt(str);//用Int来接收
System.out.println((val + 1));//124成功转换为整型
//int转换为String
//1.
int a=123;
String str1=String.valueOf(a);
System.out.println(str1);//123
String str2=a+"";
System.out.println(str2);//123
String str3=Integer.toString(a);
System.out.println(str4);//123
}
}