public class Hello {
public static void main(String[] args) {
//单行注释
//输出一个hello,world
System.out.println("Hello,world!");
/*
多行注释
*/
//JavaDoc:文档注释 /** */
/**
* @Autor billwang
* @Description HelloWorld
*/
}
}
标识符:Java所有的组成部分都需要名字。类名、变量名以及方法名都被称为标识符。
标识符注意点
public class Demo02 {
public static void main(String[] args) {
//八大基本数据类型
//整数
int num1 = 10;//最常用
byte num2 = 20;
short num3 = 30;
long num4 = 4000L;//Long类型后面加个L
//小数
float num5 = 5.2F;
double num6 = 3.1415;
//字符
char name = ‘国‘;
String name2 = "中国";
//布尔值:是非
boolean flag = true;
//boolean flag = false;
}
}
public class Demo03 {
public static void main(String[] args) {
//整数拓展: 进制:二进制0b、十进制、八进制0、十六进制0x
int i = 10;
int i2 = 010;//八进制0
int i3 = 0x10;//十六进制 0-9 A-F 16
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 = 2312121212121212f;
float d2 = 2312121212121212f;
System.out.println(d1 == d2);//true
//============================================
//字符拓展?
//===================================
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 65 = A ,2字节 0-65536 Excel最长有2 16
//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);//false
System.out.println("====================");
String sc = "hello world";
String sd = "hello world";
System.out.println(sc == sd);//true
//布尔值拓展
boolean flag = true;
if (flag == true) { }//新手
if (flag) { }//老手
//less is More! 代码要精简易读
}
}
由于Java是强类型语言,所以要进行有些运算的时候,需要用到类型转换。
低 -------------------------------高
byte,short,char,int,long,float,double
运算中,不同数据类型先转换为同一类型,然后进行运算
强制类型转换
自动类型转换
public class Demo05 {
public static void main(String[] args) {
//操作比较大的数的时候,注意溢出问题
//JDK7新特性,数字之间可以用下划线分割
int money = 10_0000_0000;
int years = 20;
int total = money*years;
System.out.println(total);//-1474836480,计算的时候溢出了
long total2 = money*years;//默认是int,转换之前已经存在问题了
System.out.println(total2);//仍然为-1474836480
long total3 = money*((long)years);//先把一个数转换为Long
System.out.println(total3);
//L l区别,一般写大写L表示Long类型,不写小写l,因为会误认为是数字‘1’
}
}
变量:可以变化的量!
Java是一种强类型语言,每个变量都必须声明其类型。
Java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域。
tyep varName [=value] [{,varName[=value]}];
//数据类型 变量名 = 值;可以使用都好隔开来声明多个同类型变量。
注意事项:
变量作用域
public class Demo07 {
//类变量 static
static double salary = 2500;
//属性:变量
//实例变量:从属于对象;如果不自行初始化,这个类型的默认值 0 0.0
//布尔值:默认值是false
//除了基本类型,其余的默认值都是null;
String name;
int age;
//main方法
public static void main(String[] args) {
//局部变量:必须声明和初始化值
int i = 10;
System.out.println(i);
//变量类型 变量名字 = new Demo07();
Demo07 demo07 = new Demo07();
System.out.println(demo07.age);
System.out.println(demo07.name);
System.out.println(salary);
}
//其他方法
public void add(){
}
}
final 常量名=值;
final double PI = 3.14;
public class Demo08 {
//修饰符,不存在先后顺序
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) {
//二元运算符
//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(a/(double)b);
}
}
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 1212121212313L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c);//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) {
//关系运算符返回的结果: 正确,错误 布尔值
int a = 10;
int b = 20;
int c = 21;
//取余,模运算
System.out.println(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++;//先执行完这行代码,即先给b赋值,再自增
//b = a;a = a + 1;
System.out.println(a);
int c = ++a;//先自增,再执行这个代码,即a自增之后再赋值给c
//a = a + 1;c = a
System.out.println(a);
System.out.println(b);
System.out.println(c);
// 幂运算 2^3 2*2*2 = 8
double pow = Math.pow(3,2);
System.out.println(pow);
}
}
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 怎么计算最快,利用位运算来做:2<<3
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 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);
System.out.println(a+b+"");
}
}
package operator;
public class Demo08 {
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);
}
}
package pkg1[.pkg2[.pkg3]...];
import package1[.package2...].(classname|*);//*通配符,导入所有东西
package com.bill.base;
//类注释
/**
* @author bill
* @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;
}
//通过命令行 javadoc 参数 Java文件
/*
使用IDEA产生JavaDoc文档! 打开 idea,点击 Tools-> Generate JavaDoc,这样会打开生成 javadoc 文档的配置 页面。 面向百度编程!
*/
}
原文:https://www.cnblogs.com/wcwblog/p/14530581.html