java 基础语法学习
2024-10-09 05:37:00
注释
- 单行注释
- 多行注释
- 文档注释
public class HelloWorld {
public static void main(String[] args) {
//单行注释
//输出一个Hello World
System.out.println("hello,world!");
| | | |
| ---- | ---- | ---- |
| | | || | | |
| ---- | ---- | ---- |
| | | |
//多行注释 /* 注释 */
/*
我是多行注释
我是多行注释
我是多行注释
我是多行注释
我是多行注释
*/
//JavaDoc:文档注释
/**
* @Description HelloWorld
* @Author Chester
*/
//有趣的代码注释
///***********************************************
// * _ooOoo_ *
// * o8888888o *
// * 88" . "88 *
// * (| -_- |) *
// * O\ = /O *
// * ____/`---'\____ *
// * .' \\| |// `. *
// * / \\||| : |||// \ *
// * / _||||| -:- |||||- \ *
// * | | \\\ - * | | *
// * | \_| ''\---/'' | | *
// * \ .-\__ `-` ___/-. / *
// * ___`. .' /--.--\ `. . __ *
// * ."" '< `.___\_<|>_/___.' >'"". *
// * | | : `- \`.;`\ _ /`;.`/ - ` : | | *
// * \ \ `-. \_ __\ /__ _/ .-` / / *
// *======`-.____`-.___\_____/___.-`____.-'======*
// * `=---=' *
// *^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
// 佛祖保佑 永无BUG
//
// 本程序已经经过开光处理,绝无可能再产生bug
// **********************************************/
}
}
标识符和关键字
关键字
Java所有的组成部分都需要名字。类名、变量名、以及方法名都被称为标识符
数据类型讲解
public class demo2 {
public static void main(String[] args) {
//八大基本数据类型
//整数
int num1=10;//最常用
byte num2=20;
short num3=30;
long num4=30L;//lang类型要在数字后面加个L
//小数:浮点数
float num5=50.1F;//float类型要在数字后面j加个F
double num6=3.1415926233;
//字符
char name='中';
//字符串,String不是关键字,是类
String namea="你好";
//布尔值:是非
boolean flag=true;
boolean flag1=false;
}
}
数据类型扩展及面试题讲解
import java.util.Arrays;
public class demo3 {
public static void main(String[] args) {
// 整数扩展 进制 二进制0b 十进制 八进制0 十六进制0x
int i=10;
int i1=010; // 八进制0
int i2=0x1A; // 十六进制0x 0~9 A~F16
System.out.println(i);
System.out.println(i1);
System.out.println(i2);
System.out.println("==================================================");
// 浮点数扩展? 银行业务怎么表示?钱
// BigDecimal 数学工具类
//float 有限 离散 舍入误差 大约 接近但不等于
//double
//最好完全避免使用浮点数进行比较
//最好完全避免使用浮点数进行比较
//最好完全避免使用浮点数进行比较
float f=0.1f;
double d=1.0/10;
System.out.println(f==d); // flase
float d1=123456754321234567f;
float d2=d1+1;
System.out.println(d1==d2); //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 65=A) 2字节 0-65536 excel 2^16
//U0000 UFFFF
char c3='\u0061';
System.out.println(c3);
//转义字符 \t :制表符 \n :换行
System.out.println("==================================================");
String sa =new String("hello world");
String sb =new String("hello world");
System.out.println(sa==sb); //false
String sc ="hello world";
String sd ="hello world";
System.out.println(sc==sd); //true
//对象 从内存分析
//布尔值扩展
boolean flag=true;
if(flag){
}
//less is more ! 代码要精简易读
}
}
类型转换
public class demo4 {
public static void main(String[] args) {
int i=128;
byte b=(byte)i; //内存溢出
// 强制转换 高-》低
System.out.println(i); // 128
System.out.println(b); // -128
// 自动转换 低-》高
double c=i;
//操作比较大的数的时候,注意溢出问题
//jdk7新特性,数字之间可以用下划线分割
int money =10_0000_0000;
int years=20;
System.out.println(money*years); // -1474836480
long total =money*years;
System.out.println(total); //-1474836480
// 计算结果默认是int,转换之前已经存在问题了
total =money*(long)(years); //先把一个数转换成Long
System.out.println(total); // 20000000000
/*
注意点:
1. 不能对布尔值进行转换
2. 不能把对象类型转换为不相干的类型
3. 在把高容量转换到低容量的时候,强制转换
4. 转换的时候可能存在内存溢出,或者精度问题
*/
}
}
变量、常量、作用域
public class Demo5 {
//属性:变量
// 实例变量:从属于对象;如果不自行初始化,这个类型的默认值 0 0.0 u0000
// 布尔值:默认是false
//除了基本类型,其余的n默认值都是null
// 类变量 static
static double salary =2500;
// 常量 final
//修饰符,不存在先后顺序
public static final double PI=3.14;
String name;
int age;
// main方法
public static void main(String[] args) {
// 局部变量:必须声明和初始化值
int a;
int b;
int c;
String name="sada";
char x='X';
double pi=3.14;
// 变量类型
Demo5 dm5=new Demo5();
System.out.println(dm5.age); // 0
System.out.println(dm5.name); // null
// 类变量 static
System.out.println(salary);
//常量 final
System.out.println(PI);
}
//其他方法
public void add(){
}
}
基本运算符
位运算
public class Demo1 {
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*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);
}
}
一个关于string 与 int “+”运算的面试题
public class Demo2 {
public static void main(String[] args) {
int a=10;
int b=20;
//字符串连接符 + ,string
System.out.println(""+a+b); // 1020
System.out.println(a+b+""); // 30
}
}
包机制
JavaDoc生成文档
通过IDEA生成:
/**
* @author ZJ
* @version 1.0
* @since 1.8
*/
public class Doc {
String name;
/**
* @author ZJ
* @param name
* @return
* @throws Exception
*/
public String test(String name)throws Exception{
return name;
}
}
- 选择是整个项目还是模块还是单个文件
- 文档输出路径
- Locale 选择地区,这个决定了文档的语言,中文就是zh_CN
- 传入JavaDoc的参数,一般这样写 -encoding UTF-8 -charset UTF-8 -windowtitle “文档HTML页面标签的标题” -link http://docs.Oracle.com/javase/7/docs/api
通过命令行生成:
最新文章
- Eclipse - 修改默认user和类的创建日期
- KendoUI系列:Window
- 软件测试 -- 测试人员和QA的区别
- ie调试器
- PHP写日志什么时候需要加锁?
- [转]Cocos2d-x建工程时避免copy文件夹和库
- P2P直播承载平台与CDN直播承载平台比较
- hadoop namenode格式化问题汇总
- fasthttp中的协程池实现
- iOS APP打开其他应用
- java中函数的参数传递
- C++ 使用Lambda
- 通过dbutil操作数据库
- 移动互联网终端的touch事件判断方向
- 周鸿祎与85后的座谈(一):人人需要Mentor,世界没有奇迹
- python3 post方式上传文件。
- [转]让opencv输出人脸检测的得分(置信率)
- [Arc058E] Iroha and Haiku
- php之快速入门学习-10(数组)
- x86 x64下调用约定浅析