---
title: "不运行，你能猜到这些个小程序的输出结果么？"
author: "renyanwei"
date: "2008-11-03T22:43:24Z"
tags: []
categories: ["非技术"]
slug: "untitled"
draft: false
lastmod: "2008-11-03T22:43:24Z"
id: 261869
url: "https://renyanwei.iteye.com/blog/261869"
---

看第一题：

```
class Test 
{
	public static void main(String[] args) 
	{
		Boolean b=false;
		if(b=true){
			System.out.println("true");
		}else{
			System.out.println("false");
		}
	}
}
```

 结果是输出True还是false 或者是编译错误？

答案是true，呵呵 因为b是Boolean类型的有点儿特殊，if里面等于是直接把b赋值成true了，然后表达式当然是true啦

 

第二题：

```
class Test {
	public static void main(String[] args){
		Test t = new Test();
		t.test(1.0, 2L, 3);
	}

	void test(double a, double b, short c){
		System.out.println("1");
	}
	 void test(float a, byte b, byte c) {
		System.out.println("2");
	}
	void test(double a, double b, double c){
		System.out.println("3");
	}
	void test(int a, long b, int c) {
		System.out.println("4");
	}
	void test(long a, long b, long c) {
		System.out.println("5");
	} 
} 
```

 这个可能有结果么？ 当然能。答案就是3.其实这里涉及到JAVA的数值类型转换问题，概念本人也不是很精通，不过死记规则就行了(这里只说转换问题)

**当一个方法的参数的值是一个int类型时，转换优先规则：float—double**

**当一个方法的参数的值是一个带小数点的数时(后面没f)，直接转换成double**

**(其他情况用的极少，没必要死究到底，做学问慢慢来)**

所以只有三个参数为double类型的方法行得通

 

第三题：

```
class Test {
	
	static int numA=0;
	static int numB=0;
	public static void main(String[] args){
		
		if(true|funA()){
		}

		if(false&funB()){
		}
		System.out.println("A:"+numA+",B:"+numB);
	}

	static Boolean funA()
	{
		numA++;
		return false;
	}
	static Boolean funB()
	{
		numB++;
		return false;
	}
} 
```

 这个题的考点儿在&和|的用法上，其实说白了就是让你知道&&和&以及||和|的区别，死记两点儿：

**&和&&的区别是：&是即使左边是false老子也要去执行下右边的，虽然没什么意思，其余两者功能一样**

**|和||的区别是：|是即使左边是true还是不死心的去右边看一下，当然也是徒劳 O(∩\_∩)O哈哈~ 其余两者功能一样**