Camposha
  • Home
  • Android
  • Buy Projects
  • My Account
    • Become a Member
    • Log In|Log Out
    • Free Projects
No Result
View All Result
Camposha
  • Home
  • Android
  • Buy Projects
  • My Account
    • Become a Member
    • Log In|Log Out
    • Free Projects
No Result
View All Result
Camposha
No Result
View All Result
ADVERTISEMENT
Home Java

Java Data Types – Boolean

2 years ago
in Java
Reading Time: 1min read
38 2
A A
40
SHARES
4k
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

SimilarPosts

Java Iterator

Java Iterator

2 years ago
4k
Java Iterable Interface

Java Iterable Interface

2 years ago
4k
Java Control Structures – Loops Introduction

Java Control Structures – Loops Introduction

2 years ago
4k
Java Operators – Logical Operators

Java Operators – Logical Operators

2 years ago
4k

 

Java like most programming languages uses the boolean data type to create variables iwth either true or false.

With this type we can use relational operators/comparison operators to compare twi two values. Say you want to compare whether a value is greater than another value.

A variable holding a boolean value is called a Boolean variable.

A boolean variable can hold only either of these two values:

  1. true
  2. false
1
2
boolean engineStared = true;
boolean planeLiftedOff = false;
The true and false are literals, just like say number 20. Furthermore they are reserved for use by Java so you cannot use them as identifiers.

Let’s look at simple quiz program. This program generates two random numbers using System.currentTimeMillis(). You then provide the answer. Then we compare your answer with what the true result. This returns true or false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package info.tutorialsloop;
 
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
 
        int firstNum = (int) (System.currentTimeMillis()%10);
        int secondNum = (int) (System.currentTimeMillis()/6%10);
 
        int total=firstNum+secondNum;
 
        Scanner input=new Scanner(System.in);
        System.out.print("Calculate "+firstNum+" + "+secondNum+" = ");
        int answer=input.nextInt();
 
        System.out.println(firstNum+" + "+secondNum + " = "+ answer+" is "+(total == answer));
 
    }
}
 
You type the answer then press enter to see result.
Here are some interesting answers:
1
2
3
4
5
6
7
8
9
10
11
12
Calculate 3 + 2 = 5
3 + 2 = 5 is true
 
Calculate 5 + 9 = 12
5 + 9 = 12 is false
 
Calculate 6 + 9 = 15
6 + 9 = 15 is true
 
Calculate 1 + 1 = 0
1 + 1 = 0 is false
 
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
Share16Tweet10Send
ADVERTISEMENT

Related Posts

Java Iterator
Java

Java Iterator

December 19, 2020
4k
Java Iterable Interface
Java

Java Iterable Interface

December 19, 2020
4k
Java Control Structures – Loops Introduction
Java

Java Control Structures – Loops Introduction

December 19, 2020
4k
Java Operators – Logical Operators
Java

Java Operators – Logical Operators

December 19, 2020
4k
Java Operators – Relational Operators
Java

Java Operators – Relational Operators

December 19, 2020
4k
Java URI
Java

Java URI

December 19, 2020
4k
ADVERTISEMENT

Get Free Projects

  • Home
  • Android
  • Buy Projects
  • My Account

© 2021 Camposha

No Result
View All Result
  • Account
  • Activate
  • Activity
  • Become a Teacher
  • Become a Teacher
  • Become a Teacher
  • Become instructor
  • Blog
  • Blog
  • Cancel Payment
  • Cancel Payment
  • Cart
  • Change Password
  • Change Password
  • Checkout
  • Checkout
  • Checkout
  • Contact
  • Contact
  • Contact Us
  • Content restricted
  • Course Checkout
  • Dashboard
  • Edit Profile
  • Edit Profile
  • FAQs
  • Forgot Password
  • Forgot Password
  • Guest
  • Guest
  • Home
  • Home
  • Home Light
  • Instructor Dashboard
  • Instructor Registration
  • IUMP – Account Page
  • IUMP – Default Redirect Page
  • IUMP – Login
  • IUMP – LogOut
  • IUMP – Register
  • IUMP – Reset Password
  • IUMP – TOS Page
  • IUMP – Visitor Inside User Page
  • List courses
  • List wish list
  • Login
  • Login
  • Maintenance
  • Members
  • Membership Account
    • Membership Billing
    • Membership Cancel
    • Membership Checkout
    • Membership Confirmation
    • Membership Invoice
    • Membership Levels
  • Membership Account
    • Membership Billing
    • Membership Cancel
    • Membership Checkout
    • Membership Confirmation
    • Membership Invoice
    • Membership Levels
  • Membership Plans
  • My Account
  • OnePage Documentation
  • Portfolio Grid
  • Portfolio Masonry
  • Portfolio Multigrid
  • Privacy Policy
  • Products
  • Profile
  • Profile
  • Profile
  • Projects
  • Register
  • Register
  • Register
  • Register
  • Sample Page
  • Shop
  • Sign in
  • Sign up
  • Student profile
  • Student Registration
  • Thank You
  • Thank You

© 2021 Camposha

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms below to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In