java注意的一些细节问题

1. 大括弧作用域问题

public static void main(String[] args) {{int x;{int x;//编译错误:Duplicate local variable x
        }}
}

2.boolean值的运算

public static void main(String[] args) {if(true && false) {}if(true & false) {}System.out.println(true & false);System.out.println(true ^ false);System.out.println(true | false);
}

false
true
true

 

3.continue label 和 break label

public static void main(String[] args) {label:for(int i=0; i<2; ++i){System.out.println("i=" + i);for(int j=0; j<3; ++j){if(j == 1){//continue;//break;//continue label;break label;}System.out.println("   j=" + j);}}
}

  这个例子中,continue label和break具有同样的作用。

public static void main(String[] args) {label:for(int k=0; k<2; ++k){System.out.println("k=" + k);for(int i=0; i<2; ++i){System.out.println("    i=" + i);for(int j=0; j<3; ++j){if(j == 1){//break;continue label;}System.out.println("        j=" + j);}}}
}

  这个例子就更加直观的看到 continue label实现不一样的效果!

4.基本类型和对应对象分别做参数的函数重载

class A{public void fun(int x){System.out.println("int 重载");}public void fun(Integer x){System.out.println("Integer 重载");}
}public class Main{public static void main(String[] args) {A a = new A();int x = 1;Integer ix = 1;a.fun(x);a.fun(ix);}
}

int 重载
Integer 重载

 5.获取绝对路径

  request.getSession().getServletContext() 获取的是Servlet容器对象,相当于tomcat容器了。getRealPath("/") 获取实际路径,“/”指代项目根目录,所以代码返回的是项目在容器中的实际发布运行的根路径

  ClassLoader类的getResource(String name),getResourceAsStream(String name)等方法,使用相对于当前项目的classpath的相对路径来查找资源。

 

1.jsp页面
  String path = pageContext.getServletContext().getRealPath("/");

  或者 String path = request.getSession().getServletContext().getRealPath("/");  

  String realPath = path+"/WEB-INF/classes/abc.properties";

2.java 程序
  InputStream in = getClass().getClassLoader().getResourceAsStream("abc.properties"); // abc.properties放在webroot/WEB-INF/classes/目录下

    推荐使用Thread.currentThread().getContextClassLoader().getResource("")来得到当前的classpath的绝对路径的URI表示法。
  prop.load(in);
  in.close();

3.只通过Java程序操作资源文件
  InputStream in = new FileInputStream("abc.properties"); // 相对路径,项目下的路径

  OutputStream out = new FileOutputStream("abc.properties");

6.异常链

public class Main{public static void test2(){throw new NullPointerException("空指针异常!");}public static void test() throws Exception{try{test2();} catch (Exception e){//throw new Exception("自定义异常!"); //(1)throw new Exception("自定义异常!", e);//(2)
        }}public static void main(String[] args) {try{test();} catch(Exception e) {StringWriter sw = new StringWriter();PrintWriter pw = new PrintWriter(sw);e.printStackTrace(pw);System.out.println(sw.toString());}}
}

注意:(1)和(2)的输出差别

(1)java.lang.Exception: 自定义异常!at com.hjzgg.Main.test(Main.java:28)at com.hjzgg.Main.main(Main.java:34)(2)java.lang.Exception: 自定义异常!at com.hjzgg.Main.test(Main.java:29)at com.hjzgg.Main.main(Main.java:35)
Caused by: java.lang.NullPointerException: 空指针异常!at com.hjzgg.Main.test2(Main.java:21)at com.hjzgg.Main.test(Main.java:26)... 1 more

 7.web中一些地址信息

1).地址栏输入:http://localhost:8080/HJZGG_BLOG/pictureAction!pictureGroupJspGetAllGroups
  System.out.println(ServletActionContext.getServletContext().getRealPath("/savePath"));
  System.out.println(ServletActionContext.getRequest().getServletPath());
  System.out.println(ServletActionContext.getRequest().getRequestURL());
  System.out.println(ServletActionContext.getRequest().getRequestURI());

打印的结果如下:
  F:\eclipseEE_workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\HJZGG_BLOG\savePath
  /pictureAction!pictureGroupJspGetAllGroups
  http://localhost:8080/HJZGG_BLOG/pictureAction!pictureGroupJspGetAllGroups
  /HJZGG_BLOG/pictureAction!pictureGroupJspGetAllGroups

2).得到完整的URL请求
  访问:http://localhost:8080/HJZGG_BLOG/pictureAction!pictureGroupJspGetAllGroups
  HttpServletRequest request;
  String url = null;
  url = request.getScheme()+"://" //请求协议 http 或 https
    + request.getServerName() //服务器地址
    + ":"
    + request.getServerPort() //端口号
    + request.getContextPath() //项目名称
    + request.getServletPath() //请求页面或其他地址 ,例如:pictureAction!pictureGroupJspGetAllGroups
    + "?" + (request.getQueryString()); //参数
  System. out.println(url);
  输出:http://localhost:8080/HJZGG_BLOG/pictureAction!pictureGroupJspGetAllGroups?null

3).得到项目根目录的URL
  HttpServletRequest request = ServletActionContext.getRequest();
  String url = null;
  url = request.getScheme()+"://" //请求协议 http 或 https
    + request.getServerName() //服务器地址(可以替换成 InetAddress.getLocalHost().getHostAddress())
    + ":"
    + request.getServerPort() //端口号
    + request.getContextPath(); //项目名称
  System. out.println(url);

  输出:http://localhost:8080/HJZGG_BLOG

8.内部类和内嵌口

//如果外部类返回是  private类型的内部类,非此外部类的其他方法不能访问这个内部类
class A{private class B{public void fun(){}}public B getBInstance(){return new B();}public void invokeBMethod(B b){b.fun();}private int x;public int getX(){return x;}
}//内嵌接口, 如果你不想将你的借口暴露给外部,写成如下方式,否则将内嵌接口定义成 public,或者将接口写成非内嵌接口的形式
class C{interface E{void gun();}private interface D {void fun();}private class DImpl implements D{@Overridepublic void fun() {}}public D getDImpl(){return new DImpl();}public void invokeDMethod(D d){d.fun();}
}//内部链接提供给外部
interface H{void fun();
}
class I{private class G implements H{@Overridepublic void fun() {}}public H getHImpl(){return new G();}
}class F implements C.E{@Overridepublic void gun() {}
}public class Main{public static void main(String[] args) {A a = new A();int x = a.getX();//error, The type A.B is not visible//a.getBInstance().fun();
         a.invokeBMethod(a.getBInstance());C c = new C();//error, The type A.B is not visible
//         c.getDImpl().fun();
         c.invokeDMethod(c.getDImpl());H h = new I().getHImpl();h.fun();}
}

 9.协变类型

java中泛型是不变的,可有时需要实现逆变与协变,怎么办呢?这时,通配符?派上了用场:

  • <? extends>实现了泛型的协变,比如:
List<? extends Number> list = new ArrayList<Integer>();
  • <? super>实现了泛型的逆变,比如:
List<? super Number> list = new ArrayList<Object>();

class Parent{public Parent getSelf(){return new Parent();}
}class Child extends Parent{@Overridepublic Child getSelf(){return new Child();}
}

  在Java1.4及以前,子类方法如果要覆盖超类的某个方法,必须具有完全相同的方法签名,包括返回值也必须完全一样。
  Java5.0放宽了这一限制,只要子类方法与超类方法具有相同的方法签名,或者子类方法的返回值是超类方法的子类型,就可以覆盖。
  注意:"协变返回(covariant return)",仅在subclass(子类)的返回类型是superclass(父类)返回类型的extension(继承)时才被容许。

10.接口内部的类

interface C{class D{public void fun(){System.out.println("this is D class!");}}D getDInstance();
}class E implements C{@Overridepublic D getDInstance() {return new D();}
}

  正常情况下,不能再接口内部放置任何代码,但是嵌套类可以作为接口的一部分。你放到接口中的任何类都自动是public和static的。因为类是static的,只是将嵌套类至于接口的命名空间内,这并不违反接口的规则。你设置可以在内部类中实现外层接口。另外实现该接口的类,都可以使用该接口中的内部嵌套类,如上所示。

11.内部类的一个好处

  要求,不使用内部类的情况下,实现下面的两个接口。

接口

interface A{void fun();
}interface B{int fun();
}

不使用内部类,实现两个接口

//The return type is incompatible with B.fun()
class C implements A, B{@Overridepublic void fun() {}
}
class C implements A{@Overridepublic void fun() {}
}//The return types are incompatible for the inherited methods B.fun(), C.fun()
class D extends C implements B{}

使用内部类,方式1

class C implements A{@Overridepublic void fun() {}class D implements B{@Overridepublic int fun() {return 0;}}
}

使用内部类,方式2

class C{class D implements A{@Overridepublic void fun() {}}class E implements B{@Overridepublic int fun() {return 0;}}
}

 12.java不能使用范型数组原因

  转自:http://www.cnblogs.com/exmyth/p/4598971.html

Java 不支持泛型数组。也就是说,

  1. List<String>[] ls = new ArrayList<String>[10];  

是不支持的,而

  1. List<String>[] ls = new ArrayList[10]  或者 List[] ls = new ArrayList[10] 却可以。

是我一直不清楚为什么不能够声明泛型的数组,指定类型可以让编译的时候不会出现类型安全的提示。

直到今天我看到Sun的一篇文档才清楚,里面提到了一种情况:

  1. List<String>[] lsa = new List<String>[10]; // Not really allowed.  
  2. Object o = lsa;  
  3. Object[] oa = (Object[]) o;  
  4. List<Integer> li = new ArrayList<Integer>();  
  5. li.add(new Integer(3));  
  6. oa[1] = li; // Unsound, but passes run time store check  
  7. String s = lsa[1].get(0); // Run-time error: ClassCastException.  

这种情况下,由于JVM泛型的擦除机制,在运行时JVM是不知道泛型信息的,所以可以给oa[1]赋上一个ArrayList<Integer>而不会出现ArrayStoreException,但是在取出数据的时候却要做一次类型转换,所以就会出现ClassCastException,如果可以进行泛型数组的声明,上面说的这种情况在编译期将不会出现任何的警告和错误,只有在运行时才会出错。而对泛型数组的声明进行限制,对于这样的情况,可以在编译期提示代码有类型安全问题,比没有任何提示要强很多。

基于以上的原因,Java不支持声明泛型数组,更确切地表达是:数组的类型不可以是类型变量,除非是采用通配符的方式,看下面这个例子:

  1. List<?>[] lsa = new List<?>[10]; // OK, array of unbounded wildcard type.  
  2. Object o = lsa;  
  3. Object[] oa = (Object[]) o;  
  4. List<Integer> li = new ArrayList<Integer>();  
  5. li.add(new Integer(3));  
  6. oa[1] = li; // Correct.  
  7. String s = (String) lsa[1].get(0); // Run time error, but cast is explicit.  


因为对于通配符的方式,最后取出数据是要做显式的类型转换的,所以并不会存在上一个例子的问题。

 13.try..catch..finally中的return

public static int fun(){int x = 1;try{return x;} catch(Exception e){} finally {x = 2;}return x;
}public static int fun(){int x = 1;try{return x;} catch(Exception e){} finally {x = 2;return x;}
}public static int fun(){int x = 1;try{if(x == 1)throw new Exception("test");} catch(Exception e){return x;} finally {x = 2;return x;}
}

  输出 1  2  1

14.内部类继承

class Outer{private String s;public Outer(String s){System.out.println("Outer initial :" + s);}public void outFun(){System.out.println("outFun");}class Inner{public void innerFun(){System.out.print("内部类调用外部类中的方法: ");outFun();}}
}class InheritInner extends Outer.Inner{public InheritInner(Outer out){out.super();}@Overridepublic void innerFun(){super.innerFun();System.out.println("InheritInner override innerFun");}
}public class Main{public static void main(String[] args) {Outer out = new Outer("Outer");InheritInner inheritInner = new InheritInner(out);inheritInner.innerFun();}
}
这是在《JAVA编程思想》上看到的一道例题,是关于继承inner   classes(内隐类)的。 先把代码和书上的一些原话写出来,如下: 由于inner   class的构造函数必须连接到一个reference指向outer   class   对象身上,所以 
当你继承inner   class时,事情便稍微复杂些。问题出在“指向outer   class对象”的那个 
神秘reference必须被初始化。但derived   class之内不存在可连接的缺省对象,这个问题 
的答案是,使用专用语法,明确产生该关联性: class   WithInner   { class   Inner{} 
} public   class   InheritInner   extends   WithInner.Inner   { InheritInner(WithInner   wi)   { wi.super();                   //---这里不懂,wi.super()指的是什么,有什么用? 
        } public   static   void   main(String[]   args)   { WithInner   wi   =   new   WithInner(); InheritInner   ii   =   new   InheritInner(wi); } 
} 

 15.java自定义注解类,如何获取注解,如何反射内部类,this$0是什么意思

   http://www.cnblogs.com/hujunzheng/p/5719611.html

转载于:https://www.cnblogs.com/hujunzheng/p/5638553.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/531303.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python表白

马上就要520了&#xff0c;不少小伙伴们一定开始想尽各种办法进行表白了…那么身为奔跑在程序员道路上的我们也一定要有独特的表白方法&#xff01; 下面是一段表白代码&#xff0c;请直接搬去用吧&#xff01;&#xff01;&#xff01; import turtle import timedef hart_ar…

git revert和reset区别

1.在github上建立测试项目并克隆到本地 2.本地中新建两个文本文件 3.将a.txt commit并push到远程仓库 执行 git add a.txt, git commit -m "a.txt", git push 4.将b.txt提交到本地仓库&#xff0c;不执行push 通过gitk命令查看提交历史如下&#xff1a; 情景&#xf…

python基础知识点整理(1)

目录一、变量与数据类型1、变量① 变量的命名规则2、字符串常用的库函数二、列表1、列表的基础定义2、列表常用的库函数3、for循环与列表4、列表切片5、元组三、if语句四、字典1、字典定义2、对字典的操作一、变量与数据类型 1、变量 ① 变量的命名规则 只能包含数字、字母以…

sorl6.0+jetty+mysql搭建solr服务

1.下载solr 官网&#xff1a;http://lucene.apache.org/solr/ 2.目录结构如下 3.启动solr&#xff08;默认使用jetty部署&#xff09; 在path路径下将 bin文件夹对应的目录加入&#xff0c;然后输入 solr start&#xff08;或者 solr start -p port&#xff0c;指定端口启动&am…

Maven中安装本地Jar包到仓库中或将本地jar包上传

摘要 maven install 本地jar命令格式 mvn install:install-file -DgroupId<group_name> -DartifactId<artifact_name> -Dversion<version_no> -Dfile<path_of_the_local_jar> -Dpackagingjar -DgeneratePomtrue 示例 mvn install:install-file -Dgroup…

二维码登录原理及生成与解析

一、前言 这几天在研究二维码的扫码登录。初来乍到&#xff0c;还有好多东西不懂。在网上看到有人写了一些通过QRCode或者Zxing实现二维码的生成和解码。一时兴起&#xff0c;决定自己亲手试一试。本人是通过QRCode实现的&#xff0c;下面具体的说一下。 二、二维码原理 基础知…

knockout+echarts实现图表展示

一、需要学习的知识 knockout, require, director, echarts, jquery。简单的入一下门&#xff0c;网上的资料很多&#xff0c;最直接就是进官网校习。 二、效果展示 三、require的配置 require.config.js中可以配置我们的自定义模块的加载。 require.config({baseUrl: ".&…

Maven在Eclipse中的实用小技巧

前言 我们在开发的工程中很多都是Maven项目&#xff0c;这样更加便于我们jar包的管理。而我们一般使用的IDE都是Eclipse&#xff0c;由于我们在日常的开发过程中会经常要用到一些Maven的操作&#xff0c;所以我今天主要跟大家分享一下我们在日常开发中的Maven实用小知识&#x…

React中使用Ant Table组件

一、Ant Design of React http://ant.design/docs/react/introduce 二、建立webpack工程 webpackreact demo下载 项目的启动&#xff0c;参考 三、简单配置 1.工程下载下来之后&#xff0c;在src目录下新建目录“table”&#xff0c;新建app.js&#xff0c;内容如下。 import R…

解决“Dynamic Web Module 3.0 requires Java 1.6 or newer.”错误

一、问题描述 1.错误截图如下。 2.设计的问题 在Eclipse中新建了一个Maven工程, 然后更改JDK版本为1.6, 结果每次使用Maven > Update project的时候JDK版本都恢复成1.5。 二、原因分析 Maven官方文档有如下描述&#xff1a; 编译器插件用来编译项目的源文件.从3.0版本开始, …

java自定义注解类

一、前言 今天阅读帆哥代码的时候&#xff0c;看到了之前没有见过的新东西, 比如java自定义注解类&#xff0c;如何获取注解&#xff0c;如何反射内部类&#xff0c;this$0是什么意思? 于是乎&#xff0c;学习并整理了一下。 二、代码示例 import java.lang.annotation.Elemen…

解决cookie跨域访问

一、前言 随着项目模块越来越多&#xff0c;很多模块现在都是独立部署。模块之间的交流有时可能会通过cookie来完成。比如说门户和应用&#xff0c;分别部署在不同的机器或者web容器中&#xff0c;假如用户登陆之后会在浏览器客户端写入cookie&#xff08;记录着用户上下文信息…

React使用antd Table生成层级多选组件

一、需求 用户对不同的应用需要有不同的权限&#xff0c;用户一般和角色关联在一起&#xff0c;新建角色的时候会选择该角色对应的应用&#xff0c;然后对应用分配权限。于是写了一种实现的方式。首先应用是一个二级树&#xff0c;一级表示的是应用分组&#xff0c;二级表示的是…

junit4进行单元测试

一、前言 提供服务的时候&#xff0c;为了保证服务的正确性&#xff0c;有时候需要编写测试类验证其正确性和可用性。以前的做法都是自己简单写一个控制层&#xff0c;然后在控制层里调用服务并测试&#xff0c;这样做虽然能够达到测试的目的&#xff0c;但是太不专业了。还是老…

快速搭建springmvc+spring data jpa工程

一、前言 这里简单讲述一下如何快速使用springmvc和spring data jpa搭建后台开发工程&#xff0c;并提供了一个简单的demo作为参考。 二、创建maven工程 http://www.cnblogs.com/hujunzheng/p/5450255.html 三、配置文件说明 1.application.properties jdbc.drivercom.mysql.jd…

dubbo服务提供与消费

一、前言 项目中用到了Dubbo&#xff0c;临时抱大腿&#xff0c;学习了dubbo的简单实用方法。现在就来总结一下dubbo如何提供服务&#xff0c;如何消费服务&#xff0c;并做了一个简单的demo作为参考。 二、Dubbo是什么 Dubbo是一个分布式服务框架&#xff0c;致力于提供高性能…

git亲测命令

一、Git新建本地分支与远程分支关联问题 git checkout -b branch_name origin/branch_name 或者 git branch --set-upstream branch_name origin/branch_name 或者 git branch branch_name git branch --set-upstream-toorigin/branch_name branch_name 二、查看本地分支所关…

mysql 7下载安装及问题解决

mysql 7安装及问题解决 一、mysql下载 下载地址&#xff1a;https://www.mysql.com/downloads/Community (GPL) DownloadsMySQL Community Server (GPL)Windows (x86, 64-bit), ZIP ArchiveNo thanks, just start my download.二、mysql安装 解压到指定目录在mysql bin目录下打…

RestTemplate发送请求并携带header信息

1、使用restTemplate的postForObject方法 注&#xff1a;目前没有发现发送携带header信息的getForObject方法。 HttpHeaders headers new HttpHeaders(); Enumeration<String> headerNames request.getHeaderNames(); while (headerNames.hasMoreElements()) {String k…

工作中常用到的命令

linux zip 和 unzip http://blog.csdn.net/shenyunsese/article/details/17556089 linux 查看日志 http://blog.chinaunix.net/uid-15463753-id-2943532.html linux 删除 http://www.jb51.net/LINUXjishu/179430.html linux查看末尾日志&#xff08;tail -f&#xff09; http:/…