Tag: 返回

void函数中return语句的用法是什么

我是java新手, return;什么return; 意思? 是break吗? public void run() { if(imageViewReused(photoToLoad)) return; Bitmap bmp=getBitmap(photoToLoad.url); memoryCache.put(photoToLoad.url, bmp); if(imageViewReused(photoToLoad)) return; BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad); Activity a=(Activity)photoToLoad.imageView.getContext(); a.runOnUiThread(bd); } 如果第二个imageViewReused(photoToLoad)返回true,则BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad)将不会被执行,对吧?

该方法必须返回int类型

public int computeStyle(String season) { if(season.equals(“summer”)){ if (this.style.equals(“toque”)){ return 8; } if (this.style.equals(“sun visor”)){ return 1; } if (this.style.equals(“fedora”)){ return 6; } } else if(season.equals(“winter”)){ if (this.style.equals(“toque”)){ return 1; } if (this.style.equals(“sun visor”)){ return 8; } if (this.style.equals(“fedora”)){ return 7; } } else return 5; } 为什么我一直得到方法必须返回类型int的错误。 这个function有什么问题? 它应该在每个可能的场景中返回一个int吗?

Java返回值(在try / catch子句中)

大家。 我有一个关于java中返回值的新手问题。 这是我的代码。 @Override public long addDrugTreatment(long id, String diagnosis, String drug, float dosage) throws PatientNotFoundExn { try { Patient patient = patientDAO.getPatientByDbId(id); long tid = patient.addDrugTreatment(diagnosis, drug, dosage); Connection treatmentConn = treatmentConnFactory.createConnection(); Session session = treatmentConn.createSession(true, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(treatmentTopic); TreatmentDto treatment = null; ObjectMessage message = session.createObjectMessage(); message.setObject(treatment); producer.send(message); return tid; } […]

从finally块返回时Java的奇怪行为

试试这段代码。 为什么getValueB()返回1而不是2? 毕竟,increment()函数被调用两次。 public class ReturningFromFinally { public static int getValueA() // This returns 2 as expected { try { return 1; } finally { return 2; } } public static int getValueB() // I expect this to return 2, but it returns 1 { try { return increment(); } finally { increment(); } } […]

理解try catch最后返回它返回的值和值

我有以下代码。 public static void main(String[] args) { System.out.println(returnString()); } private static String returnString(){ try { System.out.println(“Executing try”); return “Return try value”; } catch (Exception e){ System.out.println(“Executing Catch”); return “Return catch value”; } finally { System.out.println(“Executing finally”); return “Return finally value”; } } 这个输出是 Executing try Executing finally Return finally value 如果我改变我的finally块而不返回任何类似的东西 public static void main(String[] […]

为什么我的等式比较使用=(单个等于)在Java中无法正常工作?

我在以下行中有语法错误。 但是我无法理解这个错误的原因是什么。 if (address1.compareTo(address2) = 1) System.out.println(address1 + ” is greater than ” + address2); 当且仅当compareTo返回1我想要实现的是打印正确的消息。

尝试,最后在try块中返回执行流程

尝试时,最后使用组合,如果有返回语句,请尝试。为什么最后是块执行? class Palindrome { public static void main(String args[]) { System.out.println(Palindrome.test()); } public static int test() { try { //return 0; return 100; } finally { System.out.println(“finally trumps return.”); } } } 在上面的代码中,请告诉我执行的流程。 我知道最终将在try块之后强制执行。但是在try块中,返回staatement会将控件带到主类。 在那种情况下,控制将如何最终阻止?

在Java中使用if与三元opertator时,“错误”返回类型

在下面的类中,两种方法的返回类型与三元运算符的思想不一致: return condition?a:b; 相当于 if(condition) { return a; } else{ return b; } 第一个返回一个Double,第二个返回Long: public class IfTest { public static Long longValue = 1l; public static Double doubleValue = null; public static void main(String[] args) { System.out.println(getWithIf().getClass());// outpus Long System.out.println(getWithQuestionMark().getClass());// outputs Double } public static Object getWithQuestionMark() { return doubleValue == null ? longValue : […]

需要帮助在线程运行方法中返回对象

我有一个扩展Thread的Java类,它基本上如下所示: public class HttpRequestDispatcher extends Thread { private String url; private String method; // GET or POST private byte[] postData; public HttpRequestDispatcher(String url, String method, byte[] postData) { this.url = url; this.method = method; this.postData = postData; } public HttpRequestDispatcher(String url, String method) { this.url = url; this.method = method; } public void run() { […]

如何解释构造函数中的return语句?

据我所知,构造函数什么也没有返回,甚至没有返回, 并且 return ; 在任何方法内部意味着返回void。 所以在我的程序中 public class returnTest { public static void main(String[] args) { returnTest obj = new returnTest(); System.out.println(“here1”); } public returnTest () { System.out.println(“here2”); return ; } } 我在打电话 return; 这将返回VOID,但构造函数不应该返回任何东西,程序编译得很好。 请解释 。