Discussion:
[問題] && 和 &
(时间太久无法回复)
unknown
2006-08-18 04:34:30 UTC
Permalink
public class ShortCircuit {

public static void main(String[] args) {
String s=null;
if((s != null) && s.length() > 20)
System.out.println(s);

String t=null;
if((t != null) & t.length() > 20)
System.out.println(t);
}
}

如題,第一個if不會印出東西,(短路運算子)

第二個if為什麼會有NullPointerException呢?

謝謝回應


--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.229.203.88
骨頭
2006-08-18 04:39:45 UTC
Permalink
�� �ޭz�mseekyou (galagala~��N�� )�n���ʨ��G
: public class ShortCircuit {
: public static void main(String[] args) {
: String s=null;
: if((s != null) && s.length() > 20)
&& �O �e���������ˬd
�p�G���Ť���
�N���|�h�ˬds.length()

& �h�Ϥ� , ���|�h�ˬdt.length()
�]��t�Onull
null ���M������.length()
�N�|�X�{nullPointerException
: System.out.println(s);
: String t=null;
: if((t != null) & t.length() > 20)
: System.out.println(t);
: }
: }
: �p�D,�Ĥ@��if���|�L�X�F��,(�u���B���l)
: �ĤG��if�������|��NullPointerException�O?
: ���¦^�

--
 String temp="relax"; | Life just like programing
 while(buringlife) String.forgot(temp); | to be right or wrong
 while(sleeping) brain.setMemoryOut(); | need not to say
 stack.push(life.running); | the complier will
 stack.push(scouting.buck()); | answer your life
 stack.push(bowling.pratice()); | Bone everything

--
�� �o�H��: ���������~�{(ptt.cc)
�� From: 140.138.240.58
unknown
2006-08-18 04:41:04 UTC
Permalink
�� �ޭz�mTonyQ (���Y)�n���ʨ��G
: �� �ޭz�mseekyou (galagala~��N�� )�n���ʨ��G
: : public class ShortCircuit {
: : public static void main(String[] args) {
: : String s=null;
: : if((s != null) && s.length() > 20)
: && �O �e���������ˬd
: �p�G���Ť���
: �N���|�h�ˬds.length()
: & �h�Ϥ� , ���|�h�ˬdt.length()
: �]��t�Onull
: null ���M������.length()
: �N�|�X�{nullPointerException
: : System.out.println(s);
: : String t=null;
: : if((t != null) & t.length() > 20)
: : System.out.println(t);
: : }
: : }
: : �p�D,�Ĥ@��if���|�L�X�F��,(�u���B���l)
: : �ĤG��if�������|��NullPointerException�O?
: : ���¦^�

�F�ѤF^^,�ҥH�Ĥ@��if��exception,���b�᭱,�N���|�o�{�F = =+

--
�� �o�H��: ���������~�{(ptt.cc)
�� From: 220.229.203.88
囧rz
2006-08-18 21:08:36 UTC
Permalink
�� �ޭz�mseekyou (galagala~��N�� )�n���ʨ��G
: public class ShortCircuit {
: public static void main(String[] args) {
: String s=null;
: if((s != null) && s.length() > 20)
: System.out.println(s);
: String t=null;
: if((t != null) & t.length() > 20)
: System.out.println(t);
: }
: }
: �p�D,�Ĥ@��if���|�L�X�F��,(�u���B���l)
: �ĤG��if�������|��NullPointerException�O?
: ���¦^�
���ӹB���l�������N�q�N���P
&& �O�]���OLogical��"AND", �u�����B���l��true�ɤ~�|�h�B���k�B���l,
���X��boolean,

& �Obitwise��"AND", �u�వ�����B���l�Oboolean/Boolean��int/Integer
���X�ݨ����p�G�ܤ֦��@���Oboolean, ���N�Oboolean, �p�G���Oint,
���N�Oint

�Ĥ@�ӧP�_��, �N�q��: �Y
���B���l(s != null)�����G"�M"
�k�B���l(s.length..)�����G
�����u, �hSystem.ooxx...���]���ӫe�z, ���B���l�����G��false,���y
�B���l���i�ର�u, �ҥH�k�B���l�N���ޤF

�ĤG�ӧP�_�����N�q��
"���B���l�����G"�M"�k�B���l�����G"��bitwise��AND
�ҥH���ӹB���l�@�w���n�o�쵲�G�~����
compiler�ä��|���h�ˬd���B���l, �o�{�L�O0�F, �N���ܤ���AND(why?)
�ҥH�ĤG�Ӧb�B���k�B���l���ɭԷ|�X��.

--
�� �o�H��: ���������~�{(ptt.cc)
�� From: 140.113.209.4

继续阅读narkive:
Loading...