1 使用eclipse开发java
eclipse是集成开发环境的(IDE)类软件。
首先从打印helloworld开始。
*1 创建java项目
*2 创建程序包
*3 编写java程序
*4 运行Java程序
public class Helloworld{
public static void main(String[] args)
{
System.out.println("helloworld");
}
}
打印结果
helloworld
2 常量和变量
*1 关键字
/*
* 1: 关键字
概念:被java赋予特定含义的单词:
高级记 事本:notepad++,editplus等等这些工具对关键字有特殊的颜色标记
开发工具:eclipse,myeclipse也是对关键字有特殊颜色标记
组成关键字:字母全小写
eclipse:
alt+/:自动提示,代码补全:神键
复制代码:ctlr+alt+向下方向键
删除代码:选中代码:ctrl+d
注释:多行注释:ctrl+shift+/
单行注释:选中:ctrl+/
判断下列哪些是关键字?
class,HelloWorld,public,static,void,main,String,System
* */
public class HelloWorld {
public static void main(String[] args) {
//输出语句
System.out.println("helloworld");
}
}
*2标识符
/*
* 标识符:是一种规则;给包,类名,接口,方法,变量起名字的字符序列!
*
* 组成规则:
* 1)英文大小写字母
* 2)数字字符
* 3)_和$组成
*
* 软件开发:
* 需求分析
* 系统分析
* 功能,性能测试(测试工程师)
* 给类,接口,变量,方法命名要遵循四个字:见名知意
* Student:学生
* S:
* 1)包(目录)的命名:不管是单级包还是多级包:字母全都小写,写法:公司域名的反写(后面在跟上哪个部门)
* 单级包:
* 举例:
* org
* java的源文件...
* 多级包:
* org.westos
* org
* westos
* java的源文件..
* 2)类和接口的命名:
* 单个单词:首字母大写,其余小写
* 举例:Demo
* 多个单词:每个单词的首字母都大写,其余小写
* 举例:HelloWorld
*
* 3)给变量和方法的命名:
* 单个单词:字母全部小写
* 举例:main():程序的主入口 求两个数据之后:sum()
*
* 多个单词:第一个单词字母全部小写,从第二个单词开始,每个单词的首字母都大写,其余小写
* 举例:checkUserName(),checkPassword()
* 4)常量的命名:
* 单个单词:字母全部大写
* 举例:HELLO
* 多个单词:字母全部大写,每一个单词和单词之间用下划线隔开
* 举例:HELLO_WORLD
* 注意事项:
* 1)不能出现java中的关键字
* 2)不能以数字开头
* 3)java严格区分大小
* */
public class MakeNameDemo {
public static void main(String[] args) {
//定义变量
int a = 10 ;
//不能以数字开头
// int 1b = 20 ;
//不能是java中的关键字
// int class = 20 ;
// int Class = 20 ;
}
}
*3 常量
/
*
* 常量:在程序的执行过程中,其值不发生改变的量;
* 常量的分类:
* 1:字面值常量
* a:字符串常量:使用双引号括起来的内容:
* "helloworld","我爱高圆圆"....
* b:字符常量:使用单引号括起来的单个字母或者数字
* 'A','a','0'
* 'ab':错误的写法
* c:整数常量:
* 100,1000
* d:小数常量
* 3.1415926...
* e:布尔常量:
* true或者false
* f:空常量:
* null
*
* 2:自定义常量(与关键字:final有关:面向对象部分讲解)
* 面试题:final,finally,finalize这三个的区别?(后面讲)
*
*
*
* */
public class ConstantDemo {
public static void main(String[] args) {
//字符串常量
System.out.println("helloworld");
//字符串 +(拼接符号) 任何数据 = 字符串
System.out.println("helloworld"+'a'+1);//helloworlda1
System.out.println('a'+1+"helloworld");//98helloworld
System.out.println("5+5="+5+5);//5+5=55
System.out.println(5+5+"=5+5");//10=5+5
System.out.println("------------------------");
//字符常量
//不参与运算
System.out.println('A');
System.out.println('a');
System.out.println('0');
// System.out.println('ab');
System.out.println("------------------------");
//当字符参与运算的时候,就需要带ASCII码表中找字符对应的值
//'A'---->65
//'a'---->97
//'0'---->48
System.out.println('A'+1);
System.out.println('a'+1);
System.out.println('0'+1);
System.out.println("----------------------");
//整数常量
System.out.println(100);
//小数常量
System.out.println(3.1415926);
System.out.println("-------------------");
//布尔常量
System.out.println(true);
System.out.println(false);
//空常量
String s = null ;//针对引用类型
System.out.println(s);
}
}
*4 变量
/*
* 变量:在程序的执行过程中,某一个范围内其值发生改变的量!
* 对于变量的数据类型的分类:
*
* java高级特性:
* jdk5.0以后的新特性:
* 自动 拆装箱
* int基本数据类型---->引用类型:Integer ---->valueOf()
*
* Integer f1 = 100;===>Integer f1 = new Intger(100) ;
* Integer f2 = 100 ;
* Sop(f==f2) ; true
* Integer b1 = 150 ;
* Integer b2 = 150 ;
* Sop(b1==b2);false
* char基本数据类型--->引用类型:Character
* A :基本数据类型:
* 4类八种
* a:整数类型:整数类型默认该类型 占字节数 取值范围
* byte:字节类型 1 -128~127
* short:短整型 2
* int:整数类型默认该类型 4
* long:长整型
* b:浮点类型:默认double类型
* 单精度:float类型 4
* 双精度:double类型 8
* c:字符类型:
* char 2
* d:布尔类型(逻辑判断上)
* boolean 1
* 布尔类型默认值:false
* B :引用类型(面向对象讲)
* 数组也是引用类型
* 对于整数类型:
* 默认的类型:int类型
* 使用long类型定义变量,在当前变量值的后面加上L或者l(推荐使用L),告诉系统我定义的是一个long类型
* 浮点类型:默认双精度:double
* 定义float类型,在该变量值的末尾跟上F或者f
* */
public class DataTypeDemo {
public static void main(String[] args) {
//byte类型
byte b = 100 ;
System.out.println(b);
System.out.println("--------------");
//短整型
short s = 20 ;
s = 50 ;
System.out.println("s:"+s);
System.out.println("--------------");
// int i = 1000000000000;
// System.out.println("i:"+i);
long l = 1000000000000L;
System.out.println("l:"+l);
//浮点类型:默认double类型
double d = 12.56 ;
System.out.println("d:"+d);
//单精度
float f = 12.34F ;
System.out.println("f:"+f);
//由于浮点类型存储和整数类型的存储不一致.导致:永远是一个近似值:BigDecimal
// System.out.println(1.0-0.32);
//char类型
char ch = 'A' ;
System.out.println(ch);
//boolean类型
boolean flag = true;
System.out.println(flag);
}
}
3 运算符
*1 算数运算符
/*
* java中的运算符:
* 1)算术运算符
* 2)赋值运算符
* 3)比较(关系)运算符
* 4)逻辑运算符
* 5)位运算符
* 6)三元(三目)运算符
*
* 1)算术运算符:
* 基本的算术运算符:+,-,*,/,%(取余数)
*
* /:默认取整
*
* */
public class OperatorDemo {
public static void main(String[] args) {
//定义两个变量
int x = 3 ;
int y = 4 ;
System.out.println(x+y);
System.out.println(x-y);
System.out.println(x*y);
System.out.println(x/y);
//想让当前结果计算具体的小数,分子都可以乘以浮点类型的数据
System.out.println(x*1.0/y);
System.out.println(x%y);
}
}
- 2 赋值运算符
*
```
/*
* 赋值运算符
* 基本的赋值运算符:=
* 表达的意思:等号右边的数据赋值左边的变量
* 扩展的赋值运算符:
* +=,-=,*=,、=,%=
*
* 举例:+=
* 表达的意思:将当前符号右边的数据和左边的数据相加赋值给左边的数据
* */
public class OperatorDemo {
public static void main(String[] args) {
//定义变量
//1)给定初始化值
int a = 10 ;
//2)先定义,使用该变量之前必须赋值,否则报错
// int b ;
// System.out.println(b);//当前该变量没有被赋值
System.out.println(a);
int m = 100 ;
m += 200;//相当于m = m + 200 ;
System.out.println("m:"+m);
}
}
*3 比较运算符
/*
* 关系运算符:
* <,>,<=,>=,==,!=
*
* 符号连接的表达式结果都是boolean类型
*
* */
public class OperatorDemo {
public static void main(String[] args) {
//定义变量
int x = 3 ;
int y = 4 ;
int z = 3 ;
//== 不能写出=
System.out.println((x+y)==(x+z));
System.out.println((x+y)==(y+z));
System.out.println(“———————”);
System.out.println(x!= y);
//定量两个变量
int a = 10 ;
int b = 20 ;
boolean flag = (a==b) ;
//boolean flag = (a=b) ;导致类型不匹配
}
}
*4 逻辑运算符
/*
* 逻辑运算符:
* 通过逻辑符号来连接boolean的表达式或者值;
* 逻辑运算符中基本的运算符:
* 逻辑单与:&(并列,并且的意思)
* 特点:有false,则false
* 逻辑单或:|
* 特点:有true,则true
* 逻辑异或:^
* 特点:相同则为false,不同则为true(简单记:(男女关系)男男 女女 男女 女男)
* 逻辑非:!
* 非false,则true,非true则false;偶数个非是该数据本身的boolean类型的值
*
* 扩展的逻辑运算符:(实际开发中)
* 逻辑双与:&&
* 逻辑双或:||
* */
public class OperatorDemo {
public static void main(String[] args) {
//定义三个变量
int a = 3 ;
int b = 4 ;
int c = 5 ;
//逻辑单与:&
System.out.println((a>b) & (a>c));
System.out.println((a>b) & (a<c));
System.out.println((a<b) & (a<c));
System.out.println((a<b) & (a>c));
System.out.println("-------------------------------");
//逻辑单或|
System.out.println((a>b) | (a>c));
System.out.println((a>b) | (a<c));
System.out.println((a<b) | (a<c));
System.out.println((a<b) | (a>c));
System.out.println("-------------------------------");
//逻辑异或^
System.out.println((a>b) ^ (a>c));
System.out.println((a>b) ^ (a<c));
System.out.println((a<b) ^ (a<c));
System.out.println((a<b) ^ (a>c));
System.out.println("-------------------------------");
System.out.println(!(a>b));
System.out.println(!!(a>b));
}
}
*5 位数运算符
/*
* 位运算符:
* 位与:&
* 位:|
* 位异或:^
* ~:反码
* <<,>>,>>>
* */
public class OperatorDemo {
public static void main(String[] args) {
//位运算符
System.out.println(3 & 4);
System.out.println(3 | 4);
System.out.println(3 ^ 4);//(位异或:重点)
System.out.println(~3);
}
}
/*
* 计算出int类型3和4对应的二进制数据
* 00000000 00000000 00000000 00000011
* 00000000 00000000 00000000 00000100
*位与:&
* 特点:有0则0
* 00000000 00000000 00000000 00000011
* 位与&:00000000 00000000 00000000 00000100
* -------------------------------------------------------
* 结果: 00000000 00000000 00000000 00000000
* 0
* 位或|:
* 特点:有1,则1
* 00000000 00000000 00000000 00000011
* 位与|:00000000 00000000 00000000 00000100
* -------------------------------------------------------
* 00000000 00000000 00000000 00000111
* 结果:7
*
* 位异或:^
* 特点:相同则为0,不同则为1
* 00000000 00000000 00000000 00000011
* 位与^:00000000 00000000 00000000 00000100
* ------------------------------------------------------
* 00000000 00000000 00000000 00000111
* 结果:7
*
* 反码:~:特点:按位取反(底层对补码进行按位取反)
* 00000000 00000000 00000000 00000011 原码,反,补
* ~ --------------------------------------------------
*
* 最高符号位 数值位
* 1 1111111 11111111 11111111 11111100(补码)--->原码
* -1
* 1 1111111 11111111 11111111 11111011 --->反码
* 1 0000000 00000000 00000000 00000100
* - 4
*
* 结果:-4
*
*
*
*
*
*
* */
*6 三目运算符
/*
* 三元运算符==>三目运算符
*
* 单目运算符:~3
* 双目运算符:3+4
*
* 三元运算符
*
* 格式:
* (表达式)? true的结果 : false的结果;
* 执行流程:
* 如果表达式成立,执行true的结果
* 如果表达式不成立,执行false的结果;
*
* */
public class OperatorDemo {
public static void main(String[] args) {
//求两个数据是否相等
int a = 100;
int b = 200 ;
//用三元运算符实现
// boolean flag = (a==b) ? true : false;//==表示两个数据进行比较
// System.out.println("flag:"+flag);
//优化改进
boolean flag = a==b ;
System.out.println(flag);
//求两个数据中的最大值
//先定义变量max
int max = (a>b) ? a : b ;
System.out.println("两个数据中的最大值是:"+max);
System.out.println("-----------------------");
//求三个数据中的最大值
int m = 10 ;
int n = 50 ;
int z = 20 ;
//可以使用中间第三方变量进行实现,实际开发中就使用第三方进行实现
// int temp = (m>n) ? m : n ;
// //定义max2
// int max2 = (temp > z) ? temp : z ;
//一步走:
int max2 = (m>n) ? ((m>z)? m: z) : ((n>z)? n: z) ;//不建议使用
System.out.println("三个数据中的最大值是:"+max2);
}
}