java注意的一些细节问题
- 作者: 五速梦信息网
- 时间: 2026年04月04日 13:35
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);
}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);
}
}
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{
@Override
public 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{
@Override
public void fun() {}
} public H getHImpl(){
return new G();
}
} class F implements C.E{
@Override
public 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.协变类型
?
List<? extends Number> list = new ArrayList<Integer>();List<? super Number> list = new ArrayList<Object>();class Parent{
public Parent getSelf(){
return new Parent();
}
} class Child extends Parent{ @Override
public Child getSelf(){
return new Child();
}
}interface C{
class D{
public void fun(){
System.out.println("this is D class!");
}
}
D getDInstance();
} class E implements C{
@Override
public D getDInstance() {
return new D();
}
}interface A{
void fun();
} interface B{
int fun();
}//The return type is incompatible with B.fun()
class C implements A, B{
@Override
public void fun() {
}
}class C implements A{
@Override
public void fun() {
}
} //The return types are incompatible for the inherited methods B.fun(), C.fun()
class D extends C implements B{ }class C implements A{
@Override
public void fun() {}
class D implements B{
@Override
public int fun() {
return 0;
}
}
}class C{
class D implements A{
@Override
public void fun() {
}
} class E implements B{
@Override
public int fun() {
return 0;
}
}
}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;
}
}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();
}
@Override
public 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);
}
}
- 上一篇: Java—转换流、字符缓冲流
- 下一篇: java中子类调用父类方法
相关文章
-
Java—转换流、字符缓冲流
Java—转换流、字符缓冲流
- 互联网
- 2026年04月04日
-
java自带线程池和队列详细讲解,android中适用
java自带线程池和队列详细讲解,android中适用
- 互联网
- 2026年04月04日
-
Java字符串操作及与C#字符串操作的不同
Java字符串操作及与C#字符串操作的不同
- 互联网
- 2026年04月04日
-
java中子类调用父类方法
java中子类调用父类方法
- 互联网
- 2026年04月04日
-
Java中怎样把数组转换为ArrayList?
Java中怎样把数组转换为ArrayList?
- 互联网
- 2026年04月04日
-
Java中数组、集合、链表、队列的数据结构和优缺点和他们之间的区别
Java中数组、集合、链表、队列的数据结构和优缺点和他们之间的区别
- 互联网
- 2026年04月04日






