一.JAVA JDK 汇总

二.JDK 常用命令
三.关键字与标识符
关键字的定义和特点
定义:被Java语言赋予了特殊含义的单词
特点:关键字中所有字母都为小写
abstract default if private this
boolean do implements protected throw
break double import public throws
byte else instanceof return transient
case extends int short try
catch final interface static void
char finally long strictfp volatile
class float native super while
const for new switch
continue goto package synchronized
一共48个,其中最常见的三个关键字 true,false,null,都不是关键字,只是作为一个单独标识类型
其中,很少用的关键字:const,goto,native,strictfp,transient,volatile, 注意:const 和 goto 作为java 中保留字
1.native 是方法修饰符 ,native 方法是由另外一种语言(c/c++/汇编等)实现的本地方法,native可以和其他一些修饰符连用,但是 abstract 和接口interface 方法不能用native 修饰
eg:
public interface LoadInterface
{
void doSomeMethod();
}
public class Load implements LoadInterface
{
public native void doSomeMethod();
private native int doSomeMethodB();
public native synchronized String doSomeMethodC();
static native void doSomeMethodD();
}
strictfp
精确浮点, 修饰类和方法,当一个类和接口用strictfp声明,内部所有的float和double表达式都会成为strictfp的,interface 方法不能用strictfp修饰,class当中可以声明
eg:
strictfp interface LoadTest {
void methodA();
}
class A implements LoadTest {
public void methodA() {
}
public void methodB() {
}
public strictfp void methodC() {
}
}
class B {
strictfp void methodA() {
}
}
transient
变量修饰符 ,在对象存储时,这些变量的状态不会被持久化
volatile
修饰变量,并发线程中访问共享变量,都强迫读取共享内存中的成员的值,若成员值发生改变,强迫线程变化值写入共享内存
eg:
class Test
{
static int m = 0,n = 0;
static void methodA(){
m++;
n++;
}
// 此成员数据会出现不一致
static void methodB(){
System.out.println("m="+m+",n="+n);
}
}
这里 成员变量 m和n 会出现几次更新不一致状态,避免发生此情况,mehtodA 与mehtodB 加上同步sychronized
代码如下:
class Test
{
static int m = 0,n = 0;
static synchronized void methodA(){
m++;
n++;
}
// 此成员数据就所有并发线程共享了
static synchronized void methodB(){
System.out.println("m="+m+",n="+n);
}
}
更简洁的写法,并发线程共享的成员变量用 volatile 声明
代码实现:
class Test
{
// volatile 修饰成员变量,并发线程共享
static volatile int m = 0,n = 0;
static void methodA(){
m++;
n++;
}
// 此成员数据就所有并发线程共享了
static void methodB(){
System.out.println("m="+m+",n="+n);
}
}
四.变量与常量
public class Test{
public static void main(String args[]){
int i=100;
String binStr=Integer.toBinaryString(i);
String otcStr=Integer.toOctalString(i);
String hexStr=Integer.toHexString(i);
System.out.println(binStr);
}
}
java中的常量
整数常量。所有整数
小数常量。所有小数
布尔型常量。较为特有,只有两个数值。true false。
字符常量。将一个数字字母或者符号用单引号( ' ' )标识。
字符串常量。将一个或者多个字符用双引号标识。
null常量。只有一个数值就是:null
五.运算符
= , +=, -=, *=, /=, %=
eg:
int a,b,c; a=b=c =3;
int a = 3; a+=5;等同运算a=a+5;
思考:有什么区别?
short s = 3;
s=s+2;运算后进行类型提升,装不进short类型型中,编译失败
s+=2; 在运算的过程中进行类型转换,编译可以通过
class OperateDemo2
{
public static void main(String[] args)
{
//赋值运算符。= += -= *= /= %=
// int a,b,c;
// a = b = c = 4;
//int a = 4;
//a+=2;//a = a + 2;
short s = 3;
//s+=4;
s = (short)(s + 4);
System.out.println("s="+s);
}
}
&与运算
|或运算6
^异或运算
~反码
代码实现:
class OperateTest2
{
public static void main(String[] args)
{
/*
对一个整数的最后一个字节,高四位和低四位进行换位。
int num = 0101-1100 & 255;
1100-0101
61 = 0011-1101
1101-0011 = 211
*/
int num = 61;
int n1 = num & 15;//低四位
int n2 = num & (15<<4);//高四位
int n = n1<<4 | n2>>>4;
System.out.println("n="+n);
}
}
class OperateDemo5
{
public static void main(String[] args)
{
int x = 0,y;
y = (x>1)?100:200;
System.out.println("y="+y);
//获取两个整数中的较大的整数。
int a,b;
int max = a>b?a:b;
//获取三个整数中的较大的整数。
int o,p,q;
int temp = o>p?o:p;
int max2 = temp>q?temp:q;
}
}