V Javi je logična ključna beseda primitiven podatkovni tip. Uporablja se za shranjevanje samo dveh možnih vrednosti, bodisi true ali false. Določa 1-bit informacije in njene 'velikosti' ni mogoče natančno definirati.
vrste testiranja
Logična ključna beseda se uporablja s spremenljivkami in metodami. Njegova privzeta vrednost je false. Na splošno je povezan s pogojnimi stavki.
Primeri logične ključne besede Java
1. primer: preprost logični primer
public class BooleanExample1 { public static void main(String[] args) { int num1=10; int num2=20; boolean b1=true; boolean b2=false; if(num1<num2) { system.out.println(b1); } else system.out.println(b2); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> true </pre> <h3>Example 2: Comparing the variables of boolean type</h3> <pre> public class BooleanExample2 { public static void main(String[] args) { boolean b1=true; boolean b2=false; boolean b3=(b1==b2); System.out.println(b1); System.out.println(b2); System.out.println(b3); } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> true false false </pre> <h3>Example 3: Method of boolean type</h3> <pre> public class BooleanExample3 { public static boolean display(int num) { if(num>10) { return true; } else { return false; } } public static void main(String[] args) { System.out.println(display(15)); } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> true </pre> <h3>Example 4: Comparing objects</h3> <pre> public class BooleanExample4 { public static void main(String[] args) { BooleanExample4 b1=new BooleanExample4(); BooleanExample4 b2=new BooleanExample4(); boolean result=(b1==b2); System.out.println('Is objects are equal : '+result); } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Is objects are equal : false </pre> <h3>Example 5: Finding a prime number</h3> <pre> public class BooleanExample5 { public static void main(String[] args) { int num=7; boolean flag=false; for(int i=2;i<num;i++) { if(num%i="=0)" flag="true;" break; } if(flag) system.out.println('not prime'); else system.out.println('prime'); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> prime </pre></num;i++)></pre></num2)>
Primer 2: Primerjava spremenljivk logičnega tipa
public class BooleanExample2 { public static void main(String[] args) { boolean b1=true; boolean b2=false; boolean b3=(b1==b2); System.out.println(b1); System.out.println(b2); System.out.println(b3); } }Preizkusite zdaj
Izhod:
true false false
Primer 3: Metoda logičnega tipa
public class BooleanExample3 { public static boolean display(int num) { if(num>10) { return true; } else { return false; } } public static void main(String[] args) { System.out.println(display(15)); } }Preizkusite zdaj
Izhod:
true
Primer 4: Primerjanje predmetov
public class BooleanExample4 { public static void main(String[] args) { BooleanExample4 b1=new BooleanExample4(); BooleanExample4 b2=new BooleanExample4(); boolean result=(b1==b2); System.out.println('Is objects are equal : '+result); } }Preizkusite zdaj
Izhod:
Is objects are equal : false
Primer 5: Iskanje praštevila
public class BooleanExample5 { public static void main(String[] args) { int num=7; boolean flag=false; for(int i=2;i<num;i++) { if(num%i="=0)" flag="true;" break; } if(flag) system.out.println(\'not prime\'); else system.out.println(\'prime\'); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> prime </pre></num;i++)>