Java基础
Java基础
配置环境
注释
public class HelloWorld {
public static void main(String[] args) {
//单行注释:注释一行文字
//输出一个Hello,World!
System.out.println("Hello,World!");
//多行注释:注释一段文字
/*
我是多行注释
我是多行注释
*/
//JavaDoc:文档注释
/**
* @Description HelloWorld
* @Author 无双Java
*/
//有趣的代码注释
/*
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
佛祖保佑 永无BUG
*/
}
}
标识符和关键词
- 标识符大小写敏感
/*
每一个理论都有一个应用!
*/
public class Demo01 {
public static void main(String[] args) {
String 王者荣耀 = "百星王者";
//String 王者荣耀 = "倔强青铜";
System.out.println(王者荣耀);
//大小写敏感
String Man = "wushuang";
String man = "wushuang";
String name = "wushuang";
String hello = "wushuang";
String $hello = "wushuang";
String _hello = "wushuang";
String _1 = "wushuang";
//String 1hello = "wushuang";
//String #hello = "wushuang";
//String *hello = "wushuang";
}
}
数据类型
强类型语言
-
要求变量的使用严格符合规定,所有变量都必须定以后才能使用
安全性高
两大类
基本类型
-
整数(byte、short、int、long)
-
浮点(float、double)
-
字符
-
布尔类型(是非对错)
public class Demo02 { public static void main(String[] args) { //八大基本数据类型 //整数 int num1 = 10;//最常用 byte num2 = 20; short num3 = 30; long num4 = 30L;//Long类型要在数字后面加L //小数:浮点数 float num5 = 20.1F; double num6 = 3.1415926535; //字符 char name = 'A'; //字符串,String不是关键字、类名 //String namea = "wushuang"; //布尔值;是非 boolean flag = true; //boolean flag = false; } }
引用类型
类、接口、数组
字节
- 1bit表示1位
- 1Byte表示一个字节 1B = 8b
- 1024B = 1KB
数据扩展
public class Demo03 {
public static void main(String[] args) {
//整数拓展 进制 二进制0b 十进制 八进制0 十六进制0x
int i = 10;//十进制
int i2 = 010;//八进制
int i3 = 0x10;//十六进制 0-9 A-F
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
System.out.println("===========================");
//=======================================
//浮点数扩展? 银行业务怎么表示?钱
//BigDecimal 数学工具类
//=======================================
//float 有限 离散 舍入误差
//double
//最好完全避免使用浮点数进行比较
//最好完全避免使用浮点数进行比较
//最好完全避免使用浮点数进行比较
float f = 0.1f;//0.1
double d = 1.0/10;//0.1
System.out.println(f==d);//false
float d1 = 235461515256f;
float d2 = d1 + 1;
System.out.println(d2 == d1);//true
System.out.println("===========================");
//=======================================
//字符扩展?
//=======================================
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1);//强制转换
System.out.println(c2);
System.out.println((int)c2);
//所有的字符本质还是数字
//编码 Unicode 表:97 = a 2字节 0 - 65536
//U0000 - UFFFF
char c3 = '\u0061';
System.out.println(c3);//a
//转义字符
// \t 制表符
// \n 换行
// 。。。
System.out.println("Hello\nWorld");
System.out.println("===========================");
String sa = new String("hello world");
String sb = new String("hello world");
System.out.println(sa == sb);
String sc = "hello world";
String sd = "hello world";
System.out.println(sc == sd);
//对象,从内存分析
//布尔值扩展
boolean flag = true;
if (flag == true){};//新手
if (flag);//老手
//Less is More 代码要精简易懂
}
}
类型转化
public class Demo04 {
public static void main(String[] args) {
int i = 128;
byte b = (byte)i;//内存溢出
//强制转换 (类型)变量名 高-->低
//自动转换 低-->高
System.out.println(i);
System.out.println(b);
//注意点
/*
1. 不能对布尔值进行转换
2. 不能把对象类型转换为不相干的类型
3. 在把高容量转换到低容量时候,强制转换
4. 转换的过程可能会出现内存溢出,或精度问题
*/
System.out.println("====================");
System.out.println((int)23.7);//23
System.out.println((int)-45.89f);//-45
System.out.println("====================");
char c = 'a';
int d = c+1;
System.out.println(d);
System.out.println((char)d);
}
}
public class Demo05 {
public static void main(String[] args) {
//操作比较大的时候,注意溢出问题
//JDK7新特性,数字之间可以用下划线分割
int money = 10_0000_0000;
int years = 20;
int total = money * years;//-1474836480 计算的时候溢出了
long total2 = money * years;//默认是int,转换之前已经存在问题了
long total3 = money * ((long)years);//先把一个数转换为long
System.out.println(total3);
}
}
变量、常量、作用域
public class Demo07 {
//修饰符无先后顺序
static final double PI = 3.14;//常量
public static void main(String[] args) {
System.out.println(PI);
}
}
运算符
package operator;
public class Demo01 {
public static void main(String[] args) {
//二元运算符
int a = 10;
int b = 20;
int c = 25;
int d = 25;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);
}
}
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 123123123123123L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d);//Long
System.out.println(b+c+d);//Int
System.out.println(c+d);//Int
}
}
package operator;
public class Demo03 {
public static void main(String[] args) {
//关系运算符返回结果:正确,错误:布尔值
//if
int a = 10;
int b = 20;
int c = 21;
//取余,模运算
System.out.println(c%a);// c/a 21/10 = 2...1
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
}
}
package operator;
public class Demo04 {
public static void main(String[] args) {
//++ -- 自增,自减 一元运算符
int a = 3;
int b = a++;//先赋值,再自增
int c = ++a;//先自增,再赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
//幂运算 2^3 =2*2*2 = 8 很多运算用工具类操作!
double pow = Math.pow(2,3);
System.out.println(pow);
}
}
package operator;
//逻辑运算符
public class Demo05 {
public static void main(String[] args) {
//与、或、非
boolean a = true;
boolean b = false;
System.out.println("a && b:"+(a && b));//两个真才为真
System.out.println("a || b:"+(a || b));//两个假才为加
System.out.println("!(a && b):"+!(a && b));//假变真,真变假
//短路运算
int c = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(d);
System.out.println(c);//没走c++
}
}
package operator;
public class Demo06 {
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
*/
System.out.println(2<<3);
}
}
package operator;
public class Demo07 {
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);//1020
System.out.println(a+b+"");//30
}
}
package operator;
//三元运算符
public class Demo08 {
public static void main(String[] args) {
// x ? y : z
//如果x是真的为y,如果是假的为z
int score = 80;
String type = score < 60 ?"不及格":"及格";//必须掌握
//if
System.out.println(type);
}
}
JavaDoc
package com.wushuang.base;
/**
* @author wushuang
* @version 1.0
* @since 1.8
*/
public class Doc {
String name;
/**
*
* @param name
* @return
* @throws Exception
*/
public String test(String name) throws Exception{
return name;
}
}

浙公网安备 33010602011771号