quick question: what the difference between these lines? which one will go smoothly without error in Java??
- if ( null || false || true ) { .. /* some statements */ .. }
- if ( false || true || null ) { .. /* some statements */ .. }
- if ( false || null || true ) { .. /* some statements */ .. }
yupp.. you're right!! it's the second one with true condition before null. feel so funny about it. afters 2 years of knowing Java, i just found out that (looks like.. :p) IF conditional skip the rest of the conditional test right after it found true condition. So, in the second lines above, Java wouldn't recognize any null pointer exception, unlike the first or the third. In the first example, the compiler will stuck in the null value and throw NullPointerException. In the third example, the compiler will meet the false condition and move forward, and then stuck in the second value just like at the first occurence.
well, i guess if we weren't sure whether we gonna get null pointer exception or not (in the conditional test), just put it last like the second example.
what do YOU think..??
10 comments:
menurut gw ada 2 pendekatan logis yang ngejelasin pernyataan loe bro:
1. boolean itu cuman kenal nilai biner, true or false, 0 atau 'selain 0'. jadi, terang aja null ga dikenali
2. "IF conditional skip the rest of the conditional test right after it found true condition". pada pernyataan logika dengan or, yang paling kuat adalah true, sekali ada true, pasti hasilnya selalu true.
@1: 'tul, null adalah Exception, but that's not the main point here..
@2: "sekali ada true, pasti hasilnya true..??". are you sure?? kalo dia ketemu null duluan baru true, gw dapetnya NullPointerException.. bukan true lho!! coba sendiri bro.. makanya example ke tiga compile tapi runtime error: NullPointerException..
"@2: "sekali ada true, pasti hasilnya true..??". are you sure??"
very sure, maksud gw dalam ekspresi OR. baca aja literatur tradisional yang ngejelasin logika OR, kurang lebih kebanyakan mencantumkan hal ini:
true OR false == true
false OR true == true
false OR false == false
"kalo dia ketemu null duluan baru true, gw dapetnya NullPointerException.. bukan true lho!! coba sendiri bro.. makanya example ke tiga compile tapi runtime error: NullPointerException.."
maksud gw dengan 'sekali ada true' adalah gini: seperti pernyataan gw yang pertama bahwa compiler cuman kenal kondisi biner dalam ekspresi logika, gw blon pernah lihat dalam literatur manapun bahwa dalam ekspresi logika kenal kondisi selain true dan false. dan maksud gw dengan pernyataan 'sekali ada true' itu tentunya berlaku dalam konteks true dan false doang. dan tidak termasuk null.
dan semisalnya ada yang bernilai null dalam kondisi itu?
mungkin lebih ke java compiler nya kali ya.. klo dia udah ketemu nilai true duluan (untuk ekspresi OR ||), nilai selanjutnya kayanya ga dipeduliin lagi. buktinya ada yang null tapi ga keluar NullPointerException tuh..
lain soal kalo dia ketemu null duluan, meskipun setelah itu ada true.
contoh ney:
public class TryIF
{ public static void main(String[] args)
{ boolean a;
if (a || true)
{ System.out.println("Hello World!"); }
}
}
--> ga jalan kan?? karena a emang belum di-initialize --> null.
tapi klo urutannya dibalik:
if (true || a)
maka akan compile, dan run..
oh gitu, i see, i see, hohoho...
intinya sih masalah kompiler kali ya?
*rada puyeng nih*
kata pak dana dari milis RPL:
Entar2, coba baca2 referensi Java bab : short circuit operator, walaupun java punya operator OR namun ada dua sifat yang berbeda, yakni : pake | dan || itu beda ( ttermasuk & dan && ).
Unlike their logical counterparts & and |, which can also be applied to integral operands for bitwise operations, the conditional operators && and || can only be applied to boolean operands
In summary, we employ the conditional operators && and || if the evaluation of the right-hand operand is conditionally dependent on the left-hand operand. We use the logical operators & and | if both operands must be evaluated. The subtlety of conditional operators is illustrated by the following examples:
And jangan lupa baca Precedence untuk nyelesain suatu operasi di Java ya. ==> a reference Java programmer must read!!!!
And terakhir, model2 soal yang tadi diajukan sebelumnya oleh Epul dan Iman itu soal2 tipe Sertifikasi Java, kalo mo liat soal sertifikasi Java silahkan googling aja dg keyword : Java+certification tambah keyword mock juga gpp.
ga ngerti..
/me sambil garuk-garuk kepala
bneran kudu kuliah lagi neh..
huhuuuuuhuu
Post a Comment