Java是一门面向对象的语言。
面向过程是一种以过程为中心的编程思想。这些都是以什么正在发生为主要目标进行编程。(主要是实现功能的函数开发)
面向对象是相对于面向过程来讲的,面向对象方法,把相关的数据和方法组织为一个整体来看待,从更高的层次来进行系统建模,更贴近事物的自然运行模式。(抽象出类、属性及其方法,然后通过实例化类、执行方法来完成功能)
Java三大版本JavaSE: 包含桌面程序,以及控制台开发
JavaME:嵌入式开发,包含手机,小家电等
JavaEE: web端,服务器开发
第一个Java程序public class Demo01 {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
注释
单行注释
// 这里是单行注释多行注释
文档注释
关键字
| abstract | assert | boolean | break | byte |
|---|---|---|---|---|
| case | catch | char | class | const |
| continue | default | do | double | else |
| enum | extends | final | finally | float |
| for | goto | if | implements | import |
| instanceof | int | interface | long | native |
| new | package | private | protected | public |
| return | strictfp | short | static | super |
| switch | synchronized | this | throw | throws |
| transient | try | void | volatile | while |
- 所有标识符都应该由字母(大小写)," $ "符号,或者下划线 " _ "开始
- 首字母之后的可以由字母(大小写)," $ "符号,或者下划线 " _ "或者是数字
- 不能使用关键字作为变量名
- 可以使用中文(但是极其极其不推荐)
Java的基本数据类型有8种,分别是:byte(位)、short(短整数)、int(整数)、long(长整数)、float(单精度)、double(双精度)、char(字符)和boolean(布尔值)。
public class Demo02 {
public static void main(String[] args) {
System.out.println(Byte.MAX_VALUE); //127
System.out.println(Byte.MIN_VALUE); //-128
System.out.println(Short.MAX_VALUE); //32767
System.out.println(Short.MIN_VALUE); //-32768
System.out.println(Integer.MAX_VALUE); //2147483647
System.out.println(Integer.MIN_VALUE); //-2147483648
System.out.println(Long.MAX_VALUE); //9223372036854775807
System.out.println(Long.MIN_VALUE); //-9223372036854775808
System.out.println(Float.MAX_VALUE); //3.4028235E38
System.out.println(Float.MIN_VALUE); //1.4E-45
System.out.println(Double.MAX_VALUE); //1.7976931348623157E308
System.out.println(Double.MIN_VALUE); //4.9E-324
}
}
Java中的char类型由两个字节即十六位来表示,因为是无符号数,所以为2的16次方,数值范围就为:0 - 2^16-1;
引用数据类型类,接口,数组,像是 String 就是应用数据类型。引用地址储存在栈中,但是实际的数据储存在堆中。
类型转换低 byte,short,char --> int --> long --> float --> double 高
public class Demo03 {
public static void main(String[] args) {
byte a = 1;
int b = 1;
float c = 2;
double d = 1;
a = (byte) b;
b = (int) c;
d = c ;
//int等转String java是大小写敏感的
String text ;
text = String.valueOf(b);
text = b + "";
//String转int
String number = "114514";
b = Integer.parseInt(number);
b = Integer.parseInt(number);
}
}
注意点:
- 不能对布尔型进行转换
- 不能将其转换成不相干的类型
- 在高转低时,需要进行强转换
- 在转换时,可能会有精度的变化
//int等转String java是大小写敏感的 String text ; text = String.valueOf(b); text = b + ""; //String转int String number = "114514"; b = Integer.parseInt(number); b = Integer.parseInt(number);变量与作用域 变量
变量:即是我们对一个可以进行变化的值进行的定义。
其语法是 type(类型) 变量名 = 值;
其类型可以是各种数据类型。并且我们可以在后面添加逗号来进行多个相同类型的变量的定义。
作用域public class Demo04 {
//实例变量,可以理解成在当前类的变量,从属于当前类
int c = 2;
//类变量
static String text = "diamond";
//常量
final double PI = 3.1415926;
//main方法
public static void main(String[] args) {
Demo04 demo04 = new Demo04();
System.out.println(demo04.c);
System.out.println(text);
}
//其他方法
public int addNumber(int a,int b){
//ans是当前方法内的局部变量
int ans = a + b + c;
System.out.println(text);
return ans;
}
}
常量
在进行初始化后就不会再改变,所以适合储存一些不变量,像是Π ,e 等
一般来说常量名都会使用全大写字母。
//常量 final double PI = 3.1415926;运算符 Math类
public class Demo05 {
public static void main(String[] args) {
int num = -1;
//绝对值
System.out.println(Math.abs(num));
//幂运算
System.out.println(Math.pow(2,3));
//根号
System.out.println(Math.sqrt(4));
}
}
三元运算符
int a = 10 ; int b = 10 ; int c = a > b ? a++ : a-- ; System.out.println(c); System.out.println(a);Scanner类
我们可以通过使用Scanner类来进行对用户输入数据的收集
public class Demo06 {
public static void main(String[] args) {
String scannerText ;
Scanner scanner = new Scanner(System.in);
scannerText = scanner.next();
System.out.println("宁输入的是: "+ scannerText);
scanner.close();
}
}
执行顺序
顺序执行
就如其名字是顺着其名字来进行执行数据的。
选择结构 if单选择public class Demo07 {
public static void main(String[] args) {
int a;
Scanner scanner = new Scanner(System.in);
a = scanner.nextInt();
if (a == 1 ){
System.out.println("宁输入了 1 ");
}
}
if多选择
public class Demo07 {
public static void main(String[] args) {
int a;
Scanner scanner = new Scanner(System.in);
a = scanner.nextInt();
if (a > 0) {
System.out.println("宁输入了正数");
}else {
System.out.println("宁输入了负数");
}
}
}
if多选择
import java.util.Scanner;
public class Demo07 {
public static void main(String[] args) {
int a;
Scanner scanner = new Scanner(System.in);
a = scanner.nextInt();
if (a > 0) {
System.out.println("宁输入了正数");
}else if (a < 0){
System.out.println("宁输入了负数");
}else {
System.out.println("宁输入了 0 ");
}
}
}
Switch选择
public class Demo08 {
public static void main(String[] args) {
char grade = 'A';
switch (grade){
case 'A':
System.out.println("Perfect");
break;
case 'B':
System.out.println("Great");
break;
case 'C':
System.out.println("Good");
break;
case 'D':
System.out.println("Miss");
}
}
}
循环结构
While循环
public class Demo09 {
public static void main(String[] args) {
int num = 10 ;
while (num > 0){
System.out.println(num);
num--;
}
}
}
DoWhile循环
public class Demo09 {
public static void main(String[] args) {
int num = 10 ;
while (num > 0){
System.out.println(num);
num--;
}
System.out.println("跳出第一次循环 "+num);
do {
System.out.println(num);
num--;
}while (num > 0);
System.out.println("第二次循环后 "+num);
}
}
For循环
public class Demo10 {
public static void main(String[] args) {
int i = 1919810;
for (i = 0; i < 10; i++) {
System.out.println(i);
}
}
}
流程控制
Break
跳出当前循环
public class Demo11 {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 5){
break;
}
System.out.println(i);
}
}
}
Continue
跳出当次循环
public class Demo11 {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 5){
continue;
}
System.out.println(i);
}
}
}
Goto
在C/C++中,goto常被用于跳出多重循环。但goto 语句的使用往往会使程序的可读性降低,所以 Java 不允许 goto 跳转。实际上,自从“goto有害论”提出后,软件开发就不建议使用goto了,但是Java中依然保留了goto这个关键字留作备用,但这个关键字没有任何作用,只是为了将来可能的扩展,防止使用goto作为程序中的标识符。
类似地,Java中的const也只是一个不起作用的保留关键字(不具备C语言中定义常量的作用,Java中要想定义常量使用final关键字),与goto一样防止作为程序中的标识符。
; i < 10; i++) {
if (i == 5){
continue;
}
System.out.println(i);
}
}
}
Goto 在C/C++中,goto常被用于跳出多重循环。但goto 语句的使用往往会使程序的可读性降低,所以 Java 不允许 goto 跳转。实际上,自从“goto有害论”提出后,软件开发就不建议使用goto了,但是Java中依然保留了goto这个关键字留作备用,但这个关键字没有任何作用,只是为了将来可能的扩展,防止使用goto作为程序中的标识符。 类似地,Java中的const也只是一个不起作用的保留关键字(不具备C语言中定义常量的作用,Java中要想定义常量使用final关键字),与goto一样防止作为程序中的标识符。



