让我们看看以下代码段中的简单Java代码:
public class Main {
private int temp() {
return true ? null : 0;
// No compiler error - the compiler allows a return value of null
// in a method signature that returns an int.
}
private int same() {
if (true) {
return null;
// The same is not possible with if,
// and causes a compile-time error - incompatible types.
} else {
return 0;
}
}
public static void main(String[] args) {
Main m = …Run Code Online (Sandbox Code Playgroud) while在Java中查看以下无限循环.它会导致它下面的语句出现编译时错误.
while(true) {
System.out.println("inside while");
}
System.out.println("while terminated"); //Unreachable statement - compiler-error.
Run Code Online (Sandbox Code Playgroud)
以下相同的无限while循环,但工作正常,并不会发出任何错误,我只是用布尔变量替换条件.
boolean b=true;
while(b) {
System.out.println("inside while");
}
System.out.println("while terminated"); //No error here.
Run Code Online (Sandbox Code Playgroud)
在第二种情况下,循环后的语句显然无法访问,因为布尔变量b为true,编译器根本不会抱怨.为什么?
编辑:下面的版本while陷入了无限循环,但是对于它下面的语句没有发出编译器错误,即使if循环中的条件总是false因此,循环也永远不会返回并且可以由编译器在编译时本身.
while(true) {
if(false) {
break;
}
System.out.println("inside while");
}
System.out.println("while terminated"); //No error here.
Run Code Online (Sandbox Code Playgroud)
while(true) {
if(false) { //if true then also
return; //Replacing return with break fixes the following error.
}
System.out.println("inside while");
}
System.out.println("while terminated"); //Compiler-error - unreachable statement. …Run Code Online (Sandbox Code Playgroud) 我一直在打破这个问题.不确定我错过了什么.我无法@Value在纯java配置的spring应用程序(非web)中使用注释
@Configuration
@PropertySource("classpath:app.properties")
public class Config {
@Value("${my.prop}")
String name;
@Autowired
Environment env;
@Bean(name = "myBean", initMethod = "print")
public MyBean getMyBean(){
MyBean myBean = new MyBean();
myBean.setName(name);
System.out.println(env.getProperty("my.prop"));
return myBean;
}
}
Run Code Online (Sandbox Code Playgroud)
属性文件只包含my.prop=avaluebean如下:
public class MyBean {
String name;
public void print() {
System.out.println("Name: " + name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
环境变量正确打印值,而@Value不是.
avalue
Name: ${my.prop}
主类只是初始化上下文.
AnnotationConfigApplicationContext ctx = …Run Code Online (Sandbox Code Playgroud) Java中的以下代码使用了一个final数组,String并且毫无疑问.
final public class Main {
public static final String[] CONSTANT_ARRAY = {"I", "can", "never", "change"};
public static void main(String[] args) {
for (int x = 0; x < CONSTANT_ARRAY.length; x++) {
System.out.print(CONSTANT_ARRAY[x] + " ");
}
}
}
Run Code Online (Sandbox Code Playgroud)
它在控制台上显示以下输出.
I can never change
Run Code Online (Sandbox Code Playgroud)
以下代码也毫无疑问.
final public class Main {
public static final String[] CONSTANT_ARRAY = {"I", "can", "never", "change"};
public static void main(String[] args) {
//CONSTANT_ARRAY={"I", "can", "never", "change"}; //Error - can not assign to …Run Code Online (Sandbox Code Playgroud) Java中两个double值的简单比较会产生一些问题.让我们考虑Java中的以下简单代码片段.
package doublecomparision;
final public class DoubleComparision
{
public static void main(String[] args)
{
double a = 1.000001;
double b = 0.000001;
System.out.println("\n"+((a-b)==1.0));
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码似乎返回true,表达式的评估((a-b)==1.0)却没有.它会返回,false因为对此表达式的求值0.9999999999999999实际上是预期的1.0,1.0因此不等于,因此条件求值为boolean false.克服这种情况的最佳方法是什么?
让我们在Java中看到以下代码片段.
public class Main {
public static void main(String[] args) {
// new Character(' \u000d System.out.println("Hello");
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,虽然方法中的唯一行main()被注释掉,但它Hello在控制台上显示输出,即使看起来这个注释行包含一些语法错误.如果此行未注释,则根本不起作用,从而导致编译时错误.
为什么在这里输出"你好"?
以下JSF代码包含两个单独的<c:if></c:if>.我们来看看吧.
<?xml version='1.0' encoding='UTF-8' ?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<title>JSF EL</title>
</h:head>
<h:body>
<h:form>
<c:set scope="request" var="row" property="x" value="10"/>
<c:if test="#{row==10}">
<h:outputLabel value="value = 10"/>
</c:if>
<c:if test="#{row==15}">
<h:outputLabel value="value = 15"/>
</c:if>
</h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
它只是在运行时在JSF页面上显示value = 10.我需要<c:if></c:if>使用以下if-elseif-else(Java上下文)来表示上述内容.
if(row.equals(10))
{
//Do something...(JSF stuff)
}
else if(row.equals(15))
{
//Do something...(JSF stuff)
}
else
{
//Do something...(JSF stuff)
}
Run Code Online (Sandbox Code Playgroud)
如何使用JSF用表达式语言(EL)表示?
我有个问题.我想画一个像这样的随机字符串aXcFg3s2.我做得不好?怎么改变我的random()
private String random;
private String charsEntered;
private EditText et;
private Button ok;
CaptchaInterface.OnCorrectListener mCorrectListener;
public void setOnCorrectListener(CaptchaInterface.OnCorrectListener listener) {
mCorrectListener = listener;
}
public TextCaptcha(Context context) {
super(context);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
}
public static String random() {
Random generator = new Random();
String x = (String) (generator.nextInt(96) + 32);
return x;
}
public void onCreate(Bundle icicle) {
setContentView(R.layout.main);
random = random();
TextView display = (TextView) findViewById(R.id.textView1);
display.setText("Random Number: " + random); // Show the random number
et = (EditText) findViewById(R.id.etNumbers); …Run Code Online (Sandbox Code Playgroud) 请考虑以下Javascript代码.
function correct()
{
return 15;
}
function wrong()
{
return
15;
}
console.log("correct() called : "+correct());
console.log("wrong() called : "+wrong());Run Code Online (Sandbox Code Playgroud)
correct()上面代码片段中的方法返回正确的值,在这种情况下为15.15然而,该方法返回wrong().大多数其他语言并非如此.
但是,以下函数是正确的,并返回正确的值.
function wrong()
{
return(
15);
}
Run Code Online (Sandbox Code Playgroud)
如果语法错误,它应该发出一些编译器错误,但它不会.为什么会这样?
在下面的方法中,编译器会抱怨缺少return语句,即使该方法只有一条路径,并且它包含一个return语句.抑制错误需要另一个return声明.
public int foo() {
if (true) {
return 5;
}
}
Run Code Online (Sandbox Code Playgroud)
鉴于Java编译器可以识别无限循环,为什么它也不能处理这种情况呢?链接的问题提示,但不提供此特定情况的详细信息.