字符类型
public class Main {
public static void main(String[] args) {
//单行注释
//多行注释/* 注释 */
//文档注释/** */
//输出一个HelloWorld
//字符型
int a = 10;
byte b = 127;
short c = 123;
long d = 1234L;//long型后面加L
//小数,浮点数
float f =50.1F;//float型后面加F
double g = 3.141592653589793238462643;
//字符
char name = '周';
//字符串
String name2 = "周子龙";
//布尔型
boolean flag = true;
//大小写十分敏感
System.out.println(name);
}
}
字符扩展
public class damn1 {
public static void main(String[] args) {
//整数扩展:进制 二 0b 十 正常 八 0开头 十六 0x
int i = 10;
int i2 = 010;
int i3 = 0x10;
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
System.out.println("========================================================");
//浮点数的扩展? 银行业务怎么表示?钱
//BigDecimal 数学工具类
//float 有限 离散 舍入误差 大约 接近但不等于
//double
//最好完全避免使用浮点数进行比较
//最好完全避免使用浮点数进行比较
//最好完全避免使用浮点数进行比较
float f = 0.1f;
double d = 1.0/10;
System.out.println(f==d);
float f1 = 2114141423434324f;
float f2 = f1 + 1;
System.out.println(f1==f2);
//字符扩展
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1);
System.out.println(c2);
System.out.println((int)c2);
//所有字符的本质都是数据
System.out.println("======================================");
//转义字符
// \t 制表符
// \ h 换行符
System.out.println("Hello\tWorld");
//布尔值扩展
boolean flag = true;
if(flag==true){}//老手
if(flag){}//新手
//二者相同 代码要精简易读
}
}
类型转换
public class damn2 {
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);
System.out.println((int)-45.89f);
System.out.println("==================");
char c = 'a';
int d = c+1;
System.out.println(d);
System.out.println((char)d);
}
}
==================================================
public class damn3 {
public static void main(String[] args) {
//操作比较大的数的时候,注意溢出问题;
//JDK7的新特性,数字之间可以用下划线分割
int money =10_0000_0000;
int years = 20;
int total = money*years;//溢出
long total2 = money*years;//默认是int,转换前已经出现问题了?
long total3 = money*((long)years);//先把一个转换为long型。
System.out.println(total3);
}
}
变量
public class damn4 {
//类变量 static
static double salary = 2500;
//属性:变量
//实例变量: 从属于对象;如果不自行初始化,这个类型的默认值为0 0.0
//布尔值:默认值为false
//除了基本类型,其余默认值都是null
String name;
int age;
//main方法
public static void main(String[] args) {
//局部变量:必须声明和初始化值
//int a,b,c
int i = 10;
System.out.println(i);
//变量类型 变量名字=new damn4();
damn4 damn4 = new damn4();
System.out.println(damn4.age);
System.out.println(damn4.name);
//类变量 static
System.out.println(salary);
}
//其他方法
public void add(){
}
}
常量
public class damn5 {
//修饰符不区分前后问题;
static final double pi = 3.14;
public static void main(String[] args) {
System.out.println(pi);
}
}
运算符
+-*/
package operater;
public class Demo1 {
public static void main(String[] args) {
//二元运算符
//Ctrl + D : 复制当前行到下一行
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((float) a/b);
}
}
++ --
package operater;
public class Demo4 {
public static void main(String[] args) {
//++ -- 自增,自减 一元运算符
int a = 3;
int b = a++;//先赋值后加加
int c = ++a;//先加加后赋值
System.out.println(a);
System.out.println(a);
System.out.println(b);
System.out.println(b);
System.out.println(c);
System.out.println(c);
//幂运算2^3 2*2*2 = 8 很多运算我们使用一些工具类
double pow = Math.pow(2,3);
System.out.println(pow);
}
}
<>
package operater;
public class Demo3 {
public static void main(String[] args) {
//关系运算符返回结果: 正确,错误 布尔值
int a = 10;
int b = 20;
int c = 21;
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(c%a);
}
}
与或非
package operater;
public class Demo5 {
public static void main(String[] args) {
//与(and) 或(or) 非(取反)
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);
}
}
位运算
package operater;
public class Demo6 {
public static void main(String[] args) {
/*
A = 0011 1100
b = 0000 1101
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001 相同为0,不同为1
~B = 1111 0010
<< *2
>> /2
*/
}
}
==============================================
package operater;
public class Demo7 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b;
System.out.println(a);//a = a+b
//字符串连接符 + ,String
System.out.println(""+a+b);//字符串相加
System.out.println(a+b+"");//前面相加
}
}
三元组运算符
package operater;
//三元运算符
public class Demo8 {
public static void main(String[] args) {
//x?y:z
//如果x==true,则结果为y,否者为z。
int score = 80;
String type = score<60 ?"不及格":"及格";
System.out.println(type);
}
}
包
package com.zzl.operater;
//次包必须顶行
//避免包名重复
//alt + enter 查看错误
//.*导入包下所有类
import java.util.Date;
import com.zzl.damn.damn1;
public class Demo1 {
public static void main(String[] args) {
//二元运算符
//Ctrl + D : 复制当前行到下一行
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((float) a/b);
}
}
javadoc生成文档
package com.zzl.damn;
/**
* @author kuangshen
* @version 1.0
* @since 1.8
*/
public class Doc {
String name;
/**
* @author kuangshen
* @param name
* @return
*/
public String test(String name){
return name;
}
//我是通过命令行javadoc
//作业:学会查找使用IDea生产javadoc文档! 面向百度编程!
}