效率是一樣的,雖然語句不一樣,但是編譯成class文件后,都是相同的JVM指令。
成都創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)整合營銷推廣、網(wǎng)站重做改版、松江網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5建站、商城系統(tǒng)網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價格優(yōu)惠性價比高,為松江等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
新建一個Java文件Test2.java,然后把它編譯成Test2.class
public class Test2 {
public static boolean stringIsNull(String str) {
if (str == null)
return true;
return false;
}
public static boolean stringIsNull2(String str) {
if (str == null)
return true;
else
return false;
}
public static boolean stringIsNull3(String str) {
return str == null;
}
}
然后你可以通過JDK自帶的javap命令查看JVM指令,三個方法的JVM指令是一樣的
JAVA安裝目錄/bin/javap -c Test2
Compiled from "Test2.java"
public class Test2 extends java.lang.Object{
public Test2();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."init":()V
4: return
public static boolean stringIsNull(java.lang.String);
Code:
0: aload_0
1: ifnonnull 6
4: iconst_1
5: ireturn
6: iconst_0
7: ireturn
public static boolean stringIsNull2(java.lang.String);
Code:
0: aload_0
1: ifnonnull 6
4: iconst_1
5: ireturn
6: iconst_0
7: ireturn
public static boolean stringIsNull3(java.lang.String);
Code:
0: aload_0
1: ifnonnull 8
4: iconst_1
5: goto 9
8: iconst_0
9: ireturn
}
#include?iostream
#include?string
#include?vector
#include?algorithm
#include?time.h
const?std::string?GetGongziHorse(std::vectorstd::string?horse)
{
int?size?=?horse.size();
int?choose?=?rand()%size;
std::string?horseChoosed?=?horse[choose];
std::vectorstd::string::iterator?it?
=?find(horse.begin(),?horse.end(),?horseChoosed);
horse.erase(it);
return?horseChoosed;
}
const?std::string?GetTianjiHorse(const?std::string?horseChoosed)
{
if?(strcmp(horseChoosed.c_str(),?"千里馬")?==?0)
return?"劣馬";
else?if?(strcmp(horseChoosed.c_str(),?"好馬")?==?0)
return?"千里馬";
else
return?"好馬";
}
int?main(void)
{
srand(time(NULL));
std::string?strHorse[]?=?{"千里馬",?"好馬",?"劣馬"};
std::vectorstd::string?horseGongzi;
horseGongzi.insert(horseGongzi.end(),?strHorse,?strHorse?+?3);
int?i?=?1;
while(horseGongzi.size())
{
std::string?horseChoosed?=?GetGongziHorse(horseGongzi);
std::cout??"第"??i??"場"??std::endl;
std::cout??"虛空公子派出了:"??horseChoosed??std::endl;
std::cout??"田忌派出了:"?
GetTianjiHorse(horseChoosed)??std::endl;
i++;
std::cout??std::endl;
}
std::cout??"田忌無恥的贏了"??std::endl;
getchar();
return?0;?
}
根據(jù)流程圖,我寫了一個完整的程序給你參考。
源代碼如下(vc++6.0下編譯通過):
#include stdio.h
int main(void)
{
int x=0,max=0,min=0,i=0,s=0;
printf("please input a number(x): \n");
scanf("%d", x);
max = x;
min = x;