java自学语法_java 基础语法学习
注釋
單行注釋
多行注釋
文檔注釋
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
通過命令行生成:
總結
以上是生活随笔為你收集整理的java自学语法_java 基础语法学习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: grep的java源程序_Java实现G
- 下一篇: rsa java ao_RSA加解密工具