Java基础(课后笔记)
Java三大版本
- JavaSE:标准版(桌面程序,控制台开发)
- JavaME:嵌入式开发(手机,小家电)
- JavaEE:E级企业级开发(web端,服务器开发)
JDK、JRE、JVM
- JDK: Java Development Kit
- JRE: Java Runtime Environment
- JVM: Java Virtual Machine
数据类型
Java是强类型语言,要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用。
- 基本类型(primitive type)
- 引用类型(reference type)
public class Demo02{
public static void main(String[] args){
String a="hello";
int num=10;
system.out.println(a);
system.out.println(num);
}
}
public class Demo02{
public static void main(String[] args){
//八大基本数据类型
//整数
int num1 = 10;//最常用
byte num2 = 20;
short num3 = 30;
long num4 = 40L;//Long类型要在数字后面加上L
//小数:浮点数
float num5 = 50.1F;//Lfloat类型要在数字后面加上F
double num6 = 3.14159262324432264387;
//字符
char name = 'A';
//字符串String不是关键字,类
String namea = "暴富";
//布尔值:是非
boolean flag = true;
//boolean flag = flase;
}
}
变量
- Java是一种强类型语言,每个变量都必须声明其变量
public class Demo{
public static void main(String[] args){
//int a,b,c;
//int a=1,,b=2,c=3;
String name = "qinjiang";
char x = 'X';
double pi = 3.14;
}
}
- 作用域:类变量、局部变量,实例变量
public class Demo{
//类变量 static
static double salary = "2500";
//属性:变量
//实例变量:从属于对象;如果不自行初始化,这个类型的默认值 0 0.0
//布尔值:默认是flase
//出了基本类型,其余的都是null
//main方法
String name;
int age;
public static void main(String[] args){
//局部变量:必须声明和初始化
int i = 10;
System.out.println(i);
//变量类型 变量名字=new Demo();
Demo demo = new Demo();
System.out.println(demo.age);
System.out.println(demo.name);
//类变量 static
System.out.println(salary);
}
//其他方法
public void add(){
//
}
}
常量
- 初始化后不能再改变值!不会变动的值。
- 一般用字母大写表示
public class Demo{
//修饰符,不存在先后顺序
static final double PI = 3.14;
public static void main(String[] args){
System.out.println(PI);
}
}
运算符
- 算术运算符:+,-,*,/ , % , ++ , --
- 赋值运算符=
- 关系运算符:>,<,>=,<=,==,!=instanceof
- 逻辑运算符:&&,||,!
- 位运算符:& , | ,^,~,<<,>> , >>>(了解!!!)
- 条件运算符 ? :
- 扩展赋值运算符:+=,-=,*= , /=
package operator;
public class Demo{
public static void main(String[] args){
//++ -- 自增,自减 一元运算符
int a=3;
int b=a++;//a++ a=a+1执行完这行代码后,先给b赋值,再自增
int c=++a;//++a a=a+1执行完这行代码前,先自增,再给b赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
//幂运算 2^3
double pow = Math.pow(2,3);
System.out.println(pow);
}
}
逻辑运算
package com;
public class Se01 {
public static void main(String[] args) {
//与(and) 或(or) 非(取反)
boolean a = true;
boolean b = false;
System.out.println("a && b:" +(b&&a));//逻辑与运算:两个变量都为真,结果才为true
System.out.println("a || b:" +(a||b));//逻辑或运算:两个变量有一个为真,结果为true
System.out.println("! (a && b):" +!(a&&b));//如果是真,则变为假,如果是假则变为真
//短路运算
int c = 5;
boolean d= (c<4)&&(c++<10);
System.out.println(d);
System.out.println(c);
}
}
位运算符
package com;
public class Se02 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
----------------------------
A&B = 0000 1100
A/B = 0011 1101
A^B = 0011 0001
~B = 1111 0010
2*8= 16 2*2*2*2
<< *2
>> /2
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
0001 0000 16
*/
System.out.println(2<<3);
}
}
字符串连接
package com;
public class Se03 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b;//a=a+b
a-=b;//a=a-b
System.out.println(a);
//字符串连接 +,String
System.out.println(""+a+b);
System.out.println(a+b+"");
}
}
条件运算符(三元运算符)
package com;
public class Se04 {
public static void main(String[] args) {
//x ? y :z
//如果x==true,则结果为y,否则结果为z
int score = 80;
String type = score <60?"不及格":"及格";
//if
System.out.println(type);
}
}

浙公网安备 33010602011771号