Skip to main content

Type Conversion in Python: int(), float(), str() and bool() Explained

Have you ever entered a number into a Python program but received an error when trying to perform a calculation? This is a common problem for beginners. It often happens because the data type of a value is different from what the program expects. For example, when you use Python's input() function, the value entered by the user is returned as a string. If you want to perform mathematical calculations with that value, you may need to convert it into a numeric type first. This process is called type conversion . In this article, we will learn what type conversion is, why it is useful, how to convert values using int() , float() , str() , and bool() , and how to avoid common errors. 1. What Is Type Conversion in Python? Type conversion is the process of converting a value from one data type to another. For example, you may convert: A string into an integer. An integer into a floating-point number. A number into a string. A value into a Boolean value. Consider this example: number = ...

Master All Java Operators with Simple Explanations and Code Examples

In Java, operators are special symbols that perform operations on variables and values.

They help us carry out calculations, make decisions, and control the flow of programs efficiently.

In this article, you’ll learn all types of Java operators, along with their definitions, syntax, and examples — explained in simple terms.


๐Ÿง  What Are Operators in Java?

An operator is a symbol that tells the Java compiler to perform a specific operation on one, two, or three operands.

For example:

int a = 10, b = 5;
int sum = a + b;

Here:

  • + is an operator

  • a and b are operands

  • sum gets the value 15


๐Ÿ”น Types of Operators in Java

Java provides several categories of operators:

  1. Arithmetic Operators

  2. Relational Operators

  3. Logical Operators

  4. Assignment Operators

  5. Unary Operators

  6. Bitwise Operators

  7. Ternary Operator

  8. Shift Operators

  9. instanceof Operator

Let’s go through each of them in detail ๐Ÿ‘‡


๐Ÿงฎ 1. Arithmetic Operators

Arithmetic operators perform mathematical operations such as addition, subtraction, multiplication, etc.

Operator Name Description Example
+ Addition Adds two values a + b
- Subtraction Subtracts one value from another a - b
* Multiplication Multiplies two values a * b
/ Division Divides numerator by denominator a / b
% Modulus Returns the remainder a % b

Example:

int a = 10, b = 3;
System.out.println(a + b); // 13
System.out.println(a - b); // 7
System.out.println(a * b); // 30
System.out.println(a / b); // 3
System.out.println(a % b); // 1

⚖️ 2. Relational Operators

Relational operators compare two values and return either true or false.

Operator Name Description Example
== Equal to Checks if two values are equal a == b
!= Not equal to Checks if values are not equal a != b
> Greater than True if left is greater than right a > b
< Less than True if left is smaller than right a < b
>= Greater or equal True if left ≥ right a >= b
<= Less or equal True if left ≤ right a <= b

Example:

int a = 10, b = 5;
System.out.println(a > b);  // true
System.out.println(a == b); // false

๐Ÿงฉ 3. Logical Operators

Logical operators are used to combine two or more conditions and return a boolean result.

Operator Name Description Example
&& Logical AND Returns true if both conditions are true (a > b) && (a > 0)
|| || Logical OR
! Logical NOT Reverses the result !(a > b)

Example:

int a = 10, b = 5;
System.out.println((a > b) && (b > 0)); // true
System.out.println((a < b) || (b > 0)); // true
System.out.println(!(a == b)); // true

๐Ÿ–Š️ 4. Assignment Operators

Assignment operators assign values to variables or combine assignment with arithmetic operations.

Operator Name Description Example Equivalent
= Simple assignment Assigns right-hand value to left-hand variable a = 10 —
+= Add and assign Adds and assigns a += 5 a = a + 5
-= Subtract and assign Subtracts and assigns a -= 5 a = a - 5
*= Multiply and assign Multiplies and assigns a *= 5 a = a * 5
/= Divide and assign Divides and assigns a /= 5 a = a / 5
%= Modulus and assign Finds remainder and assigns a %= 5 a = a % 5

Example:

int a = 10;
a += 5;  // a = 15
a *= 2;  // a = 30
System.out.println(a);

๐Ÿ” 5. Unary Operators

Unary operators work with a single operand.

Operator Name Description Example
++ Increment Increases value by 1 a++ or ++a
-- Decrement Decreases value by 1 a-- or --a
+ Unary plus Indicates a positive value +a
- Unary minus Negates a value -a
! Logical NOT Reverses boolean value !true → false

Example:

int a = 5;
System.out.println(++a); // 6 (pre-increment)
System.out.println(a--); // 6 (post-decrement)
System.out.println(a);   // 5

๐Ÿ’ก 6. Bitwise Operators

Bitwise operators perform operations on bits (binary representation of integers).

