logo

Java boolesk nøgleord

I Java er det boolske nøgleord en primitiv datatype. Den bruges til kun at gemme to mulige værdier, enten sand eller falsk. Den specificerer 1-bit information, og dens 'størrelse' kan ikke defineres præcist.

hvad er android påskeæg

Det booleske nøgleord bruges sammen med variabler og metoder. Dens standardværdi er falsk. Det er generelt forbundet med betingede udsagn.

Eksempler på Java booleske søgeord

Eksempel 1: Simpelt boolesk eksempel

 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&gt;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(&apos;Is objects are equal : &apos;+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)>

Eksempel 2: Sammenligning af variablerne af boolesk type

 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); } } 
Test det nu

Produktion:

javascript onload script
 true false false 

Eksempel 3: Metode af boolesk type

 public class BooleanExample3 { public static boolean display(int num) { if(num&gt;10) { return true; } else { return false; } } public static void main(String[] args) { System.out.println(display(15)); } } 
Test det nu

Produktion:

 true 

Eksempel 4: Sammenligning af objekter

 public class BooleanExample4 { public static void main(String[] args) { BooleanExample4 b1=new BooleanExample4(); BooleanExample4 b2=new BooleanExample4(); boolean result=(b1==b2); System.out.println(&apos;Is objects are equal : &apos;+result); } } 
Test det nu

Produktion:

 Is objects are equal : false 

Eksempel 5: At finde et primtal

 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++)>