JAVA认证 百分网手机站

java学习技巧

时间:2018-01-03 15:45:31 JAVA认证 我要投稿

java学习技巧

  1、byte通常用来进行位运算,位宽度窄,一般不用来计算

java学习技巧

  2、关键字都是小写的,在eclipse中显示为红色。

  3、变量给了缺省的初始值,C语言没给,只给分配了空间,里面的数不确定

  4、char的缺省值是ASCII码中第1个

  5、运行效率:i++>i+=1>i=i+1

  6、布尔值不能进行大小比较,只能进行==比较

  7、先算&&再算||。另外&&为短路与的意思。

  例1:判断以下i的变化。

  int i=2,j=3;

  boolean b=i>j && i++>2;

  System.out.println(i);

  答案:2

  例2:以下在a,b,i不知道的情况下,判断真还是假。

  ((a>b)||((3-2)>2))||(5>2)&&(true||(++i>2))

  答案:真

  8、>>带符号右移,前面移空的位置添加符号位相同的数

  0|001 1000 右移两位(正数)

  0|000 0110

  1|001 1000 右移两位(负数)

  1|111 1100

  >>>带符号右移,前面移空的位置添加0

  9、获得-5到2的随机数

  int i;

  Random r=new Random();

  i=r.nextInt();

  // i=Math.abs(i%10)+1;//获得0到10的随机数

  i=Math.abs(i%8)-5;//获得-5到-2的随机数

  System.out.println(i);

  10、数组创建时,大小(内存)可以是前面的变量.可以动态创建数组的大小(内存),创建后就不能再改大小.

  例:

  int t=9;

  int[][] jiu;

  jiu=new int[t][];

  11、变量的作用域。

  定义的数个变量其实是放在一个栈的结构中,后定义的变量先消失,先定义的变量后消失,作用域比后定义的变量大。

  12、.基本数据类型参数的传递是值传递,

  引用....................址传递.

  class Length{

  int length;

  }

  class People{

  void walk(Length length){

  length.length=+=2;

  }

  public satic void main(String[] args){

  Length l=new Length();

  l.length=20;

  new People().walk(l);

  System.out.println(l.length);

  }

  }

  13、方法的重载,不能通过返回值类型不同来区别,只能通过参数的不同来区别.

  14、方法或变量加static和

  不加static的方法,是类的对象的方法.对象消失,方法消失

  加static的方法,是类的方法.对象消失,方法存在.方法的地址是静态的与类绑定

  变量和上面也一样.

  15、静态方法,只能访问类的'静态成员和该方法的成员

  16、JAVA不支持多重继承,类似一个树,一个类只有一个父类,但可以有多个接口

  C++支持多重继承,类似一个网,一个类可以有多个父类

  17、子类默认调用了父类无参构造函数.如果父类没有无参构造函数,必须手动重写子类的构造方法,并用super(参数)调用父类中有参构造的方法.

  例:

  class People{

  private int age;

  public People(int age){

  this.age=age;

  }

  }

  class Doctor extends People{

  //不写以下构造方法则出错.

  public Doctor(){

  super(23);

  }

  //或者

  public Doctor(int age){

  super(age);

  }

  }

  解决方法:在写类的有参构造方法时,最好定义个无参的构造方法.

  18、构造方法调用父类的构造方法super()前不能有其他的语句.

  19、final可以修饰形式参数,防止被改

  例:

  void walk(final int length){

  }

  20、import导入包的理解,重要的事情总要放到前面

  21、private与abstract冲突,原因:子类实现(继承)不了接口(抽象类)中被private修饰的成员,但接口(抽象类)中的方法必须要重写,加private就没办法重写了

  例:

  interface InterfaceTest {

  int f();

  private abstract int ff();//此句错误,要去掉private

  }

  22、内部类可以被外部使用,可以访问外部类(宿主类)的privite成员;内部类成员加public也可以被外部访问,但也危险,定义成private外部不能访问此类了(常用).

  public class OutClass {

  private int i=2;

  public class InnerClass{

  public int j=i;

  }

  }

  23、抽象类不用继承也能用

  例:

  abstract class Out2{

  private int i=2;

  public abstract int f();

  public static Out2 getInstance(){

  return new Inner2();

  }

  private static class Inner2 extends Out2{//static

  public int f(){

  return 2;

  }

  }

  public static void main(String[] args) {

  Out2 ou=Out2.getInstance();//抽象类不用继承也能用,获得此类的实例

  int i=ou.f();

  System.out.println(i);

  }

  }

  24、接口里也可以定义类(内隐类)

  例:

  interface InterfaceTest {

  int f();

  class A{

  int f(){

  return 2;

  }

  }

  }

  25、内部类的使用.类NoNameInnerClass 不用implements实现接口,而用传递进来对象来用接口

  interface Inter{

  void paint();

  }

  public class NoNameInnerClass {

  public void paint(Inter p){//传递进来对象来用接口

  p.paint();

  }

  public void func(){

  //为了获得对象,定义一个内部类,把此对象做参数

  class Paint implements Inter{

  public void paint(){

  System.out.println("paint.");

  }

  }

  Paint p=new Paint();

  paint(p);

  }

  }

  26、内部类的使用.不用类名,直接创建对象,无名内隐类,类名是他实现的接口名字

  interface Inter{

  void paint();

  }

  public class NoNameInnerClass {

  public void paint(Inter p){//传递进来对象来用接口

  p.paint();

  }

  public void func(){

  //直接创建对象,无名内隐类,类名是他实现的接口名字,

  paint(new Inter(){

  public void paint(){

  }

  });

  }

  }

  27、单态设计模式。能够创建类的唯一实例。把构造方法定义成private,再创建静态的成员方法getInstance()获得此类唯一实例.

  例1.

  public class ChineseTest{

  public static void main(String[] args) {

  Chinese Obj1 = Chinese.getInstance();

  Chinese Obj2 = Chinese.getInstance();

  System.out.println(Obj1 == Obj2);

  }

  }

  class Chinese {

  private static Chinese objRef = new Chinese();

  private Chinese() {

  }

  public static Chinese getInstance(){

  return objRef;

  }

  }

  例2:

  public class ChineseTest{

  public static void main(String[] args) {

  Chinese Obj1 = Chinese.getInstance();

  Chinese Obj2 = Chinese.getInstance();

  System.out.println(Obj1 == Obj2);

  }

  }

  class Chinese {

  private static Chinese objRef ;

  private Chinese() {

  }

  }

  28、泛型应用

  Vector类后的E代表泛型,用来限制往里面放的数据类型

  例:

  Vector v=new Vector();

  v.add("aaa");

  v.add("bbb");

  System.out.println(v.get(0));

  29、如果一个方法可能有异常,则用throw new Exception("")来抛出异常

  如果方法内用throw抛出异常,则此方法头后必须用throws(多个s)声明可能会抛出异常。

  如果一个方法头后用throws声明可能会抛出异常,则此方法在用的时候必须用try-catch语句

  例:

  public class Lx_Yichang {

  static int div(int x,int y)throws Exception{

  if(y==0){

  throw new Exception("除数不能为0!!!");//方法内用throw抛出异常

  }else{

  return x/y;

  }

  }

  public static void main(String[] args) {

  try{//用try-catch语句

  int z=0;

  z=Lx_Yichang.div(10, 0);

  System.out.println(z);

  }

  catch(Exception ex){

  System.out.println(ex.toString());

  ex.printStackTrace();

  }

  finally{

  System.out.println("End!");

  }

  }

  }

  30、Hashtable类应用,可以通过get(键)或get(hashCode)来获得值内容。

  import java.util.Hashtable;

  class PhoneList{

  String name;

  String phoneNumber;

  public PhoneList(String name,String phoneNumber){

  this.name=name;

  this.phoneNumber=phoneNumber;

  }

  }

  public class HashtableTest {

  public static void main(String[] args) {

  //利用泛型

  Hashtable hashTable=new Hashtable();

  hashTable.put("wang0",new PhoneList("wang","0000000"));

  hashTable.put("wang1",new PhoneList("wang","1111111"));

  hashTable.put("wang2",new PhoneList("wang","2222222"));

  hashTable.put("wang3",new PhoneList("wang","3333333"));

  System.out.println(hashTable.get("wang2").phoneNumber);

  //不利用泛型

  Hashtable hash=new Hashtable();

  hash.put("wang0",new PhoneList("wang","0000000"));

  hash.put("wang1",new PhoneList("wang","1111111"));

  hash.put("wang2",new PhoneList("wang","2222222"));

  hash.put("wang3",new PhoneList("wang","3333333"));

  //System.out.println(((PhoneList)hash.get("wang2")).phoneNumber);//不用泛型,需要强制类型转换

  //强制类型转换时,最好先进行类型判断

  Object o=hash.get("wang2");

  if(o instanceof PhoneList){

  System.out.println(((PhoneList)o).phoneNumber);

  }

  //利用hashcode

  Hashtable table=new Hashtable();

  PhoneList p1=new PhoneList("wang2","888888888888");

  table.put(new Integer(p1.hashCode()), p1);

  System.out.println(table.get(new Integer(p1.hashCode())).phoneNumber);

  }

  }

  31、提供一个关于游戏的简短描述,网页,游戏名称,提供商,金钱初始值等等。这些数据可以置于.jad文件中。 打包后是放在JAR的META-INF文件夹里。 用MIDlet类的getAppProperty(String key)来获得其中的值.

  32、Canvas 的hideNotify() 里写暂停的代码。showNotify() 写继续游戏的代码。

  来电话时自动调用hideNotify() 。 pauseApp也可以实现,但有的手机不支持如Nokia手机

  34、运行提示ALERT: java/lang/ClassFormatError: Bad version information.原因

  原因:当前编译器的版本过高。

  解决方法: 编译器的版本不要过高否则有的手机不认,选择编译器方法:点项目右键属性->Java Compiler-> Enable project specific settings打勾,然后选择版本较低的编译器

  35、把所有的引用都设置为null才可以变为垃圾

  Hero h=new Hero();

  Hero h2=h;

  h=null;//此时,h并没变为垃圾,因为还有h2指向它,需要把h2也设置为null,h才变为垃圾

  h2=null;

  36、去掉无用包(ctrl+shift+0).或右键->Source->Organize Imports

  37、WTK2.5的安装后,ECLIPSE的设置

  Window->Preferences->Device Management->Import->找到WTK的安装路径

  38、添加资源文件夹名

  在项目上右键->Properties->双击Java Build Path->点Add Foler->在下面的选项中选择update exclusion filters in other source folders to solve nesting,再添入资源文件夹名,如src、res等。

  39、添加抽象类、接口中的方法。

  例如对继承Canvas类,需要添加protected void keyPressed(int keyCode){}方法时.

  在代码上右键->Source->选择Override/Implements Methods->在窗口中,对要重写的方法打勾->Ok。

  40、在for语句中,第2个循环条件可以和逻辑表达试取与

  例:

  for(int i=0; i<100 && i%2==0;i++)

  41、DataInputStream包装FileInputStream后,底层操作文件,上层按指定格式输出(最易使用)

  42、FileInputStream的应用

  例:

  import java.io.File;

  import java.io.FileInputStream;

  import java.io.FileOutputStream;

  public class PekingFileInputStreamTest1 {

  public static void main(String[] args) {

  try {

  //在项目根目录里创建文件fileInputStream.txt

  File file=new File("fileInputStream.txt");

  FileOutputStream fout=new FileOutputStream(file);

  fout.write(65);

  fout.close();//输出到文件完毕后关闭

  //开始读

  FileInputStream fin=new FileInputStream("fileInputStream.txt");

  int b=fin.read();

  System.out.println(b);

  fin.close();

  } catch (Exception e) {

  // TODO: handle exception

  e.printStackTrace();

  }

  }

  }

  43、利用DataOutputStream包装FileInputStream按指定格式输出

  例:

  import java.io.DataInputStream;

  import java.io.DataOutputStream;

  import java.io.File;

  import java.io.FileOutputStream;

  import java.io.FileInputStream;

  public class PekingFileInputStreamTest2 {

  public static void main(String[] args) {

  try {

  //在项目根目录里创建文件fileInputStream.txt

  File file=new File("fileInputStream.txt");

  FileOutputStream fout=new FileOutputStream(file);

  //包装下按指定格式输出

  DataOutputStream dout=new DataOutputStream(fout);//子类fout做参数传给父类,子类当父类用

  dout.writeInt(8793);

  dout.writeUTF("感动中国");

  dout.close();

  fout.close();

  //开始读

  FileInputStream fin=new FileInputStream("fileInputStream.txt");

  DataInputStream din=new DataInputStream(fin);

  int b=din.readInt();

  String s=din.readUTF();

  System.out.println(b);

  System.out.println(s);

  din.close();

  fin.close();

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  }

  44、利用PrintWriter包装三次的例子。

  PrintWriter包装OutputStreamWriter,OutputStreamWriter包装FileOutputStream,FileOutputStream包装File

  import java.io.File;

  import java.io.FileOutputStream;

  import java.io.OutputStreamWriter;

  import java.io.PrintWriter;

  public class PekingFileInputStreamTest3 {

  public static void main(String[] args) {

  try {

  //在项目根目录里创建文件fileInputStream.txt

  File file=new File("fileInputStream.txt");

  // FileOutputStream fout=new FileOutputStream(file);

  // OutputStreamWriter osw=new OutputStreamWriter(fout);//测试字符流//字符流通向字节流的桥梁

  // PrintWriter pw=new PrintWriter(osw);//包装三次

  PrintWriter pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream(file)));

  //开始读

  pw.println("窗前名月光,");

  pw.println("疑是地上霜.");

  pw.println("抬头望明月,");

  pw.println("低头思故乡。");

  pw.flush();

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  }

【java学习技巧】相关文章:

1.JAVA学习技巧分享

2.Java与Java web的区别

3.速录学习技巧

4.粤语学习技巧

5.口语学习技巧

6.民族舞学习技巧

7.博思英语学习技巧

8.口语学习技巧解析