Operator Name Description Example
& Bitwise AND Returns 1 if both bits are 1 a & b
| | Bitwise OR Returns 1 if any bit is 1
^ Bitwise XOR Returns 1 if bits are different a ^ b
~ Bitwise NOT Inverts all bits ~a
<< Left shift Shifts bits left a << 2
>> Right shift Shifts bits right a >> 2
>>> Unsigned right shift Shifts bits right and fills with 0 a >>> 2

Example:

int a = 5;  // Binary: 0101
int b = 7;  // Binary: 0111
System.out.println(a & b);  // 5  (0101)
System.out.println(a | b);  // 7  (0111)
System.out.println(a ^ b);  // 2  (0010)
System.out.println(~a);     // -6 (bitwise NOT)

๐Ÿ”„ 7. Ternary Operator

The ternary operator is a shorthand for if-else conditions.

Syntax:

condition ? expression1 : expression2;

Example:

int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Maximum: " + max);

Output:

Maximum: 20

๐Ÿš€ 8. Shift Operators

Shift operators move bits to the left or right.

Operator Description Example
<< Left shift (multiplies by 2) a << 1
>> Right shift (divides by 2) a >> 1
>>> Unsigned right shift a >>> 1

Example:

int a = 8; // Binary: 1000
System.out.println(a << 1); // 16
System.out.println(a >> 1); // 4

๐Ÿงฑ 9. instanceof Operator

This operator checks whether an object belongs to a specific class or subclass.
It returns true if the object is an instance of that class.

Example:

String name = "Java";
System.out.println(name instanceof String); // true

๐Ÿงญ Summary Table of Java Operators

Category Operators
Arithmetic +, -, *, /, %
Relational ==, !=, >, <, >=, <=
Logical &&, `
Assignment =, +=, -=, *=, /=, %=
Unary ++, --, +, -, !
Bitwise &, `
Ternary ?:
instanceof instanceof

✅ Conclusion

Operators are the core foundation of Java programming.
They make it possible to perform calculations, comparisons, logic, and data manipulation in a simple and powerful way.

By understanding all these operators — including arithmetic, logical (&&, ||), and bitwise (&, |, ^) — you can write efficient, logical, and optimized Java code.

Comments

Popular posts from this blog

Understanding Comments in Java: A Beginner’s Guide

When learning Java programming, one of the first concepts you’ll come across is comments . Comments may not affect how a program runs, but they play a crucial role in making code more understandable, readable, and maintainable. Let’s dive deeper into what comments are, why they’re important, and the different types of comments in Java. What are Comments in Java? A comment in Java is a piece of text written within a program that is ignored by the compiler . In other words, comments are notes for humans, not instructions for machines. They help developers explain the purpose of code, describe logic, or temporarily disable certain lines of code without deleting them. Why are Comments Important? ๐Ÿ“Œ Readability – Comments make code easier to understand for you and others. ๐Ÿ“Œ Maintenance – When you revisit your code after months, comments remind you what you were doing. ๐Ÿ“Œ Collaboration – In team projects, comments explain code to fellow developers. ๐Ÿ“Œ Debugging – You c...

Java Program to Check Voting Eligibility

In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not. In India, the minimum age required to vote is 18 years . ๐Ÿ’ป Program Code // Java Program to Check Voting Eligibility import java.util.Scanner; public class VotingEligibility { public static void main(String[] args) { // Create Scanner object to take input from user Scanner sc = new Scanner(System.in); // Ask user to enter age System.out.print("Enter your age: "); int age = sc.nextInt(); // Check eligibility if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); System.out.println("You can vote after " + (18 - age) + " year(s)."); } sc.close(); // Close the scanner } } ๐Ÿง  Explanation import java.util.Scanner; → Imp...

Java Program to Calculate Total Marks and Average of Five Subjects

๐Ÿ’ก Program Explanation In this program: The user will input marks of five subjects . The program will calculate the total marks and average . It will then display both results clearly. ๐Ÿง‘‍๐Ÿ’ป Java Code: // Java Program to Calculate Total Marks and Average of Five Subjects import java.util.Scanner; // Importing Scanner class for user input public class TotalAndAverage { public static void main(String[] args) { // Creating Scanner object to take input from user Scanner sc = new Scanner(System.in); // Declaring variables for five subjects double subject1, subject2, subject3, subject4, subject5; // Taking marks input for each subject System.out.print("Enter marks of Subject 1: "); subject1 = sc.nextDouble(); System.out.print("Enter marks of Subject 2: "); subject2 = sc.nextDouble(); System.out.print("Enter marks of Subject 3: "); subject3 = sc.nextDo...